diff --git a/include/spdlog/details/context.h b/include/spdlog/details/context.h index 809418e0..d6b1c7ef 100644 --- a/include/spdlog/details/context.h +++ b/include/spdlog/details/context.h @@ -24,6 +24,8 @@ class thread_pool; class SPDLOG_API context { public: static context &instance(); + context(); + ~context(); context(const context &) = delete; context &operator=(const context &) = delete; @@ -45,13 +47,10 @@ public: // clean all resources void shutdown(); - std::recursive_mutex &tp_mutex(); -private: - context(); - ~context(); +private: std::recursive_mutex tp_mutex_; std::shared_ptr tp_; std::shared_ptr global_logger_; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 3b9197cf..f27528b1 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -23,6 +23,10 @@ namespace spdlog { using default_factory = synchronous_factory; +void set_context(std::shared_ptr context); +std::shared_ptr context(); +const std::shared_ptr &context_ref(); + // Create a logger with a templated sink type // Example: // spdlog::create("logger_name", "dailylog_filename", 11, 59); @@ -75,23 +79,19 @@ SPDLOG_API std::shared_ptr global_logger(); SPDLOG_API void set_global_logger(std::shared_ptr global_logger); -// Return the raw pointer of the global logger -SPDLOG_API logger *global_logger_raw(); - - template void log(source_loc source, level lvl, format_string_t fmt, Args &&...args) { - global_logger_raw()->log(source, lvl, fmt, std::forward(args)...); + global_logger()->log(source, lvl, fmt, std::forward(args)...); } template void log(level lvl, format_string_t fmt, Args &&...args) { - global_logger_raw()->log(lvl, fmt, std::forward(args)...); + global_logger()->log(lvl, fmt, std::forward(args)...); } -inline void log(level lvl, std::string_view msg) { global_logger_raw()->log(lvl, msg); } +inline void log(level lvl, std::string_view msg) { global_logger()->log(lvl, msg); } -inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger_raw()->log(loc, lvl, msg); } +inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger()->log(loc, lvl, msg); } template void trace(format_string_t fmt, Args &&...args) { @@ -168,7 +168,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); } #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG #define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__) - #define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::global_logger_raw(), __VA_ARGS__) + #define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::global_logger(), __VA_ARGS__) #else #define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0 #define SPDLOG_DEBUG(...) (void)0 @@ -176,7 +176,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); } #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO #define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__) - #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::global_logger_raw(), __VA_ARGS__) + #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::global_logger(), __VA_ARGS__) #else #define SPDLOG_LOGGER_INFO(logger, ...) (void)0 #define SPDLOG_INFO(...) (void)0 @@ -184,7 +184,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); } #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN #define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__) - #define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::global_logger_raw(), __VA_ARGS__) + #define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::global_logger(), __VA_ARGS__) #else #define SPDLOG_LOGGER_WARN(logger, ...) (void)0 #define SPDLOG_WARN(...) (void)0 @@ -192,7 +192,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); } #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR #define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__) - #define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::global_logger_raw(), __VA_ARGS__) + #define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::global_logger(), __VA_ARGS__) #else #define SPDLOG_LOGGER_ERROR(logger, ...) (void)0 #define SPDLOG_ERROR(...) (void)0 @@ -200,7 +200,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); } #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL #define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) - #define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::global_logger_raw(), __VA_ARGS__) + #define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::global_logger(), __VA_ARGS__) #else #define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0 #define SPDLOG_CRITICAL(...) (void)0 diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 718785ae..12ef5a30 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -12,34 +12,39 @@ namespace spdlog { +static std::shared_ptr s_context = std::make_unique(); -std::shared_ptr global_logger() { return details::context::instance().global_logger(); } +void set_context(std::shared_ptr context) { s_context = std::move(context); } -logger *global_logger_raw() { return details::context::instance().global_logger_raw(); } +std::shared_ptr context() {return s_context;} + +const std::shared_ptr &context_ref() {return s_context;} + +std::shared_ptr global_logger() { return context()->global_logger(); } void set_global_logger(std::shared_ptr global_logger) { - details::context::instance().set_logger(std::move(global_logger)); + context()->set_logger(std::move(global_logger)); } void set_formatter(std::unique_ptr formatter) { - global_logger_raw()->set_formatter(std::move(formatter)); + global_logger()->set_formatter(std::move(formatter)); } void set_pattern(std::string pattern, pattern_time_type time_type) { set_formatter(std::make_unique(std::move(pattern), time_type)); } -level get_level() { return global_logger_raw()->log_level(); } +level get_level() { return global_logger()->log_level(); } -bool should_log(level level) { return global_logger_raw()->should_log(level); } +bool should_log(level level) { return global_logger()->should_log(level); } -void set_level(level level) { global_logger_raw()->set_level(level); } +void set_level(level level) { global_logger()->set_level(level); } -void flush_on(level level) { global_logger_raw()->flush_on(level); } +void flush_on(level level) { global_logger()->flush_on(level); } -void set_error_handler(void (*handler)(const std::string &msg)) { global_logger_raw()->set_error_handler(handler); } +void set_error_handler(void (*handler)(const std::string &msg)) { global_logger()->set_error_handler(handler); } -void shutdown() { details::context::instance().shutdown(); } +void shutdown() { s_context.reset();} } // namespace spdlog