Message handler, fflush(stderr) doesn't work
-
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 ?
-
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 ?
Hi
Could you try with
setbuf(stderr, nullptr)
in main.cpp before anything else and see if that makes a difference? -
Hi
Could you try with
setbuf(stderr, nullptr)
in main.cpp before anything else and see if that makes a difference? -
@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.
-
@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.
-
@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 ? -
@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 ? -
@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? -
@mrjj , found this, https://stackoverflow.com/questions/28412477/how-to-flush-console-log-output-on-windows
Couldn't I just flush stdout too?
-
@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.
-
@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.
-
@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.