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.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "qcoloroutput_p.h"
5
6#include <QtCore/qfile.h>
7#include <QtCore/qhash.h>
8
9#ifndef Q_OS_WIN
10#include <unistd.h>
11#endif
12
14
16{
17public:
19 {
20 m_coloringEnabled = isColoringPossible();
21 }
22
23 static const char *const foregrounds[];
24 static const char *const backgrounds[];
25
26 inline void write(const QString &msg)
27 {
28 const QByteArray encodedMsg = msg.toLocal8Bit();
29 fwrite(encodedMsg.constData(), size_t(1), size_t(encodedMsg.size()), stderr);
30 }
31
33 {
34 const ushort escapeChar = 0x1B;
36 result.append(QChar(escapeChar));
40 return result;
41 }
42
43 void insertColor(int id, QColorOutput::ColorCode code) { m_colorMapping.insert(id, code); }
44 QColorOutput::ColorCode color(int id) const { return m_colorMapping.value(id); }
45 bool containsColor(int id) const { return m_colorMapping.contains(id); }
46
47 void setSilent(bool silent) { m_silent = silent; }
48 bool isSilent() const { return m_silent; }
49
50 void setCurrentColorID(int colorId) { m_currentColorID = colorId; }
51
52 bool coloringEnabled() const { return m_coloringEnabled; }
53
54private:
55 QFile m_out;
56 QColorOutput::ColorMapping m_colorMapping;
57 int m_currentColorID = -1;
58 bool m_coloringEnabled = false;
59 bool m_silent = false;
60
61 /*
62 Returns true if it's suitable to send colored output to \c stderr.
63 */
64 inline bool isColoringPossible() const
65 {
66#if defined(Q_OS_WIN)
67 /* Windows doesn't at all support ANSI escape codes, unless
68 * the user install a "device driver". See the Wikipedia links in the
69 * class documentation for details. */
70 return false;
71#else
72 /* We use QFile::handle() to get the file descriptor. It's a bit unsure
73 * whether it's 2 on all platforms and in all cases, so hopefully this layer
74 * of abstraction helps handle such cases. */
75 return isatty(fileno(stderr));
76#endif
77 }
78};
79
81{
82 "0;30",
83 "0;34",
84 "0;32",
85 "0;36",
86 "0;31",
87 "0;35",
88 "0;33",
89 "0;37",
90 "1;30",
91 "1;34",
92 "1;32",
93 "1;36",
94 "1;31",
95 "1;35",
96 "1;33",
97 "1;37"
98};
99
101{
102 "0;40",
103 "0;44",
104 "0;42",
105 "0;46",
106 "0;41",
107 "0;45",
108 "0;43"
109};
110
204
205// must be here so that QScopedPointer has access to the complete type
207
208bool QColorOutput::isSilent() const { return d->isSilent(); }
209void QColorOutput::setSilent(bool silent) { d->setSilent(silent); }
210
225{
226 if (!d->isSilent())
227 d->write(colorify(message, colorID));
228}
229
231 const QString &prefix)
232{
233 static const QHash<QtMsgType, QString> prefixes = {
238 };
239
240 Q_ASSERT(prefixes.contains(type));
241 Q_ASSERT(prefix.isEmpty() || prefix.front().isUpper());
242 write((prefix.isEmpty() ? prefixes[type] : prefix) + QStringLiteral(": "), type);
244}
245
254{
255 if (!d->isSilent())
256 d->write(message + QLatin1Char('\n'));
257}
258
269{
270 Q_ASSERT_X(colorID == -1 || d->containsColor(colorID), Q_FUNC_INFO,
271 qPrintable(QString::fromLatin1("There is no color registered by id %1")
272 .arg(colorID)));
273 Q_ASSERT_X(!message.isEmpty(), Q_FUNC_INFO,
274 "It makes no sense to attempt to print an empty string.");
275
276 if (colorID != -1)
277 d->setCurrentColorID(colorID);
278
279 if (d->coloringEnabled() && colorID != -1) {
280 const int color = d->color(colorID);
281
282 /* If DefaultColor is set, we don't want to color it. */
283 if (color & DefaultColor)
284 return message.toString();
285
286 const int foregroundCode = (color & ForegroundMask) >> ForegroundShift;
287 const int backgroundCode = (color & BackgroundMask) >> BackgroundShift;
288 QString finalMessage;
289 bool closureNeeded = false;
290
291 if (foregroundCode > 0) {
292 finalMessage.append(
294 QLatin1String(QColorOutputPrivate::foregrounds[foregroundCode - 1])));
295 closureNeeded = true;
296 }
297
298 if (backgroundCode > 0) {
299 finalMessage.append(
301 QLatin1String(QColorOutputPrivate::backgrounds[backgroundCode - 1])));
302 closureNeeded = true;
303 }
304
305 finalMessage.append(message);
306
307 if (closureNeeded)
309
310 return finalMessage;
311 }
312
313 return message.toString();
314}
315
320void QColorOutput::insertMapping(int colorID, const ColorCode colorCode)
321{
322 d->insertColor(colorID, colorCode);
323}
324
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
static const char *const foregrounds[]
static QString escapeCode(const QString &in)
bool coloringEnabled() const
void write(const QString &msg)
static const char *const backgrounds[]
void setSilent(bool silent)
bool containsColor(int id) const
bool isSilent() const
void setCurrentColorID(int colorId)
void insertColor(int id, QColorOutput::ColorCode code)
QColorOutput::ColorCode color(int id) const
void setSilent(bool silent)
void write(const QStringView message, int color=-1)
void writePrefixedMessage(const QString &message, QtMsgType type, const QString &prefix=QString())
bool isSilent() const
void insertMapping(int colorID, ColorCode colorCode)
void writeUncolored(const QString &message)
QString colorify(QStringView message, int color=-1) const
\inmodule QtCore
Definition qfile.h:93
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
T value(const Key &key) const noexcept
Definition qhash.h:1054
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QChar front() const
Definition qstring.h:230
QByteArray toLocal8Bit() const &
Definition qstring.h:638
QString & append(QChar c)
Definition qstring.cpp:3252
Combined button and popup list for selecting options.
#define Q_FUNC_INFO
QtMsgType
Definition qlogging.h:29
@ QtCriticalMsg
Definition qlogging.h:32
@ QtInfoMsg
Definition qlogging.h:34
@ QtWarningMsg
Definition qlogging.h:31
@ QtDebugMsg
Definition qlogging.h:30
GLuint color
[2]
GLenum type
GLuint GLsizei const GLchar * message
GLuint in
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
SSL_CTX int void * arg
#define qPrintable(string)
Definition qstring.h:1531
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
unsigned short ushort
Definition qtypes.h:33
\inmodule QtCore \reentrant
Definition qchar.h:18