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. Message handler, fflush(stderr) doesn't work
Qt 6.11 is out! See what's new in the release blog

Message handler, fflush(stderr) doesn't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 2 Posters 3.1k 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.
  • SPlattenS SPlatten

    I have a message handler:

    void qDebugMsgHandler(QtMsgType type, const QMessageLogContext& context, const QString& strMsg) {
        QString strOutput;
    
        if ( context.file ) {
            strOutput += QString(context.file);
    
            if ( context.function ) {
                if ( context.line > 0 ) {
                    strOutput += QString(" L%1").arg(context.line, 8, 10, QChar('0')) + QString(":");
                }
                strOutput += QString(context.function);
            }
        }
        if ( strMsg.length() > 0 ) {
            if ( strOutput.length() > 0 ) {
                strOutput += ": ";
            }
            strOutput += strMsg;
        }
        switch( type ) {
        case QtDebugMsg:
            fprintf(stderr, "   Debug:%s\n", strOutput.toLatin1().data());
            break;
        case QtInfoMsg:
            fprintf(stderr, "    Info:%s\n", strOutput.toLatin1().data());
            break;
        case QtWarningMsg:
            fprintf(stderr, " Warning:%s\n", strOutput.toLatin1().data());
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical:%s\n", strOutput.toLatin1().data());
            break;
        case QtFatalMsg:
            fprintf(stderr, "   Fatal:%s\n", strOutput.toLatin1().data());
            break;
        }
        fflush(stderr);
    }
    

    This is installed when my application starts:

    int main(int argc, char *argv[]) {
        if ( argc < 2 ) {
            error(static_cast<const char*>("Please specify configuration file!"));
       }
        QString strConfig = QString(argv[1]);
        //Install custom message handler
        qInstallMessageHandler(qDebugMsgHandler);
    

    Although every call to this handler ends with a flush(stderr), there are times when I know calls to qDebug has been called but I do not see anything in the Application Output, sometimes for a very long time.

    It seems that the call to fflush isn't enough ?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    Could you try with
    setbuf(stderr, nullptr)
    in main.cpp before anything else and see if that makes a difference?

    SPlattenS 1 Reply Last reply
    0
    • mrjjM mrjj

      Hi
      Could you try with
      setbuf(stderr, nullptr)
      in main.cpp before anything else and see if that makes a difference?

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #3

      @mrjj , thanks, just tried, no difference.

      mrjjM 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @mrjj , thanks, just tried, no difference.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @SPlatten
        Ok. normally its due to std etc is being buffered but
        seems not to be the case here then.
        and you did set a break point to verify it is indeed called ?

        1 Reply Last reply
        0
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #5

          @mrjj, sorry, my fault, it won't do anything because although the output appears eventually in the Application Output, the function that is called to do the output is not qDebug(). The output is produced in Script using "console.info", which although it appears in the Application Output obviously doesn't go via the same handlers that the qDebug() does.

          mrjjM 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @mrjj, sorry, my fault, it won't do anything because although the output appears eventually in the Application Output, the function that is called to do the output is not qDebug(). The output is produced in Script using "console.info", which although it appears in the Application Output obviously doesn't go via the same handlers that the qDebug() does.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @SPlatten
            ahh that way :)
            Nope i would assume its something else since its on JS side.

            SPlattenS 1 Reply Last reply
            0
            • mrjjM mrjj

              @SPlatten
              ahh that way :)
              Nope i would assume its something else since its on JS side.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #7

              @mrjj One possible solution could be to redirect stdout to stderr ?

              mrjjM 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @mrjj One possible solution could be to redirect stdout to stderr ?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @SPlatten
                Im not sure what is being used internally.
                https://doc.qt.io/qt-5/qtquick-debugging.html
                sounds like it is using qDebug. internally.
                but is this console.log in QML or pure JS ?

                SPlattenS 1 Reply Last reply
                0
                • mrjjM mrjj

                  @SPlatten
                  Im not sure what is being used internally.
                  https://doc.qt.io/qt-5/qtquick-debugging.html
                  sounds like it is using qDebug. internally.
                  but is this console.log in QML or pure JS ?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #9

                  @mrjj, console.info

                  mrjjM 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @mrjj, console.info

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @SPlatten
                    So yes to QML?

                    SPlattenS 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @SPlatten
                      So yes to QML?

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #11

                      @mrjj , sorry, this application is only JS.

                      mrjjM 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @mrjj , sorry, this application is only JS.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @SPlatten
                        Ok then i understand it.
                        Im not sure how to flush its buffers then.
                        JS dont have a flush on its own or anything?

                        SPlattenS 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @SPlatten
                          Ok then i understand it.
                          Im not sure how to flush its buffers then.
                          JS dont have a flush on its own or anything?

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by
                          #13

                          @mrjj , found this, https://stackoverflow.com/questions/28412477/how-to-flush-console-log-output-on-windows

                          Couldn't I just flush stdout too?

                          mrjjM 1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @mrjj , found this, https://stackoverflow.com/questions/28412477/how-to-flush-console-log-output-on-windows

                            Couldn't I just flush stdout too?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @SPlatten
                            Good found.
                            Yes seem so.
                            I would try it at once as no harm from it.

                            SPlattenS 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @SPlatten
                              Good found.
                              Yes seem so.
                              I would try it at once as no harm from it.

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #15

                              @mrjj , in the end I wrote a routine in the C++ that is invokable in the JavaScript, then just replaced all calls to console.info with my own routine, this works well.

                              1 Reply Last reply
                              1
                              • SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #16

                                @mrjj , ok, now I'm really confused, after adding my API routine, I can now see using the debugger than messages from JavaScript via the API call and C++ make it into the message handler, I' ve stepped through and the output string contains correct content, but for some reason I do not see the output from the JavaScript API call in the Application Output.

                                SPlattenS 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @mrjj , ok, now I'm really confused, after adding my API routine, I can now see using the debugger than messages from JavaScript via the API call and C++ make it into the message handler, I' ve stepped through and the output string contains correct content, but for some reason I do not see the output from the JavaScript API call in the Application Output.

                                  SPlattenS Offline
                                  SPlattenS Offline
                                  SPlatten
                                  wrote on last edited by
                                  #17

                                  @SPlatten, In the message handler, I'm going to push every entry onto a stack then have a separate thread that services the stack popping off the first from the stack.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @SPlatten, In the message handler, I'm going to push every entry onto a stack then have a separate thread that services the stack popping off the first from the stack.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @SPlatten
                                    Ok, im not sure what causes this but its worth a shot.

                                    1 Reply Last reply
                                    0
                                    • SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #19
                                      This post is deleted!
                                      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