File indexing completed on 2025-01-18 10:12:43
0001
0002
0003
0004 #pragma once
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0032
0033
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 ®istry_inst = details::registry::instance();
0041
0042
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
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
0095 inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
0096 {
0097 return details::registry::instance().get_tp();
0098 }
0099 }