From 8af91bcda0106a17bb2a6ab72b990e8cecac8ff2 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Nov 2024 13:15:37 +0200 Subject: [PATCH] Added registry::set_instance() and shared_instance() --- include/spdlog/details/registry-inl.h | 21 +++++++++++++++++---- include/spdlog/details/registry.h | 8 +++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index e09cb180..39da4700 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -26,10 +26,27 @@ #include #include #include +#include + namespace spdlog { namespace details { + +SPDLOG_INLINE static std::shared_ptr s_instance = std::make_shared(); + +SPDLOG_INLINE registry ®istry::instance() { + return *s_instance; +} + +SPDLOG_INLINE std::shared_ptr registry::shared_instance() { + return s_instance; +} + +SPDLOG_INLINE void registry::set_instance(std::shared_ptr instance) { + s_instance = instance; +} + SPDLOG_INLINE registry::registry() : formatter_(new pattern_formatter()) { #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER @@ -233,10 +250,6 @@ SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *gl } } -SPDLOG_INLINE registry ®istry::instance() { - static registry s_instance; - return s_instance; -} SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr new_logger) { std::lock_guard lock(logger_map_mutex_); diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index f78708cd..5fd7506c 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -26,6 +26,9 @@ class thread_pool; class SPDLOG_API registry { public: + registry(); + ~registry(); + using log_levels = std::unordered_map; registry(const registry &) = delete; registry &operator=(const registry &) = delete; @@ -95,16 +98,15 @@ public: // set levels for all existing/future loggers. global_level can be null if should not set. void set_levels(log_levels levels, level::level_enum *global_level); + static void set_instance(std::shared_ptr instance); static registry &instance(); + static std::shared_ptr shared_instance(); void apply_logger_env_levels(std::shared_ptr new_logger); std::mutex& console_mutex(); private: - registry(); - ~registry(); - void throw_if_exists_(const std::string &logger_name); void register_logger_(std::shared_ptr new_logger); bool set_level_from_cfg_(logger *logger);