From 5d7845c13891effcd1bc921540529c2fb23d74ae Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 25 Aug 2018 17:55:31 +0300 Subject: [PATCH] Added "clone()" support to loggers --- README.md | 12 ++++++++++++ example/example.cpp | 2 +- include/spdlog/details/async_logger_impl.h | 6 +----- include/spdlog/details/fmt_helper.h | 4 ++-- include/spdlog/details/logger_impl.h | 6 ++---- include/spdlog/logger.h | 1 - tests/includes.h | 2 +- tests/test_mpmc_q.cpp | 5 ++--- 8 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ca797e14..89a7cddd 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,18 @@ void daily_example() --- #### Periodic flush ```c++ +// clone a logger and give it new name. +// Useful for creating subsystem loggers from some "root" logger +void clone_example() +{ + auto network_logger = spdlog::get("console")->clone("network"); + network_logger->info("Logging network stuff.."); +} +``` + +--- +#### Cloning loggers for +```c++ // periodically flush all *registered* loggers every 3 seconds: // warning: only use if all your loggers are thread safe! spdlog::flush_every(std::chrono::seconds(3)); diff --git a/example/example.cpp b/example/example.cpp index cfdc4e4e..b7221447 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -59,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; diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 27344815..b6e86460 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -99,13 +99,9 @@ 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_); + 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()); diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 76a00b4c..1d84e5eb 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -22,8 +22,8 @@ inline void append_str(const std::string &str, fmt::basic_memory_buffer inline void append_c_str(const char *c_str, fmt::basic_memory_buffer &dest) { - auto len = std::char_traits::length(c_str); - dest.append(c_str, c_str + len); + auto len = std::char_traits::length(c_str); + dest.append(c_str, c_str + len); } template diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 82d901da..76daac35 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -10,7 +10,6 @@ #include #include - // create logger with given name, sinks and the default pattern formatter // all other ctors will call this one template @@ -79,8 +78,8 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg) } try { - details::log_msg log_msg(&name_, lvl); - details::fmt_helper::append_c_str(msg, log_msg.raw); + details::log_msg log_msg(&name_, lvl); + details::fmt_helper::append_c_str(msg, log_msg.raw); sink_it_(log_msg); } SPDLOG_CATCH_AND_HANDLE @@ -276,7 +275,6 @@ 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); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index b3dcb7ae..aa524f70 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -115,7 +115,6 @@ 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); diff --git a/tests/includes.h b/tests/includes.h index 4d35d6b8..bce67418 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include #include #define SPDLOG_TRACE_ON diff --git a/tests/test_mpmc_q.cpp b/tests/test_mpmc_q.cpp index ddb31961..7c56496d 100644 --- a/tests/test_mpmc_q.cpp +++ b/tests/test_mpmc_q.cpp @@ -2,12 +2,11 @@ using namespace std::chrono; using std::chrono::milliseconds; -using test_clock = std::chrono::high_resolution_clock ; - +using test_clock = std::chrono::high_resolution_clock; static milliseconds millis_from(const test_clock::time_point &tp0) { - return std::chrono::duration_cast(test_clock::now()-tp0); + return std::chrono::duration_cast(test_clock::now() - tp0); } TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]") {