From 8ee90d3349874a38a8a596aa56b3f87a0f8eca45 Mon Sep 17 00:00:00 2001 From: Carsten Neumann Date: Thu, 15 Jun 2017 11:08:44 -0500 Subject: [PATCH] Add create_async factory functions for async loggers --- include/spdlog/details/registry.h | 29 ++++++++++++++++++++++++++++ include/spdlog/details/spdlog_impl.h | 18 +++++++++++++++++ include/spdlog/spdlog.h | 9 ++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index edba44bf..039cd0d3 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -71,6 +71,26 @@ public: return new_logger; } + template + std::shared_ptr create_async(const std::string& logger_name, 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, const It& sinks_begin, const It& sinks_end) + { + std::lock_guard lock(_mutex); + throw_if_exists(logger_name); + auto new_logger = std::make_shared(logger_name, sinks_begin, sinks_end, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb); + + if (_formatter) + new_logger->set_formatter(_formatter); + + if (_err_handler) + new_logger->set_error_handler(_err_handler); + + new_logger->set_level(_level); + + //Add to registry + _loggers[logger_name] = new_logger; + return new_logger; + } + void apply_all(std::function)> fun) { std::lock_guard lock(_mutex); @@ -99,6 +119,15 @@ public: return create(logger_name, { sink }); } + std::shared_ptr create_async(const std::string& logger_name, 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, sinks_init_list sinks) + { + return create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks.begin(), sinks.end()); + } + + std::shared_ptr create_async(const std::string& logger_name, 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, sink_ptr sink) + { + return create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, { sink }); + } void formatter(formatter_ptr f) { diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 6999eed0..7fe9ab40 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -203,6 +203,24 @@ inline std::shared_ptr spdlog::create(const std::string& logger_ return details::registry::instance().create(logger_name, sinks_begin, sinks_end); } +// Create and register an async logger with a single sink +inline std::shared_ptr spdlog::create_async(const std::string& logger_name, const sink_ptr& sink, 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) +{ + return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sink); +} + +// Create and register an async logger with multiple sinks +inline std::shared_ptr spdlog::create_async(const std::string& logger_name, sinks_init_list sinks, 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 ) +{ + return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks); +} + +template +inline std::shared_ptr spdlog::create_async(const std::string& logger_name, const It& sinks_begin, const It& sinks_end, 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) +{ + return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks_begin, sinks_end); +} + inline void spdlog::set_formatter(spdlog::formatter_ptr f) { details::registry::instance().formatter(f); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index aeca50ed..9170f1d2 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -113,7 +113,7 @@ std::shared_ptr syslog_logger(const std::string& logger_name, const std: std::shared_ptr android_logger(const std::string& logger_name, const std::string& tag = "spdlog"); #endif -// Create and register a logger a single sink +// Create and register a logger with a single sink std::shared_ptr create(const std::string& logger_name, const sink_ptr& sink); // Create and register a logger with multiple sinks @@ -128,6 +128,13 @@ std::shared_ptr create(const std::string& logger_name, const It& sinks_b template std::shared_ptr create(const std::string& logger_name, Args...); +// Create and register an async logger with a single sink +std::shared_ptr create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function& worker_teardown_cb = nullptr); + +// Create and register an async logger with multiple sinks +std::shared_ptr create_async(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function& worker_teardown_cb = nullptr); +template +std::shared_ptr create_async(const std::string& logger_name, const It& sinks_begin, const It& sinks_end, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function& worker_teardown_cb = nullptr); // Register the given logger with the given name void register_logger(std::shared_ptr logger);