diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index f225e6c9..78a47fec 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -33,7 +33,9 @@ public: void register_logger(std::shared_ptr logger) { std::lock_guard lock(_mutex); - register_logger_impl(logger); + auto logger_name = logger->name(); + throw_if_exists(logger_name); + _loggers[logger_name] = logger; } @@ -46,13 +48,10 @@ public: template std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) - { - - std::shared_ptr new_logger; - + { std::lock_guard lock(_mutex); - - + throw_if_exists(logger_name); + std::shared_ptr new_logger; if (_async_mode) new_logger = std::make_shared(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms); else @@ -62,7 +61,8 @@ public: new_logger->set_formatter(_formatter); new_logger->set_level(_level); - register_logger_impl(new_logger); + //Add to registry + _loggers[logger_name] = new_logger; return new_logger; } @@ -134,17 +134,16 @@ public: return s_instance; } -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; - } +private: registry_t() {} registry_t(const registry_t&) = delete; registry_t& operator=(const registry_t&) = delete; + + void throw_if_exists(const std::string &logger_name) + { + if (_loggers.find(logger_name) != _loggers.end()) + throw spdlog_ex("logger with name '" + logger_name + "' already exists"); + } Mutex _mutex; std::unordered_map > _loggers; formatter_ptr _formatter;