From 91d8869f36195c062a07ea80bb3354f5195e6bf6 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 25 Aug 2018 17:35:20 +0300 Subject: [PATCH] Added "clone()" support to loggers --- example/example.cpp | 13 ++++++++++++- include/spdlog/async_logger.h | 2 ++ include/spdlog/details/async_logger_impl.h | 14 ++++++++++++++ include/spdlog/details/logger_impl.h | 15 +++++++++++++++ include/spdlog/logger.h | 9 ++++++++- 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 4222c829..cfdc4e4e 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -18,6 +18,7 @@ void multi_sink_example(); void user_defined_example(); void err_handler_example(); void syslog_example(); +void clone_example(); #include "spdlog/spdlog.h" @@ -34,6 +35,8 @@ int main(int, char *[]) rotating_example(); daily_example(); + clone_example(); + // async logging using a backing thread pool async_example(); @@ -56,7 +59,7 @@ int main(int, char *[]) // release any threads created by spdlog, and drop all loggers in the registry. spdlog::shutdown(); } - // Exceptions will only be thrown upon failed logger or sink construction (not during logging) + // Exceptions will only be thrown upon failed logger or sink construction (not during logging) catch (const spdlog::spdlog_ex &ex) { std::cout << "Log init failed: " << ex.what() << std::endl; @@ -122,6 +125,14 @@ void daily_example() auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); } +// clone a logger and give it new name. +// Useful for creating component/subsystem loggers from some "root" logger +void clone_example() +{ + auto network_logger = spdlog::get("console")->clone("network"); + network_logger->info("Logging network stuff.."); +} + #include "spdlog/async.h" void async_example() { diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 3250f4ad..690b1c6a 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -55,6 +55,8 @@ public: async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr tp, async_overflow_policy overflow_policy = async_overflow_policy::block); + std::shared_ptr clone(std::string new_name) override; + protected: void sink_it_(details::log_msg &msg) override; void flush_() override; diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 47659ddc..27344815 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -98,3 +98,17 @@ inline void spdlog::async_logger::backend_flush_() } SPDLOG_CATCH_AND_HANDLE } + + +inline std::shared_ptr spdlog::async_logger::clone(std::string new_name) +{ + auto cloned = std::make_shared(std::move(new_name), + sinks_.begin(), sinks_.end(), + thread_pool_, + overflow_policy_); + + cloned->set_level(this->level()); + cloned->flush_on(this->flush_level()); + cloned->set_error_handler(this->error_handler()); + return std::move(cloned); +} \ No newline at end of file diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 1451adc6..82d901da 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -271,6 +271,12 @@ inline void spdlog::logger::flush_on(level::level_enum log_level) flush_level_.store(log_level); } +inline spdlog::level::level_enum spdlog::logger::flush_level() const +{ + return static_cast(flush_level_.load(std::memory_order_relaxed)); +} + + inline bool spdlog::logger::should_flush_(const details::log_msg &msg) { auto flush_level = flush_level_.load(std::memory_order_relaxed); @@ -346,3 +352,12 @@ inline std::vector &spdlog::logger::sinks() { return sinks_; } + +inline std::shared_ptr spdlog::logger::clone(std::string logger_name) +{ + auto cloned = std::make_shared(std::move(logger_name), sinks_.begin(), sinks_.end()); + cloned->set_level(this->level()); + cloned->flush_on(this->flush_level()); + cloned->set_error_handler(this->error_handler()); + return std::move(cloned); +} \ No newline at end of file diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 91eec4bc..b3dcb7ae 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -115,21 +115,28 @@ public: level::level_enum level() const; const std::string &name() const; + // set formatting for the sinks in this logger. // each sink will get a seperate instance of the formatter object. void set_formatter(std::unique_ptr formatter); void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); + // flush functions void flush(); void flush_on(level::level_enum log_level); + level::level_enum flush_level() const; + // sinks const std::vector &sinks() const; - std::vector &sinks(); + // error handler void set_error_handler(log_err_handler err_handler); log_err_handler error_handler(); + // create new logger with same sinks and configuration. + virtual std::shared_ptr clone(std::string logger_name); + protected: virtual void sink_it_(details::log_msg &msg); virtual void flush_();