spdlog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. // spdlog main header file.
  4. // see example.cpp for usage example
  5. #ifndef SPDLOG_H
  6. #define SPDLOG_H
  7. #pragma once
  8. #include <spdlog/common.h>
  9. #include <spdlog/details/registry.h>
  10. #include <spdlog/logger.h>
  11. #include <spdlog/version.h>
  12. #include <spdlog/details/synchronous_factory.h>
  13. #include <chrono>
  14. #include <functional>
  15. #include <memory>
  16. #include <string>
  17. namespace spdlog {
  18. using default_factory = synchronous_factory;
  19. // Create and register a logger with a templated sink type
  20. // The logger's level, formatter and flush level will be set according the
  21. // global settings.
  22. //
  23. // Example:
  24. // spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
  25. template<typename Sink, typename... SinkArgs>
  26. inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
  27. {
  28. return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
  29. }
  30. // Initialize and register a logger,
  31. // formatter and flush level will be set according the global settings.
  32. //
  33. // Useful for initializing manually created loggers with the global settings.
  34. //
  35. // Example:
  36. // auto mylogger = std::make_shared<spdlog::logger>("mylogger", ...);
  37. // spdlog::initialize_logger(mylogger);
  38. SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
  39. // Return an existing logger or nullptr if a logger with such name doesn't
  40. // exist.
  41. // example: spdlog::get("my_logger")->info("hello {}", "world");
  42. SPDLOG_API std::shared_ptr<logger> get(const std::string &name);
  43. // Set global formatter. Each sink in each logger will get a clone of this object
  44. SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
  45. // Set global format string.
  46. // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
  47. SPDLOG_API void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
  48. // enable global backtrace support
  49. SPDLOG_API void enable_backtrace(size_t n_messages);
  50. // disable global backtrace support
  51. SPDLOG_API void disable_backtrace();
  52. // call dump backtrace on default logger
  53. SPDLOG_API void dump_backtrace();
  54. // Get global logging level
  55. SPDLOG_API level::level_enum get_level();
  56. // Set global logging level
  57. SPDLOG_API void set_level(level::level_enum log_level);
  58. // Determine whether the default logger should log messages with a certain level
  59. SPDLOG_API bool should_log(level::level_enum lvl);
  60. // Set global flush level
  61. SPDLOG_API void flush_on(level::level_enum log_level);
  62. // Start/Restart a periodic flusher thread
  63. // Warning: Use only if all your loggers are thread safe!
  64. SPDLOG_API void flush_every(std::chrono::seconds interval);
  65. // Set global error handler
  66. SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
  67. // Register the given logger with the given name
  68. SPDLOG_API void register_logger(std::shared_ptr<logger> logger);
  69. // Apply a user defined function on all registered loggers
  70. // Example:
  71. // spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
  72. SPDLOG_API void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun);
  73. // Drop the reference to the given logger
  74. SPDLOG_API void drop(const std::string &name);
  75. // Drop all references from the registry
  76. SPDLOG_API void drop_all();
  77. // stop any running threads started by spdlog and clean registry loggers
  78. SPDLOG_API void shutdown();
  79. // Automatic registration of loggers when using spdlog::create() or spdlog::create_async
  80. SPDLOG_API void set_automatic_registration(bool automatic_registration);
  81. // API for using default logger (stdout_color_mt),
  82. // e.g: spdlog::info("Message {}", 1);
  83. //
  84. // The default logger object can be accessed using the spdlog::default_logger():
  85. // For example, to add another sink to it:
  86. // spdlog::default_logger()->sinks().push_back(some_sink);
  87. //
  88. // The default logger can replaced using spdlog::set_default_logger(new_logger).
  89. // For example, to replace it with a file logger.
  90. //
  91. // IMPORTANT:
  92. // The default API is thread safe (for _mt loggers), but:
  93. // set_default_logger() *should not* be used concurrently with the default API.
  94. // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
  95. SPDLOG_API std::shared_ptr<spdlog::logger> default_logger();
  96. SPDLOG_API spdlog::logger *default_logger_raw();
  97. SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
  98. template<typename... Args>
  99. inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
  100. {
  101. default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
  102. }
  103. template<typename... Args>
  104. inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
  105. {
  106. default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
  107. }
  108. template<typename... Args>
  109. inline void trace(format_string_t<Args...> fmt, Args &&... args)
  110. {
  111. default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
  112. }
  113. template<typename... Args>
  114. inline void debug(format_string_t<Args...> fmt, Args &&... args)
  115. {
  116. default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
  117. }
  118. template<typename... Args>
  119. inline void info(format_string_t<Args...> fmt, Args &&... args)
  120. {
  121. default_logger_raw()->info(fmt, std::forward<Args>(args)...);
  122. }
  123. template<typename... Args>
  124. inline void warn(format_string_t<Args...> fmt, Args &&... args)
  125. {
  126. default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
  127. }
  128. template<typename... Args>
  129. inline void error(format_string_t<Args...> fmt, Args &&... args)
  130. {
  131. default_logger_raw()->error(fmt, std::forward<Args>(args)...);
  132. }
  133. template<typename... Args>
  134. inline void critical(format_string_t<Args...> fmt, Args &&... args)
  135. {
  136. default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
  137. }
  138. template<typename T>
  139. inline void log(source_loc source, level::level_enum lvl, const T &msg)
  140. {
  141. default_logger_raw()->log(source, lvl, msg);
  142. }
  143. template<typename T>
  144. inline void log(level::level_enum lvl, const T &msg)
  145. {
  146. default_logger_raw()->log(lvl, msg);
  147. }
  148. #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
  149. template<typename... Args>
  150. inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
  151. {
  152. default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
  153. }
  154. template<typename... Args>
  155. inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
  156. {
  157. default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
  158. }
  159. template<typename... Args>
  160. inline void trace(wformat_string_t<Args...> fmt, Args &&... args)
  161. {
  162. default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
  163. }
  164. template<typename... Args>
  165. inline void debug(wformat_string_t<Args...> fmt, Args &&... args)
  166. {
  167. default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
  168. }
  169. template<typename... Args>
  170. inline void info(wformat_string_t<Args...> fmt, Args &&... args)
  171. {
  172. default_logger_raw()->info(fmt, std::forward<Args>(args)...);
  173. }
  174. template<typename... Args>
  175. inline void warn(wformat_string_t<Args...> fmt, Args &&... args)
  176. {
  177. default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
  178. }
  179. template<typename... Args>
  180. inline void error(wformat_string_t<Args...> fmt, Args &&... args)
  181. {
  182. default_logger_raw()->error(fmt, std::forward<Args>(args)...);
  183. }
  184. template<typename... Args>
  185. inline void critical(wformat_string_t<Args...> fmt, Args &&... args)
  186. {
  187. default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
  188. }
  189. #endif
  190. template<typename T>
  191. inline void trace(const T &msg)
  192. {
  193. default_logger_raw()->trace(msg);
  194. }
  195. template<typename T>
  196. inline void debug(const T &msg)
  197. {
  198. default_logger_raw()->debug(msg);
  199. }
  200. template<typename T>
  201. inline void info(const T &msg)
  202. {
  203. default_logger_raw()->info(msg);
  204. }
  205. template<typename T>
  206. inline void warn(const T &msg)
  207. {
  208. default_logger_raw()->warn(msg);
  209. }
  210. template<typename T>
  211. inline void error(const T &msg)
  212. {
  213. default_logger_raw()->error(msg);
  214. }
  215. template<typename T>
  216. inline void critical(const T &msg)
  217. {
  218. default_logger_raw()->critical(msg);
  219. }
  220. } // namespace spdlog
  221. //
  222. // enable/disable log calls at compile time according to global level.
  223. //
  224. // define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
  225. // SPDLOG_LEVEL_TRACE,
  226. // SPDLOG_LEVEL_DEBUG,
  227. // SPDLOG_LEVEL_INFO,
  228. // SPDLOG_LEVEL_WARN,
  229. // SPDLOG_LEVEL_ERROR,
  230. // SPDLOG_LEVEL_CRITICAL,
  231. // SPDLOG_LEVEL_OFF
  232. //
  233. #define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
  234. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
  235. # define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
  236. # define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
  237. #else
  238. # define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
  239. # define SPDLOG_TRACE(...) (void)0
  240. #endif
  241. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
  242. # define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
  243. # define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
  244. #else
  245. # define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
  246. # define SPDLOG_DEBUG(...) (void)0
  247. #endif
  248. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
  249. # define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
  250. # define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
  251. #else
  252. # define SPDLOG_LOGGER_INFO(logger, ...) (void)0
  253. # define SPDLOG_INFO(...) (void)0
  254. #endif
  255. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
  256. # define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
  257. # define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
  258. #else
  259. # define SPDLOG_LOGGER_WARN(logger, ...) (void)0
  260. # define SPDLOG_WARN(...) (void)0
  261. #endif
  262. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
  263. # define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
  264. # define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
  265. #else
  266. # define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
  267. # define SPDLOG_ERROR(...) (void)0
  268. #endif
  269. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
  270. # define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
  271. # define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
  272. #else
  273. # define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
  274. # define SPDLOG_CRITICAL(...) (void)0
  275. #endif
  276. #ifdef SPDLOG_HEADER_ONLY
  277. # include "spdlog-inl.h"
  278. #endif
  279. #endif // SPDLOG_H