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.

134 lines
2.4 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::setSink(Sink sink)
  22. {
  23. std::lock_guard<std::mutex> lock(m_mutex);
  24. m_sink = std::move(sink);
  25. }
  26. void Logger::log(LogLevel level, const char * fmt, ...)
  27. {
  28. va_list ap;
  29. va_start(ap, fmt);
  30. vlog(level, fmt, ap);
  31. va_end(ap);
  32. }
  33. void Logger::vlog(LogLevel level, const char * fmt, va_list ap)
  34. {
  35. if (level < m_level)
  36. {
  37. return;
  38. }
  39. static const char * s_tags[] = {"DEB", "INF", "WAR", "ERR"};
  40. char tLine[2048];
  41. time_t tNow = time(nullptr);
  42. struct tm tTm;
  43. localtime_r(&tNow, &tTm);
  44. int tLen = (int) strftime(tLine, sizeof(tLine), "%Y/%m/%d %H:%M:%S ", &tTm);
  45. int tStart = tLen; // the message without the time stamp starts here (sink)
  46. tLen += snprintf(tLine + tLen, sizeof(tLine) - tLen, "%s ", s_tags[(int) level]);
  47. if (tLen < (int) sizeof(tLine))
  48. {
  49. int tN = vsnprintf(tLine + tLen, sizeof(tLine) - tLen, fmt, ap);
  50. tLen = (tN < 0) ? tLen : tLen + tN;
  51. }
  52. if (tLen > (int) sizeof(tLine) - 2)
  53. {
  54. tLen = (int) sizeof(tLine) - 2;
  55. }
  56. tLine[tLen++] = '\n';
  57. tLine[tLen] = 0;
  58. std::lock_guard<std::mutex> lock(m_mutex);
  59. if (m_sink)
  60. {
  61. tLine[tLen - 1] = 0;
  62. m_sink(level, tLine + tStart);
  63. tLine[tLen - 1] = '\n';
  64. }
  65. if (m_toStdout)
  66. {
  67. fwrite(tLine, 1, tLen, stdout);
  68. fflush(stdout);
  69. }
  70. if (! m_file.empty())
  71. {
  72. FILE * tF = fopen(m_file.c_str(), "a");
  73. if (tF != nullptr)
  74. {
  75. fwrite(tLine, 1, tLen, tF);
  76. fclose(tF);
  77. }
  78. }
  79. }
  80. void LogParent::log(LogLevel level, const char * fmt, ...) const
  81. {
  82. Logger & tL = Logger::instance();
  83. if (level < tL.level())
  84. {
  85. return;
  86. }
  87. char tMsg[1800];
  88. va_list ap;
  89. va_start(ap, fmt);
  90. vsnprintf(tMsg, sizeof(tMsg), fmt, ap);
  91. va_end(ap);
  92. tL.log(level, "%s%s", m_prefix.c_str(), tMsg);
  93. }
  94. LogLevel parseLogLevel(const std::string & s, bool * ok)
  95. {
  96. if (ok)
  97. {
  98. *ok = true;
  99. }
  100. if (s == "debug")
  101. {
  102. return LogLevel::Debug;
  103. }
  104. if (s == "info")
  105. {
  106. return LogLevel::Info;
  107. }
  108. if (s == "warn")
  109. {
  110. return LogLevel::Warn;
  111. }
  112. if (s == "error")
  113. {
  114. return LogLevel::Error;
  115. }
  116. if (ok)
  117. {
  118. *ok = false;
  119. }
  120. return LogLevel::Info;
  121. }
  122. } // namespace mtx