From 623f59ce6fe72e33c9f6bc2b5c2bcd1ae330d482 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 11 Apr 2015 16:36:31 +0300 Subject: [PATCH] Added register_logger to spdlog.h to register manually created loggers --- include/spdlog/details/registry.h | 11 ++++++++++- include/spdlog/details/spdlog_impl.h | 5 +++++ include/spdlog/spdlog.h | 13 ++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 7a283ecd..66536cb4 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -44,6 +44,13 @@ namespace details class registry { public: + + void register_logger(std::shared_ptr logger, const std::string& logger_name) + { + _loggers[logger_name] = logger; + } + + std::shared_ptr get(const std::string& logger_name) { std::lock_guard lock(_mutex); @@ -68,7 +75,8 @@ public: if (_formatter) new_logger->set_formatter(_formatter); new_logger->set_level(_level); - _loggers[logger_name] = new_logger; + register_logger(new_logger, logger_name); + return new_logger; } @@ -93,6 +101,7 @@ public: return create(logger_name, { sink }); } + void formatter(formatter_ptr f) { std::lock_guard lock(_mutex); diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 34805597..05e9c7b8 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -32,6 +32,11 @@ #include "../sinks/stdout_sinks.h" #include "../sinks/syslog_sink.h" +inline void spdlog::register_logger(std::shared_ptr logger, const std::string& logger_name) +{ + return details::registry::instance().register_logger(logger, logger_name); +} + inline std::shared_ptr spdlog::get(const std::string& name) { return details::registry::instance().get(name); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 73ea63a9..d195e1ed 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -74,7 +74,7 @@ void set_async_mode(size_t queue_size, const async_overflow_policy overflow_poli void set_sync_mode(); // -// Create multi/single threaded rotating file logger +// Create and register multi/single threaded rotating file logger // std::shared_ptr rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); std::shared_ptr rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); @@ -87,7 +87,7 @@ std::shared_ptr daily_logger_st(const std::string& logger_name, const st // -// Create stdout/stderr loggers +// Create and register stdout/stderr loggers // std::shared_ptr stdout_logger_mt(const std::string& logger_name); std::shared_ptr stdout_logger_st(const std::string& logger_name); @@ -96,25 +96,28 @@ std::shared_ptr stderr_logger_st(const std::string& logger_name); // -// Create a syslog logger +// Create and register a syslog logger // #ifdef __linux__ std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0); #endif -// Create a logger with multiple sinks +// Create and register a logger with multiple sinks std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks); template std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end); -// Create a logger with templated sink type +// Create and register a logger with templated sink type // Example: spdlog::create("mylog", "dailylog_filename", "txt"); template std::shared_ptr create(const std::string& logger_name, const Args&...); +// Register the given logger with the given name +void register_logger(std::shared_ptr logger, const std::string& logger_name); + // Drop the reference to the given logger void drop(const std::string &name);