You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "logger.h"
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <sys/time.h>
  5. namespace mtx {
  6. Logger & Logger::instance()
  7. {
  8. static Logger s_logger;
  9. return s_logger;
  10. }
  11. void Logger::setLevel(LogLevel level)
  12. {
  13. m_level = level;
  14. }
  15. void Logger::setDestinations(bool toStdout, const std::string & file)
  16. {
  17. std::lock_guard<std::mutex> lock(m_mutex);
  18. m_toStdout = toStdout;
  19. m_file = file;
  20. }
  21. void Logger::log(LogLevel level, const char * fmt, ...)
  22. {
  23. va_list ap;
  24. va_start(ap, fmt);
  25. vlog(level, fmt, ap);
  26. va_end(ap);
  27. }
  28. void Logger::vlog(LogLevel level, const char * fmt, va_list ap)
  29. {
  30. if (level < m_level)
  31. {
  32. return;
  33. }
  34. static const char * s_tags[] = {"DEB", "INF", "WAR", "ERR"};
  35. char tLine[2048];
  36. time_t tNow = time(nullptr);
  37. struct tm tTm;
  38. localtime_r(&tNow, &tTm);
  39. int tLen = (int) strftime(tLine, sizeof(tLine), "%Y/%m/%d %H:%M:%S ", &tTm);
  40. tLen += snprintf(tLine + tLen, sizeof(tLine) - tLen, "%s ", s_tags[(int) level]);
  41. if (tLen < (int) sizeof(tLine))
  42. {
  43. int tN = vsnprintf(tLine + tLen, sizeof(tLine) - tLen, fmt, ap);
  44. tLen = (tN < 0) ? tLen : tLen + tN;
  45. }
  46. if (tLen > (int) sizeof(tLine) - 2)
  47. {
  48. tLen = (int) sizeof(tLine) - 2;
  49. }
  50. tLine[tLen++] = '\n';
  51. tLine[tLen] = 0;
  52. std::lock_guard<std::mutex> lock(m_mutex);
  53. if (m_toStdout)
  54. {
  55. fwrite(tLine, 1, tLen, stdout);
  56. fflush(stdout);
  57. }
  58. if (! m_file.empty())
  59. {
  60. FILE * tF = fopen(m_file.c_str(), "a");
  61. if (tF != nullptr)
  62. {
  63. fwrite(tLine, 1, tLen, tF);
  64. fclose(tF);
  65. }
  66. }
  67. }
  68. void LogParent::log(LogLevel level, const char * fmt, ...) const
  69. {
  70. Logger & tL = Logger::instance();
  71. if (level < tL.level())
  72. {
  73. return;
  74. }
  75. char tMsg[1800];
  76. va_list ap;
  77. va_start(ap, fmt);
  78. vsnprintf(tMsg, sizeof(tMsg), fmt, ap);
  79. va_end(ap);
  80. tL.log(level, "%s%s", m_prefix.c_str(), tMsg);
  81. }
  82. LogLevel parseLogLevel(const std::string & s, bool * ok)
  83. {
  84. if (ok)
  85. {
  86. *ok = true;
  87. }
  88. if (s == "debug")
  89. {
  90. return LogLevel::Debug;
  91. }
  92. if (s == "info")
  93. {
  94. return LogLevel::Info;
  95. }
  96. if (s == "warn")
  97. {
  98. return LogLevel::Warn;
  99. }
  100. if (s == "error")
  101. {
  102. return LogLevel::Error;
  103. }
  104. if (ok)
  105. {
  106. *ok = false;
  107. }
  108. return LogLevel::Info;
  109. }
  110. } // namespace mtx