diff --git a/example/example.cpp b/example/example.cpp index 9530aa7a..0e553a96 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -70,18 +70,19 @@ int main(int, char*[]) // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. async_example(); - // syslog example. linux/osx only.. + // syslog example. linux/osx only syslog_example(); - // log user-defined types example.. + // Log user-defined types example user_defined_example(); // Change default log error handler err_handler_example(); + + // Apply a function on all registered loggers + spd::apply_all([&](std::shared_ptr l) {l->info("End of example."); }); - console->info("End of example. bye.."); - - // Release and close all loggers + // Release and close all loggers spdlog::drop_all(); } // Exceptions will only be thrown upon failed logger or sink construction (not during logging) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index c73884f7..4f5a0893 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -71,6 +71,13 @@ public: return new_logger; } + void apply_all(std::function)> fun) + { + std::lock_guard lock(_mutex); + for (auto &l : _loggers) + fun(l.second); + } + void drop(const std::string& logger_name) { std::lock_guard lock(_mutex); diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 2c6ebb45..f179ac33 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -153,8 +153,6 @@ inline void spdlog::set_error_handler(log_err_handler handler) } - - inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function& worker_teardown_cb) { details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb); @@ -165,6 +163,11 @@ inline void spdlog::set_sync_mode() details::registry::instance().set_sync_mode(); } +inline void spdlog::apply_all(std::function)> fun) +{ + details::registry::instance().apply_all(fun); +} + inline void spdlog::drop_all() { details::registry::instance().drop_all(); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 8c936c7e..82852de6 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -113,7 +113,8 @@ std::shared_ptr create(const std::string& logger_name, const It& sinks_b // Create and register a logger with templated sink type -// Example: spdlog::create("mylog", "dailylog_filename", "txt"); +// Example: +// spdlog::create("mylog", "dailylog_filename", "txt"); template std::shared_ptr create(const std::string& logger_name, Args...); @@ -121,10 +122,15 @@ std::shared_ptr create(const std::string& logger_name, Args...); // Register the given logger with the given name void register_logger(std::shared_ptr logger); +// Apply a user defined function on all registered loggers +// Example: +// spdlog::apply_all([&](std::shared_ptr l) {l->flush();}); +void apply_all(std::function)> fun); + // Drop the reference to the given logger void drop(const std::string &name); -// Drop all references +// Drop all references from the registry void drop_all();