Namespaces
Variants
Actions

How to redirect qDebug output to a file

Jump to: navigation, search
Article Metadata

Compatibility
Platform(s): Qt

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.
429 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved