Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hoiw to implement "QT_MESSAGE_PATTERN
Forum Updated to NodeBB v4.3 + New Features

Hoiw to implement "QT_MESSAGE_PATTERN

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 456 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

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

    https://woboq.com/blog/nice-debug-output-with-qt.html#:~:text=The trick I use which work with bash,and then the message in the normal color

    C++ code example would be appreciated.

    ``
    #ifdef TRACE_MDI_ADD_SUBWINDOW

    export 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
    
    Paul ColbyP 1 Reply Last reply
    0
    • posktomtenP Offline
      posktomtenP Offline
      posktomten
      wrote on last edited by posktomten
      #2

      @AnneRanch

      Hello!

      qDebug("\033[1;34m Bold Blue");
      qDebug("\033[1;33m Bold Yellow");
      qDebug("\033[0m Reset");
      

      Color Code

      I have tested with Linux and with Windows

      posktomten

      1 Reply Last reply
      0
      • A Anonymous_Banned275

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

        https://woboq.com/blog/nice-debug-output-with-qt.html#:~:text=The trick I use which work with bash,and then the message in the normal color

        C++ code example would be appreciated.

        ``
        #ifdef TRACE_MDI_ADD_SUBWINDOW

        export 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
        
        Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved