I'm using a lot of qDebug() << statements for debug output. Is there any cross-platform way I can redirect that debug output to a file, without resorting to shell scripts? I'm guessing that open() and dup2() will do the job in Linux, but will it work compiled with MinGW in Windows?
And maybe there is a Qt way to do it?
You've to install a message handler using
qInstallMessageHandlerfunction, and then, you can useQTextStreamto write the debug message to a file. Here is a sample example:Taken from the doc of
qInstallMessageHandler(I only added the comments):In the above example, the function
myMessageOutputusesstderrwhich you might want to replace with some other file stream, or completely re-write the function!Once you write and install this function, all your
qDebug(as well asqWarning,qCriticaletc) messages would be redirected to the file you're writing to in the handler.