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. Logging messages: qlogging.h

Logging messages: qlogging.h

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 887 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 27 Oct 2020, 18:26 last edited by
    #2

    Hi,

    Do you have a custom message handler ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    S 1 Reply Last reply 27 Oct 2020, 18:36
    0
    • S SGaist
      27 Oct 2020, 18:26

      Hi,

      Do you have a custom message handler ?

      S Offline
      S Offline
      sitesv
      wrote on 27 Oct 2020, 18:36 last edited by
      #3

      @SGaist I don’t know.

      void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
      ...
      void main(...)
      {
      qInstallMessageHandler(messageHandler);
      }
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 27 Oct 2020, 18:40 last edited by
        #4

        Well, installing a custom message handler usually means to you are using a custom message handler.

        You have to handle the threading aspect in your custom handler.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 2 Replies Last reply 27 Oct 2020, 19:23
        1
        • S SGaist
          27 Oct 2020, 18:40

          Well, installing a custom message handler usually means to you are using a custom message handler.

          You have to handle the threading aspect in your custom handler.

          S Offline
          S Offline
          sitesv
          wrote on 27 Oct 2020, 19:23 last edited by
          #5

          @SGaist , so, This messageHandler handles various types of log messages. So, I think this is a custom messages handler. Tomorrow I will post a code of handler.

          S 1 Reply Last reply 27 Oct 2020, 19:57
          0
          • S SGaist
            27 Oct 2020, 18:40

            Well, installing a custom message handler usually means to you are using a custom message handler.

            You have to handle the threading aspect in your custom handler.

            S Offline
            S Offline
            sitesv
            wrote on 27 Oct 2020, 19:24 last edited by sitesv
            #6

            @SGaist said in Logging messages: qlogging.h:

            You have to handle the threading aspect in your custom handler

            What do you mean?

            1 Reply Last reply
            0
            • S sitesv
              27 Oct 2020, 19:23

              @SGaist , so, This messageHandler handles various types of log messages. So, I think this is a custom messages handler. Tomorrow I will post a code of handler.

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 27 Oct 2020, 19:57 last edited by
              #7

              @sitesv said in Logging messages: qlogging.h:

              @SGaist , so, This messageHandler handles various types of log messages. So, I think this is a custom messages handler. Tomorrow I will post a code of handler.

              Calling qInstallMessageHandler implies that you are using a custom message handler.

              @sitesv said in Logging messages: qlogging.h:

              @SGaist said in Logging messages: qlogging.h:

              You have to handle the threading aspect in your custom handler

              What do you mean?

              That you have to use proper threading primitives to protect the resources you access in your handler.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 28 Oct 2020, 08:53
              1
              • S SGaist
                27 Oct 2020, 19:57

                @sitesv said in Logging messages: qlogging.h:

                @SGaist , so, This messageHandler handles various types of log messages. So, I think this is a custom messages handler. Tomorrow I will post a code of handler.

                Calling qInstallMessageHandler implies that you are using a custom message handler.

                @sitesv said in Logging messages: qlogging.h:

                @SGaist said in Logging messages: qlogging.h:

                You have to handle the threading aspect in your custom handler

                What do you mean?

                That you have to use proper threading primitives to protect the resources you access in your handler.

                S Offline
                S Offline
                sitesv
                wrote on 28 Oct 2020, 08:53 last edited by
                #8

                @SGaist there is a handler code:

                // Handler
                void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                {
                    // Creating writing-stream
                    QTextStream out(m_logFile.data());
                    // Adding data/time
                    out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz ");
                    // Detecting message type
                    switch (type){
                        case QtInfoMsg:     out << "INF "; break;
                        case QtDebugMsg:    out << "DBG "; break;
                        case QtWarningMsg:  out << "WRN "; break;
                        case QtCriticalMsg: out << "CRT "; break;
                        case QtFatalMsg:    out << "FTL "; break;
                    }
                    // Adding message type and message
                    out << context.category << ": " << msg << endl;
                    out.flush();
                }
                

                There are no external resources... Only message as handler parameter.

                C 1 Reply Last reply 28 Oct 2020, 08:57
                0
                • S sitesv
                  28 Oct 2020, 08:53

                  @SGaist there is a handler code:

                  // Handler
                  void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                  {
                      // Creating writing-stream
                      QTextStream out(m_logFile.data());
                      // Adding data/time
                      out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz ");
                      // Detecting message type
                      switch (type){
                          case QtInfoMsg:     out << "INF "; break;
                          case QtDebugMsg:    out << "DBG "; break;
                          case QtWarningMsg:  out << "WRN "; break;
                          case QtCriticalMsg: out << "CRT "; break;
                          case QtFatalMsg:    out << "FTL "; break;
                      }
                      // Adding message type and message
                      out << context.category << ": " << msg << endl;
                      out.flush();
                  }
                  

                  There are no external resources... Only message as handler parameter.

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 28 Oct 2020, 08:57 last edited by
                  #9

                  @sitesv Please read the answer from @SGaist :

                  That you have to use proper threading primitives to protect the resources you access in your handler.

                  So make sure your message handle can not be called from two threads simultaneously with e.g. a mutex.

                  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
                  2
                  • J Online
                    J Online
                    JonB
                    wrote on 28 Oct 2020, 09:24 last edited by
                    #10

                    @Christian-Ehrlicher , @SGaist
                    I have a quick question here: does Qt have any restriction on which threads can execute a QFile::method(), compared to the thread where the QFile m_logFile was created? Like, I know you can only do QSql... operations in the thread which created the database connection, does that apply to QFile, or are you free to write to that from any thread??

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 28 Oct 2020, 09:29 last edited by
                      #11

                      No, as long as you add a proper mutex.

                      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
                      2

                      11/11

                      28 Oct 2020, 09:29

                      • Login

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