Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

246 lines
6.5KB

  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/details/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 bool logger::should_log(level::level_enum msg_level) const
  53. {
  54. return msg_level >= level_.load(std::memory_order_relaxed);
  55. }
  56. SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
  57. {
  58. level_.store(log_level);
  59. }
  60. SPDLOG_INLINE level::level_enum logger::level() const
  61. {
  62. return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
  63. }
  64. SPDLOG_INLINE const std::string &logger::name() const
  65. {
  66. return name_;
  67. }
  68. // set formatting for the sinks in this logger.
  69. // each sink will get a seperate instance of the formatter object.
  70. SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
  71. {
  72. for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
  73. {
  74. if (std::next(it) == sinks_.end())
  75. {
  76. // last element - we can be move it.
  77. (*it)->set_formatter(std::move(f));
  78. }
  79. else
  80. {
  81. (*it)->set_formatter(f->clone());
  82. }
  83. }
  84. }
  85. SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
  86. {
  87. auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
  88. set_formatter(std::move(new_formatter));
  89. }
  90. // create new backtrace sink and move to it all our child sinks
  91. SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
  92. {
  93. tracer_.enable(n_messages);
  94. }
  95. // restore orig sinks and level and delete the backtrace sink
  96. SPDLOG_INLINE void logger::disable_backtrace()
  97. {
  98. tracer_.disable();
  99. }
  100. SPDLOG_INLINE void logger::dump_backtrace()
  101. {
  102. dump_backtrace_();
  103. }
  104. // flush functions
  105. SPDLOG_INLINE void logger::flush()
  106. {
  107. flush_();
  108. }
  109. SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
  110. {
  111. flush_level_.store(log_level);
  112. }
  113. SPDLOG_INLINE level::level_enum logger::flush_level() const
  114. {
  115. return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
  116. }
  117. // sinks
  118. SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
  119. {
  120. return sinks_;
  121. }
  122. SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
  123. {
  124. return sinks_;
  125. }
  126. // error handler
  127. SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
  128. {
  129. custom_err_handler_ = handler;
  130. }
  131. // create new logger with same sinks and configuration.
  132. SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
  133. {
  134. auto cloned = std::make_shared<logger>(*this);
  135. cloned->name_ = std::move(logger_name);
  136. return cloned;
  137. }
  138. // protected methods
  139. SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
  140. {
  141. for (auto &sink : sinks_)
  142. {
  143. if (sink->should_log(msg.level))
  144. {
  145. SPDLOG_TRY
  146. {
  147. sink->log(msg);
  148. }
  149. SPDLOG_LOGGER_CATCH()
  150. }
  151. }
  152. if (should_flush_(msg))
  153. {
  154. flush_();
  155. }
  156. }
  157. SPDLOG_INLINE void logger::flush_()
  158. {
  159. for (auto &sink : sinks_)
  160. {
  161. SPDLOG_TRY
  162. {
  163. sink->flush();
  164. }
  165. SPDLOG_LOGGER_CATCH()
  166. }
  167. }
  168. SPDLOG_INLINE void logger::dump_backtrace_()
  169. {
  170. using details::log_msg;
  171. if (tracer_)
  172. {
  173. sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
  174. tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
  175. sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
  176. }
  177. }
  178. SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
  179. {
  180. auto flush_level = flush_level_.load(std::memory_order_relaxed);
  181. return (msg.level >= flush_level) && (msg.level != level::off);
  182. }
  183. SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
  184. {
  185. if (custom_err_handler_)
  186. {
  187. custom_err_handler_(msg);
  188. }
  189. else
  190. {
  191. using std::chrono::system_clock;
  192. static std::mutex mutex;
  193. static std::chrono::system_clock::time_point last_report_time;
  194. static size_t err_counter = 0;
  195. std::lock_guard<std::mutex> lk{mutex};
  196. auto now = system_clock::now();
  197. err_counter++;
  198. if (now - last_report_time < std::chrono::seconds(1))
  199. {
  200. return;
  201. }
  202. last_report_time = now;
  203. auto tm_time = details::os::localtime(system_clock::to_time_t(now));
  204. char date_buf[64];
  205. std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
  206. fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
  207. }
  208. }
  209. } // namespace spdlog