logger-inl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/logger.h>
  6. #endif
  7. #include <spdlog/sinks/sink.h>
  8. #include <spdlog/details/backtracer.h>
  9. #include <spdlog/pattern_formatter.h>
  10. #include <cstdio>
  11. namespace spdlog {
  12. // public methods
  13. SPDLOG_INLINE logger::logger(const logger &other)
  14. : name_(other.name_)
  15. , sinks_(other.sinks_)
  16. , level_(other.level_.load(std::memory_order_relaxed))
  17. , flush_level_(other.flush_level_.load(std::memory_order_relaxed))
  18. , custom_err_handler_(other.custom_err_handler_)
  19. , tracer_(other.tracer_)
  20. {}
  21. SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
  22. sinks_(std::move(other.sinks_)),
  23. level_(other.level_.load(std::memory_order_relaxed)),
  24. flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
  25. custom_err_handler_(std::move(other.custom_err_handler_)),
  26. tracer_(std::move(other.tracer_))
  27. {}
  28. SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT
  29. {
  30. this->swap(other);
  31. return *this;
  32. }
  33. SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT
  34. {
  35. name_.swap(other.name_);
  36. sinks_.swap(other.sinks_);
  37. // swap level_
  38. auto other_level = other.level_.load();
  39. auto my_level = level_.exchange(other_level);
  40. other.level_.store(my_level);
  41. // swap flush level_
  42. other_level = other.flush_level_.load();
  43. my_level = flush_level_.exchange(other_level);
  44. other.flush_level_.store(my_level);
  45. custom_err_handler_.swap(other.custom_err_handler_);
  46. std::swap(tracer_, other.tracer_);
  47. }
  48. SPDLOG_INLINE void swap(logger &a, logger &b)
  49. {
  50. a.swap(b);
  51. }
  52. SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
  53. {
  54. level_.store(log_level);
  55. }
  56. SPDLOG_INLINE level::level_enum logger::level() const
  57. {
  58. return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
  59. }
  60. SPDLOG_INLINE const std::string &logger::name() const
  61. {
  62. return name_;
  63. }
  64. // set formatting for the sinks in this logger.
  65. // each sink will get a separate instance of the formatter object.
  66. SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
  67. {
  68. for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
  69. {
  70. if (std::next(it) == sinks_.end())
  71. {
  72. // last element - we can be move it.
  73. (*it)->set_formatter(std::move(f));
  74. break; // to prevent clang-tidy warning
  75. }
  76. else
  77. {
  78. (*it)->set_formatter(f->clone());
  79. }
  80. }
  81. }
  82. SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
  83. {
  84. auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
  85. set_formatter(std::move(new_formatter));
  86. }
  87. // create new backtrace sink and move to it all our child sinks
  88. SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
  89. {
  90. tracer_.enable(n_messages);
  91. }
  92. // restore orig sinks and level and delete the backtrace sink
  93. SPDLOG_INLINE void logger::disable_backtrace()
  94. {
  95. tracer_.disable();
  96. }
  97. SPDLOG_INLINE void logger::dump_backtrace()
  98. {
  99. dump_backtrace_();
  100. }
  101. // flush functions
  102. SPDLOG_INLINE void logger::flush()
  103. {
  104. flush_();
  105. }
  106. SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
  107. {
  108. flush_level_.store(log_level);
  109. }
  110. SPDLOG_INLINE level::level_enum logger::flush_level() const
  111. {
  112. return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
  113. }
  114. // sinks
  115. SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
  116. {
  117. return sinks_;
  118. }
  119. SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
  120. {
  121. return sinks_;
  122. }
  123. // error handler
  124. SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
  125. {
  126. custom_err_handler_ = std::move(handler);
  127. }
  128. // create new logger with same sinks and configuration.
  129. SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
  130. {
  131. auto cloned = std::make_shared<logger>(*this);
  132. cloned->name_ = std::move(logger_name);
  133. return cloned;
  134. }
  135. // protected methods
  136. SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled)
  137. {
  138. if (log_enabled)
  139. {
  140. sink_it_(log_msg);
  141. }
  142. if (traceback_enabled)
  143. {
  144. tracer_.push_back(log_msg);
  145. }
  146. }
  147. SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
  148. {
  149. for (auto &sink : sinks_)
  150. {
  151. if (sink->should_log(msg.level))
  152. {
  153. SPDLOG_TRY
  154. {
  155. sink->log(msg);
  156. }
  157. SPDLOG_LOGGER_CATCH(msg.source)
  158. }
  159. }
  160. if (should_flush_(msg))
  161. {
  162. flush_();
  163. }
  164. }
  165. SPDLOG_INLINE void logger::flush_()
  166. {
  167. for (auto &sink : sinks_)
  168. {
  169. SPDLOG_TRY
  170. {
  171. sink->flush();
  172. }
  173. SPDLOG_LOGGER_CATCH(source_loc())
  174. }
  175. }
  176. SPDLOG_INLINE void logger::dump_backtrace_()
  177. {
  178. using details::log_msg;
  179. if (tracer_.enabled() && !tracer_.empty())
  180. {
  181. sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
  182. tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
  183. sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
  184. }
  185. }
  186. SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
  187. {
  188. auto flush_level = flush_level_.load(std::memory_order_relaxed);
  189. return (msg.level >= flush_level) && (msg.level != level::off);
  190. }
  191. SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
  192. {
  193. if (custom_err_handler_)
  194. {
  195. custom_err_handler_(msg);
  196. }
  197. else
  198. {
  199. using std::chrono::system_clock;
  200. static std::mutex mutex;
  201. static std::chrono::system_clock::time_point last_report_time;
  202. static size_t err_counter = 0;
  203. std::lock_guard<std::mutex> lk{mutex};
  204. auto now = system_clock::now();
  205. err_counter++;
  206. if (now - last_report_time < std::chrono::seconds(1))
  207. {
  208. return;
  209. }
  210. last_report_time = now;
  211. auto tm_time = details::os::localtime(system_clock::to_time_t(now));
  212. char date_buf[64];
  213. std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
  214. #if defined(USING_R) && defined(R_R_H) // if in R environment
  215. REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
  216. #else
  217. std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
  218. #endif
  219. }
  220. }
  221. } // namespace spdlog