spdlog.h 11 KB

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