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