From 9cbdd5ffd4b5db9609be608cf16463f85cabf57e Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 20 Jul 2018 23:20:48 +0300 Subject: [PATCH] Added async_nonblocking factory --- include/spdlog/async.h | 17 +++++++++++++++-- include/spdlog/async_logger.h | 8 ++++++++ include/spdlog/common.h | 9 +-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index 3aab7a89..d3e61505 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -21,9 +21,11 @@ #include namespace spdlog { + // async logger factory - creates async loggers backed with thread pool. // if a global thread pool doesn't already exist, create it with default queue size of 8192 items and single thread. -struct async_factory +template +struct async_factory_impl { template static std::shared_ptr create(const std::string &logger_name, SinkArgs &&... args) @@ -39,18 +41,29 @@ struct async_factory } auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(logger_name, std::move(sink), std::move(tp), async_overflow_policy::block); + auto new_logger = std::make_shared(logger_name, std::move(sink), std::move(tp), OverflowPolicy); registry::instance().register_and_init(new_logger); return new_logger; } }; +using async_factory = async_factory_impl; +using async_factory_nonblock = async_factory_impl < async_overflow_policy::overrun_oldest>; + + template inline std::shared_ptr create_async(const std::string &logger_name, SinkArgs &&... sink_args) { return async_factory::create(logger_name, std::forward(sink_args)...); } +template +inline std::shared_ptr create_async_nb(const std::string &logger_name, SinkArgs &&... sink_args) +{ + return async_factory::create(logger_name, std::forward(sink_args)...); +} + + // set global thread pool. inline void init_thread_pool(size_t q_size, size_t thread_count) { diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 4a6eb768..7d221aa3 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -23,6 +23,14 @@ #include namespace spdlog { + +// Async overflow policy - block by default. +enum class async_overflow_policy +{ + block, // Block until message can be enqueued + overrun_oldest // Discard oldest message in the queue if full when trying to add new item. +}; + namespace details { class thread_pool; } diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 8adc3426..c36410b4 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -121,14 +121,7 @@ inline spdlog::level::level_enum from_str(const std::string &name) using level_hasher = std::hash; } // namespace level -// -// Async overflow policy - block by default. -// -enum class async_overflow_policy -{ - block, // Block until message can be enqueued - overrun_oldest // Discard oldest message in the queue if full when trying to add new item. -}; + // // Pattern time - specific time getting to use for pattern_formatter.