Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QColorOutput Class Reference

\nonreentrant More...

#include <qcoloroutput_p.h>

+ Collaboration diagram for QColorOutput:

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
 

Detailed Description

\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.

enum MyMessage
{
Important
};
output.insertMapping(Import, QColorOutput::BlueForeground);
output.write("This is important", Important);
output.write("Jack, I'm only the selected official!", Error);
\nonreentrant
QT_BEGIN_NAMESPACE typedef uchar * output
See also
{http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html}{Bash Prompt HOWTO, 6.1. Colors}, {http://linuxgazette.net/issue51/livingston-blade.html}{Linux Gazette, Tweaking Eterm, Edward Livingston-Blade}, {http://www.ecma-international.org/publications/standards/Ecma-048.htm}{Standard ECMA-48, Control Functions for Coded Character Sets, ECMA International}, {http://en.wikipedia.org/wiki/ANSI_escape_code}{Wikipedia, ANSI escape code}, {http://linuxgazette.net/issue65/padala.html}{Linux Gazette, So You Like Color!, Pradeep Padala}

Definition at line 27 of file qcoloroutput_p.h.

Member Typedef Documentation

◆ ColorCode

◆ ColorMapping

Definition at line 69 of file qcoloroutput_p.h.

Member Enumeration Documentation

◆ ColorCodeComponent

\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.

Enumerator
BlackForeground 
BlueForeground 
GreenForeground 
CyanForeground 
RedForeground 
PurpleForeground 
BrownForeground 
LightGrayForeground 
DarkGrayForeground 
LightBlueForeground 
LightGreenForeground 
LightCyanForeground 
LightRedForeground 
LightPurpleForeground 
YellowForeground 
WhiteForeground 
BlackBackground 
BlueBackground 
GreenBackground 
CyanBackground 
RedBackground 
PurpleBackground 
BrownBackground 
DefaultColor 

Definition at line 39 of file qcoloroutput_p.h.

Constructor & Destructor Documentation

◆ QColorOutput()

QColorOutput::QColorOutput ( )

Constructs a QColorOutput instance, ready for use.

Definition at line 203 of file qcoloroutput.cpp.

◆ ~QColorOutput()

QColorOutput::~QColorOutput ( )
default

Member Function Documentation

◆ colorify()

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertMapping()

void QColorOutput::insertMapping ( int colorID,
ColorCode colorCode )

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isSilent()

bool QColorOutput::isSilent ( ) const

Definition at line 208 of file qcoloroutput.cpp.

References QColorOutputPrivate::isSilent().

+ Here is the call graph for this function:

◆ setSilent()

void QColorOutput::setSilent ( bool silent)

Definition at line 209 of file qcoloroutput.cpp.

References QColorOutputPrivate::setSilent().

+ Here is the call graph for this function:

◆ write() [1/2]

Q_WEAK_OVERLOAD void QColorOutput::write ( const QString & message,
int color = -1 )
inline

Definition at line 82 of file qcoloroutput_p.h.

References write().

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write() [2/2]

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writePrefixedMessage()

void QColorOutput::writePrefixedMessage ( const QString & message,
QtMsgType type,
const QString & prefix = QString() )

Definition at line 230 of file qcoloroutput.cpp.

References QString::front(), QString::isEmpty(), Q_ASSERT, QStringLiteral, QtCriticalMsg, QtDebugMsg, QtInfoMsg, QtWarningMsg, write(), and writeUncolored().

+ Here is the call graph for this function:

◆ writeUncolored()

void QColorOutput::writeUncolored ( const QString & message)

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: