Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/spdlog/async.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 //
0007 // Async logging using global thread pool
0008 // All loggers created here share same global thread pool.
0009 // Each log message is pushed to a queue along with a shared pointer to the
0010 // logger.
0011 // If a logger deleted while having pending messages in the queue, it's actual
0012 // destruction will defer
0013 // until all its messages are processed by the thread pool.
0014 // This is because each message in the queue holds a shared_ptr to the
0015 // originating logger.
0016 
0017 #include <spdlog/async_logger.h>
0018 #include <spdlog/details/registry.h>
0019 #include <spdlog/details/thread_pool.h>
0020 
0021 #include <functional>
0022 #include <memory>
0023 #include <mutex>
0024 
0025 namespace spdlog {
0026 
0027 namespace details {
0028 static const size_t default_async_q_size = 8192;
0029 }
0030 
0031 // async logger factory - creates async loggers backed with thread pool.
0032 // if a global thread pool doesn't already exist, create it with default queue
0033 // size of 8192 items and single thread.
0034 template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
0035 struct async_factory_impl {
0036     template <typename Sink, typename... SinkArgs>
0037     static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
0038         auto &registry_inst = details::registry::instance();
0039 
0040         // create global thread pool if not already exists..
0041 
0042         auto &mutex = registry_inst.tp_mutex();
0043         std::lock_guard<std::recursive_mutex> tp_lock(mutex);
0044         auto tp = registry_inst.get_tp();
0045         if (tp == nullptr) {
0046             tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
0047             registry_inst.set_tp(tp);
0048         }
0049 
0050         auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
0051         auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
0052                                                          std::move(tp), OverflowPolicy);
0053         registry_inst.initialize_logger(new_logger);
0054         return new_logger;
0055     }
0056 };
0057 
0058 using async_factory = async_factory_impl<async_overflow_policy::block>;
0059 using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
0060 
0061 template <typename Sink, typename... SinkArgs>
0062 inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name,
0063                                                     SinkArgs &&...sink_args) {
0064     return async_factory::create<Sink>(std::move(logger_name),
0065                                        std::forward<SinkArgs>(sink_args)...);
0066 }
0067 
0068 template <typename Sink, typename... SinkArgs>
0069 inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
0070                                                        SinkArgs &&...sink_args) {
0071     return async_factory_nonblock::create<Sink>(std::move(logger_name),
0072                                                 std::forward<SinkArgs>(sink_args)...);
0073 }
0074 
0075 // set global thread pool.
0076 inline void init_thread_pool(size_t q_size,
0077                              size_t thread_count,
0078                              std::function<void()> on_thread_start,
0079                              std::function<void()> on_thread_stop) {
0080     auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,
0081                                                      on_thread_stop);
0082     details::registry::instance().set_tp(std::move(tp));
0083 }
0084 
0085 inline void init_thread_pool(size_t q_size,
0086                              size_t thread_count,
0087                              std::function<void()> on_thread_start) {
0088     init_thread_pool(q_size, thread_count, on_thread_start, [] {});
0089 }
0090 
0091 inline void init_thread_pool(size_t q_size, size_t thread_count) {
0092     init_thread_pool(
0093         q_size, thread_count, [] {}, [] {});
0094 }
0095 
0096 // get the global thread pool.
0097 inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
0098     return details::registry::instance().get_tp();
0099 }
0100 }  // namespace spdlog