Implement Qt log output to files

Source: Internet
Author: User
  1. # Include <qtdebug>
  2. # Include <qfile>
  3. # Include <qtextstream
    >
  4. Void custommessagehandler (qtmsgtype type, const char * MSG)
  5. {
  6. Qstring
    Txt;
  7. Switch (type)
  8. {
  9. Case qtdebugmsg: // debugging information prompt
  10. TXT = qstring ("Debug: % 1"). Arg (MSG );
  11. Break;
  12. Case qtwarningmsg: // general warning prompt
  13. TXT = qstring ("Warning: % 1"). Arg (MSG );
  14. Break;
  15. Case QtCriticalMsg: // severe error message
  16. Txt = QString ("Critical: % 1"). arg (msg );
  17. Break;
  18. Case QtFatalMsg: // fatal error prompt
  19. Txt = QString ("Fatal: % 1"). arg (msg );
  20. Abort ();
  21. Default:
  22. Break;
  23. }
  24. QFile outFile ("Log.txt ");
  25. OutFile. open (QIODevice: WriteOnly | QIODevice: Append );
  26. QTextStream ts (& outFile );
  27. Ts <txt <endl;
  28. }
  29. Int main (int argc, char * argv [])
  30. {
  31. QApplication app (argc, argv );
  32. // Register your MsgHandler first
  33. QInstallMsgHandler (customMessageHandler );
  34. // Logs can be directly written into the file as follows, and the log also contains time information.
  35. QDebug ("This is a debug message at thisisqt.com ");
  36. QWarning ("This is a warning message at thisisqt.com ");
  37. QCritical ("This is a critical message at thisisqt.com ");
  38. QFatal ("This is a fatal message at thisisqt.com ");
  39. Return app.exe c ();
  40. }

Above cited from http://www.thisisqt.com/forum/viewthread.php? Tid = 90

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.