cnstream_logging.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************
  2. * Copyright (C) 2019 by Cambricon, Inc. All rights reserved
  3. *
  4. * This source code is licensed under the Apache-2.0 license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * A part of this source code is referenced from glog project.
  8. * https://github.com/google/glog/blob/master/src/logging.cc
  9. *
  10. * Copyright (c) 1999, Google Inc.
  11. *
  12. * This source code is licensed under the BSD 3-Clause license found in the
  13. * LICENSE file in the root directory of this source tree.
  14. *
  15. *************************************************************************/
  16. #ifndef CNSTREAM_CORE_LOGGING_HPP_
  17. #define CNSTREAM_CORE_LOGGING_HPP_
  18. #include <gflags/gflags.h>
  19. #include <time.h>
  20. #include <string>
  21. #include <streambuf>
  22. #include <ostream>
  23. /**
  24. * @brief Log filter.
  25. *
  26. * Usage:
  27. * 1 ./app --log_filter=SOURCE:2,INFERENCE:3 ...
  28. * 2 export CNSTREAM_log_filter=SOURCE:2,INFERENCE:3 ...
  29. */
  30. DECLARE_string(log_filter);
  31. /**
  32. * @brief Min category log level, default LOG_INFO
  33. */
  34. DECLARE_int32(min_log_level);
  35. /**
  36. * @brief Flush log file time, in second, default 30s
  37. */
  38. DECLARE_int32(flush_log_file_secs);
  39. /**
  40. * @brief Log messages go to stderr, default true
  41. */
  42. DECLARE_bool(log_to_stderr);
  43. /**
  44. * @brief Log messages go to log file, default false
  45. */
  46. DECLARE_bool(log_to_file);
  47. #define STR(src) #src
  48. #define LOGF(category) \
  49. cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_FATAL).stream()
  50. #define LOGF_IF(category, condition) \
  51. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGF(category)
  52. #define LOGE(category) \
  53. cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_ERROR).stream()
  54. #define LOGE_IF(category, condition) \
  55. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGE(category)
  56. #define LOGW(category) \
  57. cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_WARNING).stream()
  58. #define LOGW_IF(category, condition) \
  59. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGW(category)
  60. #define LOGI(category) cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_INFO).stream()
  61. #define LOGI_IF(category, condition) \
  62. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGI(category)
  63. #define LOGD(category) \
  64. cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_DEBUG).stream()
  65. #define LOGD_IF(category, condition) \
  66. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGD(category)
  67. #define LOGT(category) \
  68. cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_TRACE).stream()
  69. #define LOGT_IF(category, condition) \
  70. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGT(category)
  71. #define LOGA(category) cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_ALL).stream()
  72. #define LOGA_IF(category, condition) \
  73. !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGA(category)
  74. namespace cnstream {
  75. /**
  76. * @brief log severity
  77. * 0, FATAL
  78. * 1, ERROR
  79. * 2, WARNING
  80. * 3, INFO
  81. * 4, DEBUG
  82. * 5, TRACE
  83. * 6, ALL
  84. */
  85. enum class LogSeverity { LOG_FATAL = 0, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_TRACE, LOG_ALL };
  86. class LogSink {
  87. public:
  88. virtual ~LogSink() { }
  89. virtual void Send(LogSeverity severity, const char* category,
  90. const char* filename, int line,
  91. const struct ::tm* tm_time, int32_t usecs,
  92. const char* message, size_t message_len) = 0;
  93. virtual void WaitTillSent() { } // noop default
  94. static std::string ToString(LogSeverity severity, const char* category,
  95. const char* filename, int line,
  96. const struct ::tm* tm_time, int32_t usecs,
  97. const char* message, size_t message_len);
  98. }; // class LogSink
  99. class LogMessageVoidify {
  100. public:
  101. LogMessageVoidify() { }
  102. // This has to be an operator with a precedence lower than << but
  103. // higher than ?:
  104. void operator&(std::ostream&) { }
  105. };
  106. class LogMessage {
  107. public:
  108. class LogStreamBuf : public std::streambuf {
  109. public:
  110. // REQUIREMENTS: "len" must be >= 2 to account for the '\n' and '\0'.
  111. LogStreamBuf(char *buf, int len) {
  112. setp(buf, buf + len - 2);
  113. }
  114. // This effectively ignores overflow.
  115. virtual int_type overflow(int_type ch) {
  116. return ch;
  117. }
  118. // Legacy public ostrstream method.
  119. size_t pcount() const { return pptr() - pbase(); }
  120. char* pbase() const { return std::streambuf::pbase(); }
  121. }; // class LogStreamBuf
  122. class LogStream : public std::ostream {
  123. public:
  124. LogStream(char *buf, int len)
  125. : std::ostream(NULL),
  126. streambuf_(buf, len) {
  127. rdbuf(&streambuf_);
  128. }
  129. // Legacy std::streambuf methods.
  130. size_t pcount() const { return streambuf_.pcount(); }
  131. char* pbase() const { return streambuf_.pbase(); }
  132. char* str() const { return pbase(); }
  133. private:
  134. LogStream(const LogStream&) = delete;
  135. LogStream& operator=(const LogStream&) = delete;
  136. LogStreamBuf streambuf_;
  137. }; // class LogStream
  138. LogMessage(const char* category, const char* file, int line, LogSeverity severity);
  139. ~LogMessage();
  140. void Init(const char* category, const char* file, int line, LogSeverity severity);
  141. std::ostream& stream();
  142. struct LogMessageData;
  143. private:
  144. LogMessage(const LogMessage&) = delete;
  145. LogMessage& operator=(const LogMessage&) = delete;
  146. void Flush();
  147. void SendToLog();
  148. LogMessageData* data_;
  149. LogMessageData* allocated_;
  150. static const size_t MaxLogMsgLen;
  151. }; // class LogMessage
  152. void InitCNStreamLogging(const char* log_dir);
  153. void AddLogSink(LogSink* log_sink);
  154. void RemoveLogSink(LogSink* log_sink);
  155. void ShutdownCNStreamLogging();
  156. } // namespace cnstream
  157. #endif // CNSTREAM_CORE_LOGGING_HPP_