diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 66536cb4..5f708743 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -45,9 +45,10 @@ class registry { public: - void register_logger(std::shared_ptr logger, const std::string& logger_name) + void register_logger(std::shared_ptr logger) { - _loggers[logger_name] = logger; + std::lock_guard lock(_mutex); + register_logger_impl(logger); } @@ -61,12 +62,12 @@ public: template std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) { - std::lock_guard lock(_mutex); - //If already exists, just return it - auto found = _loggers.find(logger_name); - if (found != _loggers.end()) - return found->second; + std::shared_ptr new_logger; + + std::lock_guard lock(_mutex); + + if (_async_mode) new_logger = std::make_shared(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb); else @@ -74,9 +75,9 @@ public: if (_formatter) new_logger->set_formatter(_formatter); - new_logger->set_level(_level); - register_logger(new_logger, logger_name); + new_logger->set_level(_level); + register_logger_impl(new_logger); return new_logger; } @@ -148,6 +149,13 @@ public: } private: + void register_logger_impl(std::shared_ptr logger) + { + auto logger_name = logger->name(); + if (_loggers.find(logger_name) != std::end(_loggers)) + throw spdlog_ex("logger with name " + logger_name + " already exists"); + _loggers[logger->name()] = logger; + } registry() = default; registry(const registry&) = delete; registry& operator=(const registry&) = delete; diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 05e9c7b8..76843d71 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -32,9 +32,9 @@ #include "../sinks/stdout_sinks.h" #include "../sinks/syslog_sink.h" -inline void spdlog::register_logger(std::shared_ptr logger, const std::string& logger_name) +inline void spdlog::register_logger(std::shared_ptr logger) { - return details::registry::instance().register_logger(logger, logger_name); + return details::registry::instance().register_logger(logger); } inline std::shared_ptr spdlog::get(const std::string& name) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index d195e1ed..8601555f 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -116,7 +116,7 @@ std::shared_ptr create(const std::string& logger_name, const Arg // Register the given logger with the given name -void register_logger(std::shared_ptr logger, const std::string& logger_name); +void register_logger(std::shared_ptr logger); // Drop the reference to the given logger void drop(const std::string &name);