Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:43

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 <memory>
0022 #include <mutex>
0023 #include <functional>
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 {
0037     template<typename Sink, typename... SinkArgs>
0038     static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
0039     {
0040         auto &registry_inst = details::registry::instance();
0041 
0042         // create global thread pool if not already exists..
0043 
0044         auto &mutex = registry_inst.tp_mutex();
0045         std::lock_guard<std::recursive_mutex> tp_lock(mutex);
0046         auto tp = registry_inst.get_tp();
0047         if (tp == nullptr)
0048         {
0049             tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
0050             registry_inst.set_tp(tp);
0051         }
0052 
0053         auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
0054         auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
0055         registry_inst.initialize_logger(new_logger);
0056         return new_logger;
0057     }
0058 };
0059 
0060 using async_factory = async_factory_impl<async_overflow_policy::block>;
0061 using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
0062 
0063 template<typename Sink, typename... SinkArgs>
0064 inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
0065 {
0066     return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
0067 }
0068 
0069 template<typename Sink, typename... SinkArgs>
0070 inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
0071 {
0072     return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
0073 }
0074 
0075 // set global thread pool.
0076 inline void init_thread_pool(
0077     size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
0078 {
0079     auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
0080     details::registry::instance().set_tp(std::move(tp));
0081 }
0082 
0083 inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
0084 {
0085     init_thread_pool(q_size, thread_count, on_thread_start, [] {});
0086 }
0087 
0088 inline void init_thread_pool(size_t q_size, size_t thread_count)
0089 {
0090     init_thread_pool(
0091         q_size, thread_count, [] {}, [] {});
0092 }
0093 
0094 // get the global thread pool.
0095 inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
0096 {
0097     return details::registry::instance().get_tp();
0098 }
0099 } // namespace spdlog