- # Include <qtdebug>
- # Include <qfile>
- # Include <qtextstream
>
- Void custommessagehandler (qtmsgtype type, const char * MSG)
- {
- Qstring
Txt;
- Switch (type)
- {
- Case qtdebugmsg: // debugging information prompt
- TXT = qstring ("Debug: % 1"). Arg (MSG );
- Break;
- Case qtwarningmsg: // general warning prompt
- TXT = qstring ("Warning: % 1"). Arg (MSG );
- Break;
- Case QtCriticalMsg: // severe error message
- Txt = QString ("Critical: % 1"). arg (msg );
- Break;
- Case QtFatalMsg: // fatal error prompt
- Txt = QString ("Fatal: % 1"). arg (msg );
- Abort ();
- Default:
- Break;
- }
- QFile outFile ("Log.txt ");
- OutFile. open (QIODevice: WriteOnly | QIODevice: Append );
- QTextStream ts (& outFile );
- Ts <txt <endl;
- }
- Int main (int argc, char * argv [])
- {
- QApplication app (argc, argv );
- // Register your MsgHandler first
- QInstallMsgHandler (customMessageHandler );
- // Logs can be directly written into the file as follows, and the log also contains time information.
- QDebug ("This is a debug message at thisisqt.com ");
- QWarning ("This is a warning message at thisisqt.com ");
- QCritical ("This is a critical message at thisisqt.com ");
- QFatal ("This is a fatal message at thisisqt.com ");
- Return app.exe c ();
- }
Above cited from http://www.thisisqt.com/forum/viewthread.php? Tid = 90