spdlog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. template<typename Rep, typename Period>
  65. inline void flush_every(std::chrono::duration<Rep, Period> interval)
  66. {
  67. details::registry::instance().flush_every(interval);
  68. }
  69. // Set global error handler
  70. SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
  71. // Register the given logger with the given name
  72. SPDLOG_API void register_logger(std::shared_ptr<logger> logger);
  73. // Apply a user defined function on all registered loggers
  74. // Example:
  75. // spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
  76. SPDLOG_API void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun);
  77. // Drop the reference to the given logger
  78. SPDLOG_API void drop(const std::string &name);
  79. // Drop all references from the registry
  80. SPDLOG_API void drop_all();
  81. // stop any running threads started by spdlog and clean registry loggers
  82. SPDLOG_API void shutdown();
  83. // Automatic registration of loggers when using spdlog::create() or spdlog::create_async
  84. SPDLOG_API void set_automatic_registration(bool automatic_registration);
  85. // API for using default logger (stdout_color_mt),
  86. // e.g: spdlog::info("Message {}", 1);
  87. //
  88. // The default logger object can be accessed using the spdlog::default_logger():
  89. // For example, to add another sink to it:
  90. // spdlog::default_logger()->sinks().push_back(some_sink);
  91. //
  92. // The default logger can replaced using spdlog::set_default_logger(new_logger).
  93. // For example, to replace it with a file logger.
  94. //
  95. // IMPORTANT:
  96. // The default API is thread safe (for _mt loggers), but:
  97. // set_default_logger() *should not* be used concurrently with the default API.
  98. // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
  99. SPDLOG_API std::shared_ptr<spdlog::logger> default_logger();
  100. SPDLOG_API spdlog::logger *default_logger_raw();
  101. SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
  102. template<typename... Args>
  103. inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
  104. {
  105. default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
  106. }
  107. template<typename... Args>
  108. inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
  109. {
  110. default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
  111. }
  112. template<typename... Args>
  113. inline void trace(format_string_t<Args...> fmt, Args &&... args)
  114. {
  115. default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
  116. }
  117. template<typename... Args>
  118. inline void debug(format_string_t<Args...> fmt, Args &&... args)
  119. {
  120. default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
  121. }
  122. template<typename... Args>
  123. inline void info(format_string_t<Args...> fmt, Args &&... args)
  124. {
  125. default_logger_raw()->info(fmt, std::forward<Args>(args)...);
  126. }
  127. template<typename... Args>
  128. inline void warn(format_string_t<Args...> fmt, Args &&... args)
  129. {
  130. default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
  131. }
  132. template<typename... Args>
  133. inline void error(format_string_t<Args...> fmt, Args &&... args)
  134. {
  135. default_logger_raw()->error(fmt, std::forward<Args>(args)...);
  136. }
  137. template<typename... Args>
  138. inline void critical(format_string_t<Args...> fmt, Args &&... args)
  139. {
  140. default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
  141. }
  142. template<typename T>
  143. inline void log(source_loc source, level::level_enum lvl, const T &msg)
  144. {
  145. default_logger_raw()->log(source, lvl, msg);
  146. }
  147. template<typename T>
  148. inline void log(level::level_enum lvl, const T &msg)
  149. {
  150. default_logger_raw()->log(lvl, msg);
  151. }
  152. #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
  153. template<typename... Args>
  154. inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
  155. {
  156. default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
  157. }
  158. template<typename... Args>
  159. inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
  160. {
  161. default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
  162. }
  163. template<typename... Args>
  164. inline void trace(wformat_string_t<Args...> fmt, Args &&... args)
  165. {
  166. default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
  167. }
  168. template<typename... Args>
  169. inline void debug(wformat_string_t<Args...> fmt, Args &&... args)
  170. {
  171. default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
  172. }
  173. template<typename... Args>
  174. inline void info(wformat_string_t<Args...> fmt, Args &&... args)
  175. {
  176. default_logger_raw()->info(fmt, std::forward<Args>(args)...);
  177. }
  178. template<typename... Args>
  179. inline void warn(wformat_string_t<Args...> fmt, Args &&... args)
  180. {
  181. default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
  182. }
  183. template<typename... Args>
  184. inline void error(wformat_string_t<Args...> fmt, Args &&... args)
  185. {
  186. default_logger_raw()->error(fmt, std::forward<Args>(args)...);
  187. }
  188. template<typename... Args>
  189. inline void critical(wformat_string_t<Args...> fmt, Args &&... args)
  190. {
  191. default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
  192. }
  193. #endif
  194. template<typename T>
  195. inline void trace(const T &msg)
  196. {
  197. default_logger_raw()->trace(msg);
  198. }
  199. template<typename T>
  200. inline void debug(const T &msg)
  201. {
  202. default_logger_raw()->debug(msg);
  203. }
  204. template<typename T>
  205. inline void info(const T &msg)
  206. {
  207. default_logger_raw()->info(msg);
  208. }
  209. template<typename T>
  210. inline void warn(const T &msg)
  211. {
  212. default_logger_raw()->warn(msg);
  213. }
  214. template<typename T>
  215. inline void error(const T &msg)
  216. {
  217. default_logger_raw()->error(msg);
  218. }
  219. template<typename T>
  220. inline void critical(const T &msg)
  221. {
  222. default_logger_raw()->critical(msg);
  223. }
  224. } // namespace spdlog
  225. //
  226. // enable/disable log calls at compile time according to global level.
  227. //
  228. // define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
  229. // SPDLOG_LEVEL_TRACE,
  230. // SPDLOG_LEVEL_DEBUG,
  231. // SPDLOG_LEVEL_INFO,
  232. // SPDLOG_LEVEL_WARN,
  233. // SPDLOG_LEVEL_ERROR,
  234. // SPDLOG_LEVEL_CRITICAL,
  235. // SPDLOG_LEVEL_OFF
  236. //
  237. #ifndef SPDLOG_NO_SOURCE_LOC
  238. # define SPDLOG_LOGGER_CALL(logger, level, ...) \
  239. (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
  240. #else
  241. # define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
  242. #endif
  243. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
  244. # define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
  245. # define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
  246. #else
  247. # define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
  248. # define SPDLOG_TRACE(...) (void)0
  249. #endif
  250. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
  251. # define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
  252. # define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
  253. #else
  254. # define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
  255. # define SPDLOG_DEBUG(...) (void)0
  256. #endif
  257. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
  258. # define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
  259. # define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
  260. #else
  261. # define SPDLOG_LOGGER_INFO(logger, ...) (void)0
  262. # define SPDLOG_INFO(...) (void)0
  263. #endif
  264. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
  265. # define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
  266. # define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
  267. #else
  268. # define SPDLOG_LOGGER_WARN(logger, ...) (void)0
  269. # define SPDLOG_WARN(...) (void)0
  270. #endif
  271. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
  272. # define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
  273. # define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
  274. #else
  275. # define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
  276. # define SPDLOG_ERROR(...) (void)0
  277. #endif
  278. #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
  279. # define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
  280. # define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
  281. #else
  282. # define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
  283. # define SPDLOG_CRITICAL(...) (void)0
  284. #endif
  285. #ifdef SPDLOG_HEADER_ONLY
  286. # include "spdlog-inl.h"
  287. #endif
  288. #endif // SPDLOG_H