PDA

View Full Version : redirect qDebug() to file



sriky27
16th March 2009, 14:16
Hi,

How to redirect the qDebug() output to a file instead of application output

Regards,
Srikanth

lni
16th March 2009, 15:28
Simplest way without changing code:

your_excutable > your_file
or
your_excutable >& your_file

sriky27
16th March 2009, 16:40
Hi,

Thank you for your reply but I would be installing the application on the device, is there any other way please suggest

-Srikanth

^NyAw^
16th March 2009, 16:46
Hi,

Install a message handler using "qIntallMsgHandler" and there you can use "QTextStream" to write the message to a file.

Also, think that "qDebug" only will work when the application is compiled as debug.

spirit
16th March 2009, 16:47
try this


#include <QApplication>
#include <QtDebug>
#include <QFile>
#include <QTextStream>

void myMessageHandler(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("log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt << endl;
}

int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
qInstallMsgHandler(myMessageHandler);
...
return app.exec();
}

sriky27
17th March 2009, 07:38
Thanks a lot for your suggestions I appreciate it

fullmetalcoder
17th March 2009, 11:23
Also it is recommended to "handle" fatal messages (using abort() or exit(-1) or something akin) because that's what the default handler does and if you don't do it you're likely to end up with segfaults instead (though not necessarily at the same place so you may have a hard time tracking the bugs...).

spirit
17th March 2009, 11:37
Also it is recommended to "handle" fatal messages (using abort() or exit(-1) or something akin) because that's what the default handler does and if you don't do it you're likely to end up with segfaults instead (though not necessarily at the same place so you may have a hard time tracking the bugs...).



...
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
abort();
...

faldzip
17th March 2009, 18:13
Qt is using that handler in the function qt_message_output() and then it's checking if the message is QtFatalMsg and generating core dump or sth, so there is no need to implement it in addition to that. Whats more it's more complicated on Windows (with msvc) than just abort() in default implementation.

fullmetalcoder
17th March 2009, 20:21
...
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
abort();
...

Sorry missed that one line, I was in a bit of a rush when I read your post... :o