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. QtMessageHandler context contains no information
Qt 6.11 is out! See what's new in the release blog

QtMessageHandler context contains no information

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.4k Views 2 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.
  • T Offline
    T Offline
    tmag
    wrote on last edited by
    #1

    We have a QtMessageHandler registered with qInstallMessageHandler and when messages are logged the context.file and context.function do not seem to be defined.

    void handle_message(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
      QByteArray localMsg = msg.toLocal8Bit();
      const char *file = context.file ? context.file : "???";
      const char *func = context.function ? context.function : "???";
    
      switch (type) {
        case QtDebugMsg:
          fprintf(stderr, "[DEBUG %s:%u#%s] %s\n", file, context.line, func, localMsg.constData());
          break;
      }
      // ...
    }
    

    This always produces ??? in the place of the function and file name when called with qDebug() << "Message";

    Would anyone know why this might be the case? Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • T tmag

      We have a QtMessageHandler registered with qInstallMessageHandler and when messages are logged the context.file and context.function do not seem to be defined.

      void handle_message(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
        QByteArray localMsg = msg.toLocal8Bit();
        const char *file = context.file ? context.file : "???";
        const char *func = context.function ? context.function : "???";
      
        switch (type) {
          case QtDebugMsg:
            fprintf(stderr, "[DEBUG %s:%u#%s] %s\n", file, context.line, func, localMsg.constData());
            break;
        }
        // ...
      }
      

      This always produces ??? in the place of the function and file name when called with qDebug() << "Message";

      Would anyone know why this might be the case? Thanks in advance!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @tmag
      I would guess qDebug() does not say anywhere it will actually pass a file/function name in QMessageLogContext from wherever you call it, does it? (Unlike, say, Q_ASSERT() which I know prints them, whether it goes via qInstallMessageHandler.) You would have to pass in from caller __FILE__, __LINE__, __WHATEVER_FOR_FILENAME somehow to achieve this, probably not via qDebug()?

      This is only a guess. But put a breakpoint on handle_message(), look back up the stack view to where qDebug() is calling it, and see what parameters are being passed/how the QMessageLogContext context is set up.

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @tmag
        I would guess qDebug() does not say anywhere it will actually pass a file/function name in QMessageLogContext from wherever you call it, does it? (Unlike, say, Q_ASSERT() which I know prints them, whether it goes via qInstallMessageHandler.) You would have to pass in from caller __FILE__, __LINE__, __WHATEVER_FOR_FILENAME somehow to achieve this, probably not via qDebug()?

        This is only a guess. But put a breakpoint on handle_message(), look back up the stack view to where qDebug() is calling it, and see what parameters are being passed/how the QMessageLogContext context is set up.

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @JonB

        void DocCenterApp::initialize()
        {
            qDebug()<<"Sorry @jonB you're wrong";
        ....
        }
        

        11-06-2022 15:51:50.236 [eDocCenter2 from initialize() in DocCenterApp.cpp line 59] Sorry @jonB you're wrong

        JonBJ 1 Reply Last reply
        1
        • M mpergand

          @JonB

          void DocCenterApp::initialize()
          {
              qDebug()<<"Sorry @jonB you're wrong";
          ....
          }
          

          11-06-2022 15:51:50.236 [eDocCenter2 from initialize() in DocCenterApp.cpp line 59] Sorry @jonB you're wrong

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @mpergand
          Err, OK, thanks!
          The OP should still put a breakpoint on his handler and have a look up the stack/look in the context variable.

          T 1 Reply Last reply
          0
          • kkoehneK Offline
            kkoehneK Offline
            kkoehne
            Moderators
            wrote on last edited by kkoehne
            #5

            @tmag said in QtMessageHandler context contains no information:

            Would anyone know why this might be the case? Thanks in advance!

            Your project most likely was compiled with either QT_NO_MESSAGELOGCONTEXT or QT_NO_DEBUG defines. If the latter, you can force message log contexts by defining QT_MESSAGELOGCONTEXT.

            Director R&D, The Qt Company

            T 1 Reply Last reply
            2
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              Also, to add to what @kkoehne says, perhaos you compiled in Release mode? Usually the QMessageLogContext stuff is available only in Debug builds.

              1 Reply Last reply
              1
              • JonBJ JonB

                @mpergand
                Err, OK, thanks!
                The OP should still put a breakpoint on his handler and have a look up the stack/look in the context variable.

                T Offline
                T Offline
                tmag
                wrote on last edited by
                #7

                @JonB
                Thanks for the response. Whenever the breakpoint was hit the context struct just had null values for the file and line number.

                1 Reply Last reply
                0
                • kkoehneK kkoehne

                  @tmag said in QtMessageHandler context contains no information:

                  Would anyone know why this might be the case? Thanks in advance!

                  Your project most likely was compiled with either QT_NO_MESSAGELOGCONTEXT or QT_NO_DEBUG defines. If the latter, you can force message log contexts by defining QT_MESSAGELOGCONTEXT.

                  T Offline
                  T Offline
                  tmag
                  wrote on last edited by
                  #8

                  @kkoehne
                  Brilliant thanks, this worked :)

                  1 Reply Last reply
                  0

                  • Login

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