Fixed registry functions

This commit is contained in:
gabime 2015-04-11 17:15:20 +03:00
parent 623f59ce6f
commit 256c64d72f
3 changed files with 20 additions and 12 deletions

View File

@ -45,9 +45,10 @@ class registry
{ {
public: public:
void register_logger(std::shared_ptr<logger> logger, const std::string& logger_name) void register_logger(std::shared_ptr<logger> logger)
{ {
_loggers[logger_name] = logger; std::lock_guard<std::mutex> lock(_mutex);
register_logger_impl(logger);
} }
@ -61,12 +62,12 @@ 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::lock_guard<std::mutex> lock(_mutex);
//If already exists, just return it
auto found = _loggers.find(logger_name);
if (found != _loggers.end())
return found->second;
std::shared_ptr<logger> new_logger; std::shared_ptr<logger> new_logger;
std::lock_guard<std::mutex> lock(_mutex);
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); new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb);
else else
@ -74,9 +75,9 @@ public:
if (_formatter) if (_formatter)
new_logger->set_formatter(_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; return new_logger;
} }
@ -148,6 +149,13 @@ 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() = default; registry() = default;
registry(const registry&) = delete; registry(const registry&) = delete;
registry& operator=(const registry&) = delete; registry& operator=(const registry&) = delete;

View File

@ -32,9 +32,9 @@
#include "../sinks/stdout_sinks.h" #include "../sinks/stdout_sinks.h"
#include "../sinks/syslog_sink.h" #include "../sinks/syslog_sink.h"
inline void spdlog::register_logger(std::shared_ptr<logger> logger, const std::string& logger_name) inline void spdlog::register_logger(std::shared_ptr<logger> logger)
{ {
return details::registry::instance().register_logger(logger, logger_name); return details::registry::instance().register_logger(logger);
} }
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name) inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)

View File

@ -116,7 +116,7 @@ std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Arg
// Register the given logger with the given name // Register the given logger with the given name
void register_logger(std::shared_ptr<logger> logger, const std::string& logger_name); void register_logger(std::shared_ptr<logger> logger);
// Drop the reference to the given logger // Drop the reference to the given logger
void drop(const std::string &name); void drop(const std::string &name);