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.

logger_impl.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. #include <spdlog/logger.h>
  7. #include <spdlog/sinks/stdout_sinks.h>
  8. #include <memory>
  9. #include <string>
  10. // create logger with given name, sinks and the default pattern formatter
  11. // all other ctors will call this one
  12. template<class It>
  13. inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end):
  14. _name(logger_name),
  15. _sinks(begin, end),
  16. _formatter(std::make_shared<pattern_formatter>("%+"))
  17. {
  18. _level = level::info;
  19. _flush_level = level::off;
  20. _last_err_time = 0;
  21. _err_handler = [this](const std::string &msg)
  22. {
  23. this->_default_err_handler(msg);
  24. };
  25. }
  26. // ctor with sinks as init list
  27. inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list):
  28. logger(logger_name, sinks_list.begin(), sinks_list.end())
  29. {}
  30. // ctor with single sink
  31. inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink):
  32. logger(logger_name,
  33. {
  34. single_sink
  35. })
  36. {}
  37. inline spdlog::logger::~logger() = default;
  38. inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
  39. {
  40. _set_formatter(msg_formatter);
  41. }
  42. inline void spdlog::logger::set_pattern(const std::string& pattern)
  43. {
  44. _set_pattern(pattern);
  45. }
  46. template <typename... Args>
  47. inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Args&... args)
  48. {
  49. if (!should_log(lvl)) return;
  50. try
  51. {
  52. details::log_msg log_msg(&_name, lvl);
  53. log_msg.raw.write(fmt, args...);
  54. _sink_it(log_msg);
  55. }
  56. catch (const std::exception &ex)
  57. {
  58. _err_handler(ex.what());
  59. }
  60. catch (...)
  61. {
  62. _err_handler("Unknown exception");
  63. }
  64. }
  65. template <typename... Args>
  66. inline void spdlog::logger::log(level::level_enum lvl, const char* msg)
  67. {
  68. if (!should_log(lvl)) return;
  69. try
  70. {
  71. details::log_msg log_msg(&_name, lvl);
  72. log_msg.raw << msg;
  73. _sink_it(log_msg);
  74. }
  75. catch (const std::exception &ex)
  76. {
  77. _err_handler(ex.what());
  78. }
  79. catch (...)
  80. {
  81. _err_handler("Unknown exception");
  82. }
  83. }
  84. template<typename T>
  85. inline void spdlog::logger::log(level::level_enum lvl, const T& msg)
  86. {
  87. if (!should_log(lvl)) return;
  88. try
  89. {
  90. details::log_msg log_msg(&_name, lvl);
  91. log_msg.raw << msg;
  92. _sink_it(log_msg);
  93. }
  94. catch (const std::exception &ex)
  95. {
  96. _err_handler(ex.what());
  97. }
  98. catch (...)
  99. {
  100. _err_handler("Unknown exception");
  101. }
  102. }
  103. template <typename... Args>
  104. inline void spdlog::logger::trace(const char* fmt, const Args&... args)
  105. {
  106. log(level::trace, fmt, args...);
  107. }
  108. template <typename... Args>
  109. inline void spdlog::logger::debug(const char* fmt, const Args&... args)
  110. {
  111. log(level::debug, fmt, args...);
  112. }
  113. template <typename... Args>
  114. inline void spdlog::logger::info(const char* fmt, const Args&... args)
  115. {
  116. log(level::info, fmt, args...);
  117. }
  118. template <typename... Args>
  119. inline void spdlog::logger::warn(const char* fmt, const Args&... args)
  120. {
  121. log(level::warn, fmt, args...);
  122. }
  123. template <typename... Args>
  124. inline void spdlog::logger::error(const char* fmt, const Args&... args)
  125. {
  126. log(level::err, fmt, args...);
  127. }
  128. template <typename... Args>
  129. inline void spdlog::logger::critical(const char* fmt, const Args&... args)
  130. {
  131. log(level::critical, fmt, args...);
  132. }
  133. template<typename T>
  134. inline void spdlog::logger::trace(const T& msg)
  135. {
  136. log(level::trace, msg);
  137. }
  138. template<typename T>
  139. inline void spdlog::logger::debug(const T& msg)
  140. {
  141. log(level::debug, msg);
  142. }
  143. template<typename T>
  144. inline void spdlog::logger::info(const T& msg)
  145. {
  146. log(level::info, msg);
  147. }
  148. template<typename T>
  149. inline void spdlog::logger::warn(const T& msg)
  150. {
  151. log(level::warn, msg);
  152. }
  153. template<typename T>
  154. inline void spdlog::logger::error(const T& msg)
  155. {
  156. log(level::err, msg);
  157. }
  158. template<typename T>
  159. inline void spdlog::logger::critical(const T& msg)
  160. {
  161. log(level::critical, msg);
  162. }
  163. //
  164. // name and level
  165. //
  166. inline const std::string& spdlog::logger::name() const
  167. {
  168. return _name;
  169. }
  170. inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
  171. {
  172. _level.store(log_level);
  173. }
  174. inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
  175. {
  176. _err_handler = err_handler;
  177. }
  178. inline spdlog::log_err_handler spdlog::logger::error_handler()
  179. {
  180. return _err_handler;
  181. }
  182. inline void spdlog::logger::flush_on(level::level_enum log_level)
  183. {
  184. _flush_level.store(log_level);
  185. }
  186. inline spdlog::level::level_enum spdlog::logger::level() const
  187. {
  188. return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
  189. }
  190. inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
  191. {
  192. return msg_level >= _level.load(std::memory_order_relaxed);
  193. }
  194. //
  195. // protected virtual called at end of each user log call (if enabled) by the line_logger
  196. //
  197. inline void spdlog::logger::_sink_it(details::log_msg& msg)
  198. {
  199. _formatter->format(msg);
  200. for (auto &sink : _sinks)
  201. {
  202. if( sink->should_log( msg.level))
  203. {
  204. sink->log(msg);
  205. }
  206. }
  207. if(_should_flush_on(msg))
  208. flush();
  209. }
  210. inline void spdlog::logger::_set_pattern(const std::string& pattern)
  211. {
  212. _formatter = std::make_shared<pattern_formatter>(pattern);
  213. }
  214. inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
  215. {
  216. _formatter = msg_formatter;
  217. }
  218. inline void spdlog::logger::flush()
  219. {
  220. for (auto& sink : _sinks)
  221. sink->flush();
  222. }
  223. inline void spdlog::logger::_default_err_handler(const std::string &msg)
  224. {
  225. auto now = time(nullptr);
  226. if (now - _last_err_time < 60)
  227. return;
  228. auto tm_time = details::os::localtime(now);
  229. char date_buf[100];
  230. std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
  231. details::log_msg err_msg;
  232. err_msg.formatted.write("[*** LOG ERROR ***] [{}] [{}] [{}]{}", name(), msg, date_buf, details::os::eol);
  233. sinks::stderr_sink_mt::instance()->log(err_msg);
  234. _last_err_time = now;
  235. }
  236. inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg)
  237. {
  238. const auto flush_level = _flush_level.load(std::memory_order_relaxed);
  239. return (msg.level >= flush_level) && (msg.level != level::off);
  240. }