Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Questions Concerning colorizing text in Ubuntu terminal.
QtWS25 Last Chance

Questions Concerning colorizing text in Ubuntu terminal.

Scheduled Pinned Locked Moved Unsolved The Lounge
62 Posts 6 Posters 12.2k Views
  • 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 21 Sept 2021, 18:44 last edited by
    #50

    See QLabel::setText() and supported HTML subset.
    Enough said about ansi escape sequences.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Cougar 0
      wrote on 21 Sept 2021, 19:00 last edited by
      #51

      Will do, Thanks!

      Cougar

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cougar 0
        wrote on 21 Sept 2021, 20:43 last edited by Cougar 0
        #52

        @JonB, Thanks for pointing out that QT is not a programming language that explains a lot!
        @JonB said in Questions Concerning colorizing text in Ubuntu terminal.:

        Do you mean you just want to translate the 10 lines of Perl code you wrote above to C++/Qt? I will post that for you if that's what you want (no charge!).

        If you will let me except your offer to do that I would really appreciate it!
        I would be more than happy to compensate you for doing that!
        Just let me know.
        Thanks!

        Cougar

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cougar 0
          wrote on 23 Sept 2021, 07:26 last edited by Cougar 0
          #53

          @JonB said in Questions Concerning colorizing text in Ubuntu terminal.:

          You are mixing your expectations of what should happen in a terminal versus creating Qt *windows^ and thinking the same code will work there.
          You can do what the Perl script does equally from any C or C++ program. Just write the same sequences to stdout, and if you run it from a terminal/Command Prompt it will behave like the Perl script.

          I thought the same code would work, No, I just like seeing the difference.
          A car's engine cannot run on water, it takes gasoline.
          But you can see the difference in the two.
          The problem with my post is that there are two different subjects and I believe that is where I confused some you guys and started upsetting some people, of which I never meant to do that.
          Now that I have figured all this out, you were correct from the beginning.
          All I need is the following 8 written in Perl, rewritten in C++.

          $SIG{__WARN__} = $old;
          
            $type = "\e[1m\e[91m$type\e[0m" if ($type =~ m/(fatal|fail|error|stop)/i);# bold red
            $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i);  # red
            $type = "\e[93m$type\e[0m" if ($type =~ m/(reset|warning|secure|unset)/i);# yellow
            $type = "\e[95m$type\e[0m" if ($type =~ m/(add|update|delete)/i);         # magenta
            $type = "\e[96m$type\e[0m" if ($type =~ m/(list|uplink)/i);               # cyan
            $type = "\e[94m$type\e[0m" if ($type =~ m/(beacon|syncer)/i);             # blue
            $type = "\e[92m$type\e[0m" if ($type =~ m/(stat|kfnew)/i);                # green
            $type = "\e[1m\e[92m$type\e[0m" if ($type =~ m/(info|debug)/i);           # bold green
          
            # print to stdout if enabled
            print "[$time]\t[$type]\t$msg\n" if $self->{printlog};
          

          Thank you to all who posted and tried to help this old man!
          @JonB, I'd be glad to compensate you for doing this, my email address. wpt1114 at yahoo.com
          Thanks

          Cougar

          J 2 Replies Last reply 23 Sept 2021, 07:47
          0
          • C Cougar 0
            23 Sept 2021, 07:26

            @JonB said in Questions Concerning colorizing text in Ubuntu terminal.:

            You are mixing your expectations of what should happen in a terminal versus creating Qt *windows^ and thinking the same code will work there.
            You can do what the Perl script does equally from any C or C++ program. Just write the same sequences to stdout, and if you run it from a terminal/Command Prompt it will behave like the Perl script.

            I thought the same code would work, No, I just like seeing the difference.
            A car's engine cannot run on water, it takes gasoline.
            But you can see the difference in the two.
            The problem with my post is that there are two different subjects and I believe that is where I confused some you guys and started upsetting some people, of which I never meant to do that.
            Now that I have figured all this out, you were correct from the beginning.
            All I need is the following 8 written in Perl, rewritten in C++.

            $SIG{__WARN__} = $old;
            
              $type = "\e[1m\e[91m$type\e[0m" if ($type =~ m/(fatal|fail|error|stop)/i);# bold red
              $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i);  # red
              $type = "\e[93m$type\e[0m" if ($type =~ m/(reset|warning|secure|unset)/i);# yellow
              $type = "\e[95m$type\e[0m" if ($type =~ m/(add|update|delete)/i);         # magenta
              $type = "\e[96m$type\e[0m" if ($type =~ m/(list|uplink)/i);               # cyan
              $type = "\e[94m$type\e[0m" if ($type =~ m/(beacon|syncer)/i);             # blue
              $type = "\e[92m$type\e[0m" if ($type =~ m/(stat|kfnew)/i);                # green
              $type = "\e[1m\e[92m$type\e[0m" if ($type =~ m/(info|debug)/i);           # bold green
            
              # print to stdout if enabled
              print "[$time]\t[$type]\t$msg\n" if $self->{printlog};
            

            Thank you to all who posted and tried to help this old man!
            @JonB, I'd be glad to compensate you for doing this, my email address. wpt1114 at yahoo.com
            Thanks

            J Offline
            J Offline
            JonB
            wrote on 23 Sept 2021, 07:47 last edited by
            #54

            @Cougar-0
            I have just seen your post and will translate just these lines to C++ using Qt to do the regular expression matching. I have to go shopping in 30+ minutes time, I hope to have posted it by then.

            1 Reply Last reply
            0
            • C Cougar 0
              23 Sept 2021, 07:26

              @JonB said in Questions Concerning colorizing text in Ubuntu terminal.:

              You are mixing your expectations of what should happen in a terminal versus creating Qt *windows^ and thinking the same code will work there.
              You can do what the Perl script does equally from any C or C++ program. Just write the same sequences to stdout, and if you run it from a terminal/Command Prompt it will behave like the Perl script.

              I thought the same code would work, No, I just like seeing the difference.
              A car's engine cannot run on water, it takes gasoline.
              But you can see the difference in the two.
              The problem with my post is that there are two different subjects and I believe that is where I confused some you guys and started upsetting some people, of which I never meant to do that.
              Now that I have figured all this out, you were correct from the beginning.
              All I need is the following 8 written in Perl, rewritten in C++.

              $SIG{__WARN__} = $old;
              
                $type = "\e[1m\e[91m$type\e[0m" if ($type =~ m/(fatal|fail|error|stop)/i);# bold red
                $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i);  # red
                $type = "\e[93m$type\e[0m" if ($type =~ m/(reset|warning|secure|unset)/i);# yellow
                $type = "\e[95m$type\e[0m" if ($type =~ m/(add|update|delete)/i);         # magenta
                $type = "\e[96m$type\e[0m" if ($type =~ m/(list|uplink)/i);               # cyan
                $type = "\e[94m$type\e[0m" if ($type =~ m/(beacon|syncer)/i);             # blue
                $type = "\e[92m$type\e[0m" if ($type =~ m/(stat|kfnew)/i);                # green
                $type = "\e[1m\e[92m$type\e[0m" if ($type =~ m/(info|debug)/i);           # bold green
              
                # print to stdout if enabled
                print "[$time]\t[$type]\t$msg\n" if $self->{printlog};
              

              Thank you to all who posted and tried to help this old man!
              @JonB, I'd be glad to compensate you for doing this, my email address. wpt1114 at yahoo.com
              Thanks

              J Offline
              J Offline
              JonB
              wrote on 23 Sept 2021, 08:21 last edited by JonB
              #55

              @Cougar-0
              Here are those Perl lines rewritten in C++ + Qt for the reg exs.

                  QString type;
                  type = "This is a fatal message";
                  //type = "This is a debug message";
              
                  if (type.contains(QRegularExpression("(fatal|fail|error|stop)")))
                      type = QString("\e[1m\e[91m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(refused|nodevice|timeout)")))
                      type = QString("\e[91m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(reset|warning|secure|unset)")))
                      type = QString("\e[93m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(add|update|delete)")))
                      type = QString("\e[95m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(list|uplink)")))
                      type = QString("\e[96m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(beacon|syncer)")))
                      type = QString("\e[94m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(stat|kfnew)")))
                      type = QString("\e[92m%1\e[0m").arg(type);
                  if (type.contains(QRegularExpression("(info|debug)")))
                      type = QString("\e[1m\e[92m%1\e[0m").arg(type);
              
                  qDebug() << type;
              

              You will need #include <QRegularExpression> at the start of your source file. You should put these lines somewhere after the initial QApplication app(argc, argv);.

              You will note this is similar to what @SGaist wrote much earlier above. I have stuck to rigidly translating your Perl just as-is.

              You have a string variable named type. You need to set it to whatever you want before the code (I show a couple of examples), and after the code it has been changed to the original with the colorizing sequences.

              In practice you will want these lines in some re-usable function, so that the outside world can pass in the desired string for type. The function might then output the result string, or it might return that string for use in the caller.

              The Perl code as translated will not fare well if the input string contains more than one of the words for different colors (e.g. "This is fatal list info"), but that is how the Perl code has been written.

              Finally I would remind you again that this will only show colorized if you send the output to a terminal, e.g. run it as a command-line application in a Windows Command Prompt or Linux xterm. We could adapt this principle to produce, say, a string which you could use on a QLabel in a Qt UI application; just it would not use the same escape sequences as you have (we could do text coloring by HTML which is accepted by a QLabel instead).

              1 Reply Last reply
              2
              • C Offline
                C Offline
                Cougar 0
                wrote on 27 Sept 2021, 09:55 last edited by
                #56

                alt text
                Thanks @JonB, @SGaist , you guys helped me learn a lot!
                Which brings me to this.
                Now I understand the code you guys gave is for a stand alone program to be ran in terminal.
                The code commands the terminal to colorize what words you tell it to.

                Below is the file from my friends program "MasterServer" , named "logevent.cpp"

                #include "logger.h"
                
                void Logger::logEvent(const QString &messageType,
                                      const QString &message)
                {
                    // printing to display suppressed?
                    if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                    {
                        QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                        logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl;
                    }
                
                    // printing to logfile suppressed?
                    if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                    {
                        // write message to log
                        QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                        writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) );
                    }
                }
                

                I believe this is the file that can be used to insert the ansi escape sequence code in. That has the correct syntax and will compile and be read by my friends program telling the terminal what color to color certain words if they appear.

                My bad example!

                
                void Logger::logEvent(const QString &messageType,
                                      const QString &message)
                *********************************************************************
                "INSERT , BUT WITH ALL THE CORRECT CODE /SYNTAX!
                if (type.contains(QRegularExpression("(fatal|fail|error|stop)")))
                        type = QString("\e[1m\e[91m%1\e[0m").arg(type);
                    if (type.contains(QRegularExpression("(refused|nodevice|timeout)")))
                        type = QString("\e[91m%1\e[0m").arg(type);
                
                @JonB , I am using your rewrite code for this bad example. I must admit when I saw your code and it had "type" in it I knew this file had type in it so I was hoping with a little work (rewrite) it would work, but no  luck. 
                Now if anyone can see a better place to insert or the correct way of doing this please by all means have at it!
                If this cannot be done then It can't be done.
                
                *********************************************************************
                {
                    // printing to display suppressed?
                    if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                    {
                        QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                        logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl;
                    }
                
                    // printing to logfile suppressed?
                    if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                    {
                        // write message to log
                        QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                        writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) );
                    }
                }
                

                Again I want to thank all who replied and tried to help this old man.
                I've learned a lot, I just hope most of you still have most of your hair left!
                For me, it's been fun!
                Thanks!

                Cougar

                J 1 Reply Last reply 27 Sept 2021, 10:12
                0
                • C Cougar 0
                  27 Sept 2021, 09:55

                  alt text
                  Thanks @JonB, @SGaist , you guys helped me learn a lot!
                  Which brings me to this.
                  Now I understand the code you guys gave is for a stand alone program to be ran in terminal.
                  The code commands the terminal to colorize what words you tell it to.

                  Below is the file from my friends program "MasterServer" , named "logevent.cpp"

                  #include "logger.h"
                  
                  void Logger::logEvent(const QString &messageType,
                                        const QString &message)
                  {
                      // printing to display suppressed?
                      if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                      {
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl;
                      }
                  
                      // printing to logfile suppressed?
                      if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                      {
                          // write message to log
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) );
                      }
                  }
                  

                  I believe this is the file that can be used to insert the ansi escape sequence code in. That has the correct syntax and will compile and be read by my friends program telling the terminal what color to color certain words if they appear.

                  My bad example!

                  
                  void Logger::logEvent(const QString &messageType,
                                        const QString &message)
                  *********************************************************************
                  "INSERT , BUT WITH ALL THE CORRECT CODE /SYNTAX!
                  if (type.contains(QRegularExpression("(fatal|fail|error|stop)")))
                          type = QString("\e[1m\e[91m%1\e[0m").arg(type);
                      if (type.contains(QRegularExpression("(refused|nodevice|timeout)")))
                          type = QString("\e[91m%1\e[0m").arg(type);
                  
                  @JonB , I am using your rewrite code for this bad example. I must admit when I saw your code and it had "type" in it I knew this file had type in it so I was hoping with a little work (rewrite) it would work, but no  luck. 
                  Now if anyone can see a better place to insert or the correct way of doing this please by all means have at it!
                  If this cannot be done then It can't be done.
                  
                  *********************************************************************
                  {
                      // printing to display suppressed?
                      if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                      {
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl;
                      }
                  
                      // printing to logfile suppressed?
                      if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                      {
                          // write message to log
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) );
                      }
                  }
                  

                  Again I want to thank all who replied and tried to help this old man.
                  I've learned a lot, I just hope most of you still have most of your hair left!
                  For me, it's been fun!
                  Thanks!

                  J Offline
                  J Offline
                  JonB
                  wrote on 27 Sept 2021, 10:12 last edited by JonB
                  #57

                  @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                  @JonB , I am using your rewrite code for this bad example. I must admit when I saw your code and it had "type" in it I knew this file had type in it so I was hoping with a little work (rewrite) it would work, but no luck.
                  Now if anyone can see a better place to insert or the correct way of doing this please by all means have at it!
                  If this cannot be done then It can't be done.

                  As I said, I translated the Perl code as-is to do exactly the same.

                  Here I am guessing the const QString &message parameter to logEvent is the string you want to display/save/pass on with colorizing escape sequences, instead of the original $type Perl variable or the type in the C++. Unlike that example, here we cannot alter the const QString &message input parameter because of the const. So we shall need a new variable to put the escape sequences in. Just change my code to something like:

                  void Logger::logEvent(const QString &messageType,
                                        const QString &message)
                  {
                      QString colorizedMessage(message);
                  
                      if (colorizedMessage.contains(QRegularExpression("(fatal|fail|error|stop)")))
                          colorizedMessage = QString("\e[1m\e[91m%1\e[0m").arg(colorizedMessage);
                      if (colorizedMessage.contains(QRegularExpression("(refused|nodevice|timeout)")))
                          colorizedMessage = QString("\e[91m%1\e[0m").arg(colorizedMessage);
                      ...
                      // Now `colorizedMessage` holds the original `message` with any escape sequences
                      // so you can do with it as you will, e.g.
                      // printing to display suppressed?
                      if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                      {
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) << endl;
                      }
                  
                      // printing to logfile suppressed?
                      if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                      {
                          // write message to log
                          QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                      }
                  }
                  
                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Cougar 0
                    wrote on 27 Sept 2021, 10:44 last edited by
                    #58

                    @JonB , used your updated file and it started comping beautifully until,

                    Logger/logevent.cpp:1:6: error: ‘Logger’ has not been declared
                        1 | void Logger::logEvent(const QString &messageType,
                          |      ^~~~~~
                    Logger/logevent.cpp:1:29: error: ‘QString’ does not name a type
                        1 | void Logger::logEvent(const QString &messageType,
                          |                             ^~~~~~~
                    Logger/logevent.cpp:2:29: error: ‘QString’ does not name a type
                        2 |                       const QString &message)
                          |                             ^~~~~~~
                    Logger/logevent.cpp: In function ‘void logEvent(const int&, const int&)’:
                    Logger/logevent.cpp:4:5: error: ‘QString’ was not declared in this scope
                        4 |     QString colorizedMessage(message);
                          |     ^~~~~~~
                    Logger/logevent.cpp:6:9: error: ‘colorizedMessage’ was not declared in this scope
                        6 |     if (colorizedMessage.contains(QRegularExpression("update")))
                          |         ^~~~~~~~~~~~~~~~
                    Logger/logevent.cpp:6:35: error: ‘QRegularExpression’ was not declared in this scope
                        6 |     if (colorizedMessage.contains(QRegularExpression("update")))
                          |                                   ^~~~~~~~~~~~~~~~~~
                    Logger/logevent.cpp:8:9: error: ‘colorizedMessage’ was not declared in this scope
                        8 |     if (colorizedMessage.contains(QRegularExpression("check")))
                          |         ^~~~~~~~~~~~~~~~
                    Logger/logevent.cpp:8:35: error: ‘QRegularExpression’ was not declared in this scope
                        8 |     if (colorizedMessage.contains(QRegularExpression("check")))
                          |                                   ^~~~~~~~~~~~~~~~~~
                    Logger/logevent.cpp:10:5: error: expected primary-expression before ‘...’ token
                       10 |     ...
                          |     ^~~
                    Logger/logevent.cpp:21:12: error: ‘_suppressLog’ was not declared in this scope
                       21 |     if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                          |            ^~~~~~~~~~~~
                    Logger/logevent.cpp:24:16: error: expected ‘;’ before ‘dateTimeStr’
                       24 |         QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                          |                ^~~~~~~~~~~~
                          |                ;
                    Logger/logevent.cpp:25:23: error: ‘QStringLiteral’ was not declared in this scope
                       25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                          |                       ^~~~~~~~~~~~~~
                    Logger/logevent.cpp:25:58: error: ‘dateTimeStr’ was not declared in this scope
                       25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                          |                                                          ^~~~~~~~~~~
                    Logger/logevent.cpp:25:84: error: ‘colorizedMessage’ was not declared in this scope
                       25 | QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                          |                                                              ^~~~~~~~~~~~~~~~
                    
                    Logger/logevent.cpp:25:9: error: ‘writeLogFile’ was not declared in this scope
                       25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                          |         ^~~~~~~~~~~~
                    Logger/logevent.cpp:2:38: warning: unused parameter ‘message’ [-Wunused-parameter]
                        2 |                       const QString &message)
                          |                       ~~~~~~~~~~~~~~~^~~~~~~
                    Logger/logevent.cpp: At global scope:
                    Logger/logevent.cpp:29:1: error: expected unqualified-id before numeric constant
                       29 | 0
                          | ^
                    make: *** [Makefile:819: logevent.o] Error 1
                    cougarxr7@GoneSpy:~/Programs/TestingQt5/MasterServer-Qt5-master/src$ 
                    

                    I am beginning to believe we can't get there from here....
                    Tell me what I did wrong, please.

                    Cougar

                    J 1 Reply Last reply 27 Sept 2021, 11:18
                    0
                    • C Cougar 0
                      27 Sept 2021, 10:44

                      @JonB , used your updated file and it started comping beautifully until,

                      Logger/logevent.cpp:1:6: error: ‘Logger’ has not been declared
                          1 | void Logger::logEvent(const QString &messageType,
                            |      ^~~~~~
                      Logger/logevent.cpp:1:29: error: ‘QString’ does not name a type
                          1 | void Logger::logEvent(const QString &messageType,
                            |                             ^~~~~~~
                      Logger/logevent.cpp:2:29: error: ‘QString’ does not name a type
                          2 |                       const QString &message)
                            |                             ^~~~~~~
                      Logger/logevent.cpp: In function ‘void logEvent(const int&, const int&)’:
                      Logger/logevent.cpp:4:5: error: ‘QString’ was not declared in this scope
                          4 |     QString colorizedMessage(message);
                            |     ^~~~~~~
                      Logger/logevent.cpp:6:9: error: ‘colorizedMessage’ was not declared in this scope
                          6 |     if (colorizedMessage.contains(QRegularExpression("update")))
                            |         ^~~~~~~~~~~~~~~~
                      Logger/logevent.cpp:6:35: error: ‘QRegularExpression’ was not declared in this scope
                          6 |     if (colorizedMessage.contains(QRegularExpression("update")))
                            |                                   ^~~~~~~~~~~~~~~~~~
                      Logger/logevent.cpp:8:9: error: ‘colorizedMessage’ was not declared in this scope
                          8 |     if (colorizedMessage.contains(QRegularExpression("check")))
                            |         ^~~~~~~~~~~~~~~~
                      Logger/logevent.cpp:8:35: error: ‘QRegularExpression’ was not declared in this scope
                          8 |     if (colorizedMessage.contains(QRegularExpression("check")))
                            |                                   ^~~~~~~~~~~~~~~~~~
                      Logger/logevent.cpp:10:5: error: expected primary-expression before ‘...’ token
                         10 |     ...
                            |     ^~~
                      Logger/logevent.cpp:21:12: error: ‘_suppressLog’ was not declared in this scope
                         21 |     if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                            |            ^~~~~~~~~~~~
                      Logger/logevent.cpp:24:16: error: expected ‘;’ before ‘dateTimeStr’
                         24 |         QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                            |                ^~~~~~~~~~~~
                            |                ;
                      Logger/logevent.cpp:25:23: error: ‘QStringLiteral’ was not declared in this scope
                         25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                            |                       ^~~~~~~~~~~~~~
                      Logger/logevent.cpp:25:58: error: ‘dateTimeStr’ was not declared in this scope
                         25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                            |                                                          ^~~~~~~~~~~
                      Logger/logevent.cpp:25:84: error: ‘colorizedMessage’ was not declared in this scope
                         25 | QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                            |                                                              ^~~~~~~~~~~~~~~~
                      
                      Logger/logevent.cpp:25:9: error: ‘writeLogFile’ was not declared in this scope
                         25 |         writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                            |         ^~~~~~~~~~~~
                      Logger/logevent.cpp:2:38: warning: unused parameter ‘message’ [-Wunused-parameter]
                          2 |                       const QString &message)
                            |                       ~~~~~~~~~~~~~~~^~~~~~~
                      Logger/logevent.cpp: At global scope:
                      Logger/logevent.cpp:29:1: error: expected unqualified-id before numeric constant
                         29 | 0
                            | ^
                      make: *** [Makefile:819: logevent.o] Error 1
                      cougarxr7@GoneSpy:~/Programs/TestingQt5/MasterServer-Qt5-master/src$ 
                      

                      I am beginning to believe we can't get there from here....
                      Tell me what I did wrong, please.

                      J Offline
                      J Offline
                      JonB
                      wrote on 27 Sept 2021, 11:18 last edited by JonB
                      #59

                      @Cougar-0
                      You were just supposed to replace your existing Logger::logEvent in:

                      #include "logger.h"
                      
                      void Logger::logEvent(const QString &messageType,
                                            const QString &message)
                      {
                      

                      You have either put this somewhere else, or made some mistake earlier in the file, or something unknown. Nothing in the code I gave you would cause these errors by itself.

                      Logger/logevent.cpp:1:6: error: ‘Logger’ has not been declared

                      I believe the 1:6 indicates this is occurring on line #1 of the file. As per the code above which you already had, there will need to be some kind of #include "logger.h" before this line, maybe you have removed that.....

                      1 Reply Last reply
                      1
                      • C Offline
                        C Offline
                        Cougar 0
                        wrote on 27 Sept 2021, 12:06 last edited by Cougar 0
                        #60

                        @JonB , Success! We have color text!
                        I now have code to work with and tweek!
                        Thank you so very much! I learned a lot!!

                        colortext.png

                        Here is the code;

                        #include "logger.h"
                        
                        void Logger::logEvent(const QString &messageType,
                                              const QString &message)
                        {
                            QString colorizedMessage(message);
                             
                            if (colorizedMessage.contains(QRegularExpression("update"))) {
                                colorizedMessage = QString("\e[1m\e[95m%1\e[0m").arg(colorizedMessage);
                            }
                            
                                    if (colorizedMessage.contains(QRegularExpression("check"))) {
                                colorizedMessage = QString("\e[97m%1\e[0m").arg(colorizedMessage);
                            }
                            // Now `colorizedMessage` holds the original `message` with any escape sequences
                            // so you can do with it as you will, e.g.
                            
                            // printing to display suppressed?
                            if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                            {
                                QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                                logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) << endl;
                            }
                        
                            // printing to logfile suppressed?
                            if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                            {
                                // write message to log
                                QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                                writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                            }
                        }
                        

                        I noticed it only colored update and not the word check.
                        Not only update but the whole line is colored!
                        Now that means I have tweeking to do!

                        Cougar

                        J 1 Reply Last reply 27 Sept 2021, 12:15
                        0
                        • C Cougar 0
                          27 Sept 2021, 12:06

                          @JonB , Success! We have color text!
                          I now have code to work with and tweek!
                          Thank you so very much! I learned a lot!!

                          colortext.png

                          Here is the code;

                          #include "logger.h"
                          
                          void Logger::logEvent(const QString &messageType,
                                                const QString &message)
                          {
                              QString colorizedMessage(message);
                               
                              if (colorizedMessage.contains(QRegularExpression("update"))) {
                                  colorizedMessage = QString("\e[1m\e[95m%1\e[0m").arg(colorizedMessage);
                              }
                              
                                      if (colorizedMessage.contains(QRegularExpression("check"))) {
                                  colorizedMessage = QString("\e[97m%1\e[0m").arg(colorizedMessage);
                              }
                              // Now `colorizedMessage` holds the original `message` with any escape sequences
                              // so you can do with it as you will, e.g.
                              
                              // printing to display suppressed?
                              if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
                              {
                                  QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                                  logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) << endl;
                              }
                          
                              // printing to logfile suppressed?
                              if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
                              {
                                  // write message to log
                                  QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
                                  writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
                              }
                          }
                          

                          I noticed it only colored update and not the word check.
                          Not only update but the whole line is colored!
                          Now that means I have tweeking to do!

                          J Offline
                          J Offline
                          JonB
                          wrote on 27 Sept 2021, 12:15 last edited by JonB
                          #61

                          @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                          I noticed it only colored update and not the word check.

                          The word check comes in your messageType parameter, not in the message parameter which is where the update ... string is. Hence is does not get colorized.

                          You would either have to:

                          • Subject the messageType parameter to the same escape sequence injection as you do on the message parameter. That is where factoring the colorizing code into its own function with a parameter would be a very good idea, as shown by @SGaist is his very first response.

                          • First create the whole string to be output (QString fullMessage = QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message)) and then subject that fullMessage to the colorization code before outputting it instead of just the message.

                          But I said a long time ago I am not volunteering to write all the code... :)

                          1 Reply Last reply
                          3
                          • C Offline
                            C Offline
                            Cougar 0
                            wrote on 27 Sept 2021, 12:50 last edited by
                            #62

                            I didn't expect you or anyone to write all the code for this, especially for nothing, that is why I offered to pay whoever could help me get this to work.
                            I don't have the 5 or 6 years left it would take me to learn how to do all this!
                            I will take it from here....
                            Thanks for everything!

                            Cougar

                            1 Reply Last reply
                            0

                            59/62

                            27 Sept 2021, 11:18

                            • Login

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