Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to redirect qDebug output to a file

From 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 01:17.
227 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.

×