diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index fd8e4be9..3fd64930 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -144,13 +144,23 @@ public: _async_mode = false; } - static registry_t& instance() + static std::shared_ptr> instance() { - static registry_t s_instance; - return s_instance; + if(_instance == nullptr) + _instance = std::make_shared>(); + return _instance; } + static void set_registry(std::shared_ptr> registry) + { + _instance = registry; + } + + registry_t() {} + private: + static std::shared_ptr> _instance; + void register_logger_impl(std::shared_ptr logger) { auto logger_name = logger->name(); @@ -158,7 +168,6 @@ private: throw spdlog_ex("logger with name " + logger_name + " already exists"); _loggers[logger->name()] = logger; } - registry_t(){} registry_t(const registry_t&) = delete; registry_t& operator=(const registry_t&) = delete; Mutex _mutex; @@ -171,10 +180,14 @@ private: std::function _worker_warmup_cb = nullptr; std::chrono::milliseconds _flush_interval_ms; }; + +template +std::shared_ptr> registry_t::_instance = nullptr; + #ifdef SPDLOG_NO_REGISTRY_MUTEX typedef registry_t registry; -#else +# else typedef registry_t registry; -#endif +#endif //SPDLOG_NO_REGISTRY_MUTEX } } diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index cfd6f826..cdbe1a94 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -34,17 +34,17 @@ inline void spdlog::register_logger(std::shared_ptr logger) { - return details::registry::instance().register_logger(logger); + return details::registry::instance()->register_logger(logger); } inline std::shared_ptr spdlog::get(const std::string& name) { - return details::registry::instance().get(name); + return details::registry::instance()->get(name); } inline void spdlog::drop(const std::string &name) { - details::registry::instance().drop(name); + details::registry::instance()->drop(name); } // Create multi/single threaded rotating file logger @@ -72,22 +72,22 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string // Create stdout/stderr loggers inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); } inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_st::instance()); } inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); } inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_st::instance()); } #ifdef __linux__ @@ -103,7 +103,7 @@ inline std::shared_ptr spdlog::syslog_logger(const std::string& inline std::shared_ptr spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) { - return details::registry::instance().create(logger_name, sinks); + return details::registry::instance()->create(logger_name, sinks); } @@ -111,44 +111,44 @@ template inline std::shared_ptr spdlog::create(const std::string& logger_name, const Args&... args) { sink_ptr sink = std::make_shared(args...); - return details::registry::instance().create(logger_name, { sink }); + return details::registry::instance()->create(logger_name, { sink }); } template inline std::shared_ptr spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) { - return details::registry::instance().create(logger_name, sinks_begin, sinks_end); + return details::registry::instance()->create(logger_name, sinks_begin, sinks_end); } inline void spdlog::set_formatter(spdlog::formatter_ptr f) { - details::registry::instance().formatter(f); + details::registry::instance()->formatter(f); } inline void spdlog::set_pattern(const std::string& format_string) { - return details::registry::instance().set_pattern(format_string); + return details::registry::instance()->set_pattern(format_string); } inline void spdlog::set_level(level::level_enum log_level) { - return details::registry::instance().set_level(log_level); + return details::registry::instance()->set_level(log_level); } inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms) { - details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); + details::registry::instance()->set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); } inline void spdlog::set_sync_mode() { - details::registry::instance().set_sync_mode(); + details::registry::instance()->set_sync_mode(); } inline void spdlog::drop_all() { - details::registry::instance().drop_all(); + details::registry::instance()->drop_all(); }