1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6//
7// Async logging using global thread pool
8// All loggers created here share same global thread pool.
9// Each log message is pushed to a queue along with a shared pointer to the
10// logger.
11// If a logger deleted while having pending messages in the queue, it's actual
12// destruction will defer
13// until all its messages are processed by the thread pool.
14// This is because each message in the queue holds a shared_ptr to the
15// originating logger.
16
17#include <spdlog/async_logger.h>
18#include <spdlog/details/registry.h>
19#include <spdlog/details/thread_pool.h>
20
21#include <functional>
22#include <memory>
23#include <mutex>
24
25namespace spdlog {
26
27namespace details {
28static const size_t default_async_q_size = 8192;
29}
30
31// async logger factory - creates async loggers backed with thread pool.
32// if a global thread pool doesn't already exist, create it with default queue
33// size of 8192 items and single thread.
34template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
35struct async_factory_impl {
36 template <typename Sink, typename... SinkArgs>
37 static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
38 auto &registry_inst = details::registry::instance();
39
40 // create global thread pool if not already exists..
41
42 auto &mutex = registry_inst.tp_mutex();
43 std::lock_guard<std::recursive_mutex> tp_lock(mutex);
44 auto tp = registry_inst.get_tp();
45 if (tp == nullptr) {
46 tp = std::make_shared<details::thread_pool>(args: details::default_async_q_size, args: 1U);
47 registry_inst.set_tp(tp);
48 }
49
50 auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
51 auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
52 std::move(tp), OverflowPolicy);
53 registry_inst.initialize_logger(new_logger);
54 return new_logger;
55 }
56};
57
58using async_factory = async_factory_impl<async_overflow_policy::block>;
59using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
60
61template <typename Sink, typename... SinkArgs>
62inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name,
63 SinkArgs &&...sink_args) {
64 return async_factory::create<Sink>(std::move(logger_name),
65 std::forward<SinkArgs>(sink_args)...);
66}
67
68template <typename Sink, typename... SinkArgs>
69inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
70 SinkArgs &&...sink_args) {
71 return async_factory_nonblock::create<Sink>(std::move(logger_name),
72 std::forward<SinkArgs>(sink_args)...);
73}
74
75// set global thread pool.
76inline void init_thread_pool(size_t q_size,
77 size_t thread_count,
78 std::function<void()> on_thread_start,
79 std::function<void()> on_thread_stop) {
80 auto tp = std::make_shared<details::thread_pool>(args&: q_size, args&: thread_count, args&: on_thread_start,
81 args&: on_thread_stop);
82 details::registry::instance().set_tp(std::move(tp));
83}
84
85inline void init_thread_pool(size_t q_size,
86 size_t thread_count,
87 std::function<void()> on_thread_start) {
88 init_thread_pool(q_size, thread_count, on_thread_start, on_thread_stop: [] {});
89}
90
91inline void init_thread_pool(size_t q_size, size_t thread_count) {
92 init_thread_pool(
93 q_size, thread_count, on_thread_start: [] {}, on_thread_stop: [] {});
94}
95
96// get the global thread pool.
97inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
98 return details::registry::instance().get_tp();
99}
100} // namespace spdlog
101