Qt customizes qdebug () Information to the log file

Source: Internet
Author: User

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
  1. # Include <qtdebug>
  2. # Include <qfile>
  3. # Include <qtextstream>
  4. // The callback function implements debugging information to the file.
  5. Void custommessagehandler (qtmsgtype type, const char * MSG)
  6. {
  7. Qstring txt;
  8. Switch (type ){
  9. Case qtdebugmsg:
  10. TXT = qstring ("Debug: % 1"). Arg (MSG );
  11. Break;
  12. Case qtwarningmsg:
  13. TXT = qstring ("Warning: % 1"). Arg (MSG );
  14. Break;
  15. Case qtcriticalmsg:
  16. TXT = qstring ("critical: % 1"). Arg (MSG );
  17. Break;
  18. Case qtfatalmsg:
  19. TXT = qstring ("Fatal: % 1"). Arg (MSG );
  20. Abort ();
  21. }
  22. Qfile OUTFILE ("Debug. log ");
  23. OUTFILE. Open (qiodevice: writeonly | qiodevice: append );
  24. Qtextstream TS (& OUTFILE );
  25. TS <TXT <Endl;
  26. }
  27. Int main (INT argc, char * argv [])
  28. {
  29. Qapplication app (argc, argv );
  30. // Register the callback function using this method
  31. Qinstallmsghandler (custommessagehandler );
  32. ...
  33. Return app.exe C ();
  34. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.