Hoiw to implement "QT_MESSAGE_PATTERN
-
I like to colorize parts (text) of the QDebug output.
After RTFM I am not sure how.,Obviously my code is not correct .... and the example is not very informative....
C++ code example would be appreciated.
``
#ifdef TRACE_MDI_ADD_SUBWINDOWexport QT_MESSAGE_PATTERN="`echo -e "\033[34m%{function}\033[0m: %{message}"`" text = " START trace adding subw8indow (Continue MDI TRACE) "; text += Q_FUNC_INFO; text += " @ line "; text += QString::number(__LINE__); // change color ?? qDebug(). qDebug() << text; #endif
-
@AnneRanch
Hello!
qDebug("\033[1;34m Bold Blue"); qDebug("\033[1;33m Bold Yellow"); qDebug("\033[0m Reset");
I have tested with Linux and with Windows
-
I like to colorize parts (text) of the QDebug output.
After RTFM I am not sure how.,Obviously my code is not correct .... and the example is not very informative....
C++ code example would be appreciated.
``
#ifdef TRACE_MDI_ADD_SUBWINDOWexport QT_MESSAGE_PATTERN="`echo -e "\033[34m%{function}\033[0m: %{message}"`" text = " START trace adding subw8indow (Continue MDI TRACE) "; text += Q_FUNC_INFO; text += " @ line "; text += QString::number(__LINE__); // change color ?? qDebug(). qDebug() << text; #endif
Hi @AnneRanch
I like to colorize parts (text) of the QDebug output.
Here's how I do it in one of my open-source projects:
#if defined(Q_OS_UNIX) #include <unistd.h> #elif defined(Q_OS_WIN) #include <Windows.h> #endif inline bool haveConsole() { #if defined(Q_OS_UNIX) return isatty(STDERR_FILENO); #elif defined(Q_OS_WIN) return GetConsoleWindow(); #else return false; #endif } void configureLogging(const QCommandLineParser &parser) { // Start with the Qt default message pattern (see qtbase:::qlogging.cpp:defaultPattern) QString messagePattern = QStringLiteral("%{if-category}%{category}: %{endif}%{message}"); if (parser.isSet(QStringLiteral("debug"))) { #ifdef QT_MESSAGELOGCONTEXT // %{file}, %{line} and %{function} are only available when QT_MESSAGELOGCONTEXT is set. messagePattern.prepend(QStringLiteral("%{function} ")); #endif messagePattern.prepend(QStringLiteral("%{time process} %{threadid} %{type} ")); } const QString color = parser.value(QStringLiteral("color")); if ((color == QStringLiteral("yes")) || (color == QStringLiteral("auto") && haveConsole())) { messagePattern.prepend(QStringLiteral( "%{if-debug}\x1b[37m%{endif}" // White "%{if-info}\x1b[32m%{endif}" // Green "%{if-warning}\x1b[35m%{endif}" // Magenta "%{if-critical}\x1b[31m%{endif}" // Red "%{if-fatal}\x1b[31;1m%{endif}")); // Red and bold messagePattern.append(QStringLiteral("\x1b[0m")); // Reset. } qSetMessagePattern(messagePattern); }
Cheers.