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