Solved qDebug instant output?
-
It seems that when I send output to qDebug, eg:
qDebug() << __FILE__ << __LINE__ << strGetNodeName() << strGetAttribute(clsXMLnode::mscszAttrID);
When I single step this in the debugger, I do not see the output in the Application Output, sometimes not till quite a bit later.
Is there anything to explain this or is this the way its supposed to work? If it is designed behaviour then is there a way to change it to make it output instantly?
-
@SPlatten said in qDebug instant output?:
any guidance on how to achieve this?
So hard to google for 'qt messagehandler'?
https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler -
Since qDebug() is using stdio for it's output the os and std specific io buffering is taking effect. Don't know why it is that important for debug output though.
-
@Christian-Ehrlicher , its important for me as I want to see the debug output in order to debug the application, the fact that it doesn't appear when I want it to is a problem.
-
Then implement your own qt message handler and make sure your io is not buffered.
-
@Christian-Ehrlicher , thank you, any guidance on how to achieve this?
-
@SPlatten said in qDebug instant output?:
any guidance on how to achieve this?
So hard to google for 'qt messagehandler'?
https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler -
@Christian-Ehrlicher , Thank you, my brain is fried atm.
-
Hi, just wanted to add, if you're implementing your message handler exactly like that example in https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
you'll find it will work nicely for qDebug() outputs on Linux and MacOS.On Windows however it will still suffer from the same delay (buffering).
It's easy to fix though, just add a fflush(stderr) at the end, like this:... case QtFatalMsg: fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); break; } fflush(stderr); }
In my message handler I just leave it in also for MacOS and Linux (it's harmless anyway).
Edit: forgot to say: you only need that fflush(stderr); if you're on MinGW, if you're using MSVC 2019 you are more lucky, no fflush() needed (tested with Qt version 5.15.0)
-
@hskoglund said in qDebug instant output?:
no fflush() needed
I would doubt this. Only with fflush() you can be sure. Everything else does not flush the internal buffer which may work in 99% of all times.
-
Yeah, agree. Even though I tested on 5.15.0 and only the combination of Windows and MinGW exhibited that buffering delay, it's better to have it there.
Question is then, why does not the example at https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler contain a fflush()?
-
I did add a fflush to the function, just to be sure and it all seems to work fine.