Http://wan-2004.iteye.com/blog/888963
Qt hasQinstallmsghandlerThe method can be used to customize the callback function after a message is generated. The callback function also has the qdebug level information. In this way, we can easily customize error messages to our own log files.
As follows:
CPP Code
- # Include <qtdebug>
- # Include <qfile>
- # Include <qtextstream>
- // The callback function implements debugging information to the file.
- 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 ("Debug. log ");
- OUTFILE. Open (qiodevice: writeonly | qiodevice: append );
- Qtextstream TS (& OUTFILE );
- TS <TXT <Endl;
- }
- Int main (INT argc, char * argv [])
- {
- Qapplication app (argc, argv );
- // Register the callback function using this method
- Qinstallmsghandler (custommessagehandler );
- ...
- Return app.exe C ();
- }