log.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // ***********************************************************/
  2. // log.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // Log handling module.
  7. //
  8. // QT bug tracking:
  9. // https://bugreports.qt.io/secure/Dashboard.jspa
  10. // ***********************************************************/
  11. #include "log.h"
  12. #include "qtextcodec.h"
  13. #include <Windows.h>
  14. #define BUFF_LEN (1024)
  15. #ifdef UNICODE
  16. void Output(LPCWSTR szFormat, ...)
  17. #else
  18. void Output(const char* szFormat, ...)
  19. #endif
  20. {
  21. #ifdef UNICODE
  22. WCHAR szBuff[BUFF_LEN + 1];
  23. va_list arg;
  24. va_start(arg, szFormat);
  25. _vsnwprintf_s(szBuff, _countof(szBuff), _TRUNCATE, szFormat, arg);
  26. //_vsnwprintf(szBuff, BUFF_LEN, szFormat, arg);
  27. va_end(arg);
  28. #else
  29. char szBuff[BUFF_LEN];
  30. va_list arg;
  31. va_start(arg, szFormat);
  32. _vsnprintf(szBuff, BUFF_LEN, szFormat, arg);
  33. va_end(arg);
  34. #endif
  35. OutputDebugString(szBuff);
  36. }
  37. void logOutput(const QtMsgType type, const QMessageLogContext& context, const QString& msg)
  38. {
  39. QString txt, type_str;
  40. QString time = QDateTime::currentDateTime().toString("dd/MM/yy hh:mm:ss.zzz"); //"hh:mm:ss.zzz"
  41. QFileInfo file(context.file);
  42. switch (type) {
  43. case QtDebugMsg:
  44. type_str = "Debug";
  45. break;
  46. case QtInfoMsg:
  47. type_str = "Info";
  48. break;
  49. case QtWarningMsg:
  50. type_str = "Warning";
  51. break;
  52. case QtCriticalMsg:
  53. type_str = "Critical";
  54. break;
  55. case QtFatalMsg:
  56. type_str = "Fatal";
  57. abort();
  58. default:
  59. break;
  60. }
  61. if (msg.startsWith("QObject::startTimer:") || msg.startsWith("QObject::killTimer:"))
  62. return;
  63. #if !NDEBUG // log to debug window with debug version
  64. txt = QString("[%1][%2]%3 (file:%4:%5, fun:%6)\n")
  65. .arg(time)
  66. .arg(type_str)
  67. .arg(msg)
  68. .arg(file.fileName())
  69. .arg(context.line)
  70. .arg(context.function);
  71. Output(txt.toStdWString().c_str());
  72. #else // log to file
  73. if (type <= QtDebugMsg)
  74. return;
  75. txt = QString("[%1][%2]%3").arg(time).arg(type_str).arg(msg);
  76. Logger& logger = Logger::instance();
  77. logger.log(txt);
  78. #endif
  79. }
  80. Logger::Logger(const QString& file)
  81. : m_logfile(nullptr)
  82. , m_ts(nullptr)
  83. {
  84. m_logfile = std::make_unique<QFile>(file);
  85. if (m_logfile) {
  86. if (m_logfile->open(QIODevice::WriteOnly | QIODevice::Append)) {
  87. m_ts = std::make_unique<QTextStream>(m_logfile.get());
  88. m_ts->setCodec(QTextCodec::codecForName("UTF8"));
  89. }
  90. }
  91. }
  92. Logger::~Logger()
  93. {
  94. if (m_logfile)
  95. m_logfile->close();
  96. }
  97. void Logger::log(const QString& str)
  98. {
  99. if (m_ts)
  100. (*m_ts) << str << Qt::endl;
  101. }