![]() |
Qt 6.x
The Qt SDK
|
\nonreentrant More...
#include <qcoloroutput_p.h>
Public Types | |
enum | ColorCodeComponent { BlackForeground = 1 << ForegroundShift , BlueForeground = 2 << ForegroundShift , GreenForeground = 3 << ForegroundShift , CyanForeground = 4 << ForegroundShift , RedForeground = 5 << ForegroundShift , PurpleForeground = 6 << ForegroundShift , BrownForeground = 7 << ForegroundShift , LightGrayForeground = 8 << ForegroundShift , DarkGrayForeground = 9 << ForegroundShift , LightBlueForeground = 10 << ForegroundShift , LightGreenForeground = 11 << ForegroundShift , LightCyanForeground = 12 << ForegroundShift , LightRedForeground = 13 << ForegroundShift , LightPurpleForeground = 14 << ForegroundShift , YellowForeground = 15 << ForegroundShift , WhiteForeground = 16 << ForegroundShift , BlackBackground = 1 << BackgroundShift , BlueBackground = 2 << BackgroundShift , GreenBackground = 3 << BackgroundShift , CyanBackground = 4 << BackgroundShift , RedBackground = 5 << BackgroundShift , PurpleBackground = 6 << BackgroundShift , BrownBackground = 7 << BackgroundShift , DefaultColor = 1 << SpecialShift } |
using | ColorCode = QFlags< ColorCodeComponent > |
using | ColorMapping = QHash< int, ColorCode > |
Public Member Functions | |
QColorOutput () | |
~QColorOutput () | |
bool | isSilent () const |
void | setSilent (bool silent) |
void | insertMapping (int colorID, ColorCode colorCode) |
void | writeUncolored (const QString &message) |
void | write (const QStringView message, int color=-1) |
Q_WEAK_OVERLOAD void | write (const QString &message, int color=-1) |
void | writePrefixedMessage (const QString &message, QtMsgType type, const QString &prefix=QString()) |
QString | colorify (QStringView message, int color=-1) const |
\nonreentrant
Outputs colored messages to stderr
.
QColorOutput is a convenience class for outputting messages to stderr
using color escape codes, as mandated in ECMA-48. QColorOutput will only color output when it is detected to be suitable. For instance, if stderr
is detected to be attached to a file instead of a TTY, no coloring will be done.
QColorOutput does its best attempt. but it is generally undefined what coloring or effect the various coloring flags has. It depends strongly on what terminal software that is being used.
When using ‘echo -e 'my escape sequence’`, {\033} works as an initiator but not when printing from a C++ program, despite having escaped the backslash. That's why we below use characters with value 0x1B.
It can be convenient to subclass QColorOutput with a private scope, such that the functions are directly available in the class using it.
To output messages, call write() or writeUncolored(). write() takes as second argument an integer, which QColorOutput uses as a lookup key to find the color it should color the text in. The mapping from keys to colors is done using insertMapping(). Typically this is used by having enums for the various kinds of messages, which subsequently are registered.
Definition at line 27 of file qcoloroutput_p.h.
Definition at line 68 of file qcoloroutput_p.h.
using QColorOutput::ColorMapping = QHash<int, ColorCode> |
Definition at line 69 of file qcoloroutput_p.h.
\value BlackForeground \value BlueForeground \value GreenForeground \value CyanForeground \value RedForeground \value PurpleForeground \value BrownForeground \value LightGrayForeground \value DarkGrayForeground \value LightBlueForeground \value LightGreenForeground \value LightCyanForeground \value LightRedForeground \value LightPurpleForeground \value YellowForeground \value WhiteForeground \value BlackBackground \value BlueBackground \value GreenBackground \value CyanBackground \value RedBackground \value PurpleBackground \value BrownBackground
\value DefaultColor QColorOutput performs no coloring. This typically means black on white or white on black, depending on the settings of the user's terminal.
Definition at line 39 of file qcoloroutput_p.h.
QColorOutput::QColorOutput | ( | ) |
Constructs a QColorOutput instance, ready for use.
Definition at line 203 of file qcoloroutput.cpp.
|
default |
QString QColorOutput::colorify | ( | QStringView | message, |
int | colorID = -1 |
||
) | const |
Treats message and colorID identically to write(), but instead of writing message to stderr
, it is prepared for being written to stderr
, but is then returned.
This is useful when the colored string is inserted into a translated string(dividing the string into several small strings prevents proper translation).
Definition at line 268 of file qcoloroutput.cpp.
References QString::append(), arg, QColorOutputPrivate::backgrounds, QColorOutputPrivate::color(), QColorOutputPrivate::coloringEnabled(), QColorOutputPrivate::containsColor(), DefaultColor, QColorOutputPrivate::escapeCode(), QColorOutputPrivate::foregrounds, QString::fromLatin1(), Q_ASSERT_X, Q_FUNC_INFO, qPrintable, and QColorOutputPrivate::setCurrentColorID().
Referenced by write().
Adds a color mapping from colorID to colorCode, for this QColorOutput instance.
Definition at line 320 of file qcoloroutput.cpp.
References QColorOutputPrivate::insertColor().
Referenced by QQmlJSLogger::QQmlJSLogger().
bool QColorOutput::isSilent | ( | ) | const |
Definition at line 208 of file qcoloroutput.cpp.
References QColorOutputPrivate::isSilent().
void QColorOutput::setSilent | ( | bool | silent | ) |
Definition at line 209 of file qcoloroutput.cpp.
References QColorOutputPrivate::setSilent().
|
inline |
Definition at line 82 of file qcoloroutput_p.h.
References write().
Referenced by write().
void QColorOutput::write | ( | const QStringView | message, |
int | colorID = -1 |
||
) |
Sends message to stderr
, using the color looked up in the color mapping using colorID.
If color isn't available in the color mapping, result and behavior is undefined.
If colorID is 0, which is the default value, the previously used coloring is used. QColorOutput is initialized to not color at all.
If message is empty, effects are undefined.
message will be printed as is. For instance, no line endings will be inserted.
Definition at line 224 of file qcoloroutput.cpp.
References colorify(), QColorOutputPrivate::isSilent(), and QColorOutputPrivate::write().
Referenced by QQmlJSLogger::processMessages(), and writePrefixedMessage().
void QColorOutput::writePrefixedMessage | ( | const QString & | message, |
QtMsgType | type, | ||
const QString & | prefix = QString() |
||
) |
Definition at line 230 of file qcoloroutput.cpp.
References QHash< Key, T >::contains(), QString::front(), QString::isEmpty(), QChar::isUpper(), Q_ASSERT, QStringLiteral, QtCriticalMsg, QtDebugMsg, QtInfoMsg, QtWarningMsg, write(), and writeUncolored().
Writes message to stderr
as if for instance QTextStream would have been used, and adds a line ending at the end.
This function can be practical to use such that one can use QColorOutput for all forms of writing.
Definition at line 253 of file qcoloroutput.cpp.
References QColorOutputPrivate::isSilent(), and QColorOutputPrivate::write().
Referenced by writePrefixedMessage().