mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 18:41:35 +08:00
check if logger already exists in the registry before creating it
This commit is contained in:
parent
becef922c2
commit
2f8e22d828
@ -33,7 +33,9 @@ public:
|
|||||||
void register_logger(std::shared_ptr<logger> logger)
|
void register_logger(std::shared_ptr<logger> logger)
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
register_logger_impl(logger);
|
auto logger_name = logger->name();
|
||||||
|
throw_if_exists(logger_name);
|
||||||
|
_loggers[logger_name] = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -47,12 +49,9 @@ public:
|
|||||||
template<class It>
|
template<class It>
|
||||||
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::shared_ptr<logger> new_logger;
|
|
||||||
|
|
||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
|
throw_if_exists(logger_name);
|
||||||
|
std::shared_ptr<logger> new_logger;
|
||||||
if (_async_mode)
|
if (_async_mode)
|
||||||
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms);
|
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms);
|
||||||
else
|
else
|
||||||
@ -62,7 +61,8 @@ public:
|
|||||||
new_logger->set_formatter(_formatter);
|
new_logger->set_formatter(_formatter);
|
||||||
|
|
||||||
new_logger->set_level(_level);
|
new_logger->set_level(_level);
|
||||||
register_logger_impl(new_logger);
|
//Add to registry
|
||||||
|
_loggers[logger_name] = new_logger;
|
||||||
return new_logger;
|
return new_logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,16 +135,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void register_logger_impl(std::shared_ptr<logger> 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_t<Mutex>() {}
|
registry_t<Mutex>() {}
|
||||||
registry_t<Mutex>(const registry_t<Mutex>&) = delete;
|
registry_t<Mutex>(const registry_t<Mutex>&) = delete;
|
||||||
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
|
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = 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;
|
Mutex _mutex;
|
||||||
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
||||||
formatter_ptr _formatter;
|
formatter_ptr _formatter;
|
||||||
|
Loading…
Reference in New Issue
Block a user