×
Namespaces

Variants
Actions

How to redirect qDebug output to a file

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: customMessageHandler,qInstallMsgHandler
Created: vasant21 (30 Aug 2010)
Last edited: hamishwillee (11 Oct 2012)

In Qt the default message handler function prints debug messages, warnings, critical and fatal error messages to standard output or to the debugger. If you wish to get more control over message handling you need to implement your own message handler and register your message handler by calling qInstallMsgHandler(yourHandlerFunction).


#include <QtDebug>
#include <QFile>
#include <QTextStream>
 
void customMessageHandler(QtMsgType type, const char *msg)
{
QString txt;
switch (type) {
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;
 
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
break;
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
break;
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
abort();
}
 
QFile outFile("debuglog.txt");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt << endl;
}
 
int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
 
//Lets register our custom handler, before we start
qInstallMsgHandler(customMessageHandler);
...
return app.exec();
}


  • Now output of a call to qDebug() redirects to debug-log file specified in the customMessageHandler.
This page was last modified on 11 October 2012, at 04:17.
257 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×