From 805aafe338c115767560f9d61d7183d4a6671464 Mon Sep 17 00:00:00 2001 From: rlewicki Date: Fri, 12 Oct 2018 20:17:16 +0200 Subject: [PATCH] Added optional default logger setup possibility User can now add default logger passed to set_default function and then use it across code base by just calling get_default function. This helps to avoid common typos when accessing logger via get function. --- example/example.cpp | 10 ++++++++++ include/spdlog/details/registry.h | 11 +++++++++++ include/spdlog/spdlog.h | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/example/example.cpp b/example/example.cpp index 372ca14c..d7a5e2da 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -20,6 +20,7 @@ void user_defined_example(); void err_handler_example(); void syslog_example(); void clone_example(); +void default_logger_example(); #include "spdlog/spdlog.h" @@ -38,6 +39,8 @@ int main(int, char *[]) clone_example(); + default_logger_example(); + // async logging using a backing thread pool async_example(); @@ -138,6 +141,13 @@ void clone_example() network_logger->info("Logging network stuff.."); } +void default_logger_example() +{ + auto logger = spdlog::get("console"); + spdlog::set_default(logger); + spdlog::get_default()->warn("Default logger message..."); +} + #include "spdlog/async.h" void async_example() { diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 63baf748..c7015b97 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -72,6 +72,16 @@ public: tp_ = std::move(tp); } + void set_default(std::shared_ptr logger) + { + default_logger_ = std::move(logger); + } + + std::shared_ptr get_default() + { + return default_logger_; + } + std::shared_ptr get_tp() { std::lock_guard lock(tp_mutex_); @@ -202,6 +212,7 @@ private: std::mutex logger_map_mutex_, flusher_mutex_; std::recursive_mutex tp_mutex_; std::unordered_map> loggers_; + std::shared_ptr default_logger_; std::unique_ptr formatter_; level::level_enum level_ = level::info; level::level_enum flush_level_ = level::off; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 40640ab9..81501d1f 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -54,6 +54,23 @@ inline std::shared_ptr get(const std::string &name) return details::registry::instance().get(name); } +// Return default logger instance when no explicitly defined +// example: spdlog::get_default()->info("hello {}", "world"); +inline std::shared_ptr get_default() +{ + return details::registry::instance().get_default(); +} + +// Sets logger passed in parameter as default logger that will be returned +// when calling get_default() function +// example: +// auto logger = spdlog::create("logger_name", "dailylog_filename", 11, 59); +// spdlog::set_default(logger); +inline void set_default(std::shared_ptr logger) +{ + details::registry::instance().set_default(std::move(logger)); +} + // Set global formatter. Each sink in each logger will get a clone of this object inline void set_formatter(std::unique_ptr formatter) {