Livox SDK API  V2.0.0
async_logger_impl.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 // async logger implementation
9 // uses a thread pool to perform the actual logging
10 
12 
13 #include <chrono>
14 #include <memory>
15 #include <string>
16 
17 template<typename It>
19  std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
20  : logger(std::move(logger_name), begin, end)
21  , thread_pool_(std::move(tp))
22  , overflow_policy_(overflow_policy)
23 {
24 }
25 
27  std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
28  : async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy)
29 {
30 }
31 
33  std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
34  : async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy)
35 {
36 }
37 
38 // send the log message to the thread pool
40 {
41 #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
42  incr_msg_counter_(msg);
43 #endif
44  if (auto pool_ptr = thread_pool_.lock())
45  {
46  pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
47  }
48  else
49  {
50  throw spdlog_ex("async log: thread pool doesn't exist anymore");
51  }
52 }
53 
54 // send flush request to the thread pool
56 {
57  if (auto pool_ptr = thread_pool_.lock())
58  {
59  pool_ptr->post_flush(shared_from_this(), overflow_policy_);
60  }
61  else
62  {
63  throw spdlog_ex("async flush: thread pool doesn't exist anymore");
64  }
65 }
66 
67 //
68 // backend functions - called from the thread pool to do the actual job
69 //
70 inline void spdlog::async_logger::backend_log_(const details::log_msg &incoming_log_msg)
71 {
72  try
73  {
74  for (auto &s : sinks_)
75  {
76  if (s->should_log(incoming_log_msg.level))
77  {
78  s->log(incoming_log_msg);
79  }
80  }
81  }
83 
84  if (should_flush_(incoming_log_msg))
85  {
86  backend_flush_();
87  }
88 }
89 
91 {
92  try
93  {
94  for (auto &sink : sinks_)
95  {
96  sink->flush();
97  }
98  }
100 }
101 
102 inline std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
103 {
104  auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name), sinks_.begin(), sinks_.end(), thread_pool_, overflow_policy_);
105 
106  cloned->set_level(this->level());
107  cloned->flush_on(this->flush_level());
108  cloned->set_error_handler(this->error_handler());
109  return std::move(cloned);
110 }
spdlog::async_logger::async_logger
async_logger(std::string logger_name, It begin, It end, std::weak_ptr< details::thread_pool > tp, async_overflow_policy overflow_policy=async_overflow_policy::block)
Definition: async_logger_impl.h:18
thread_pool.h
spdlog::sink_ptr
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
fmt::v5::internal::end
auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
spdlog::logger
Definition: logger.h:31
spdlog::sinks_init_list
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
spdlog::async_logger::clone
std::shared_ptr< logger > clone(std::string new_name) override
Definition: async_logger_impl.h:102
spdlog::async_logger::flush_
void flush_() override
Definition: async_logger_impl.h:55
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:42
spdlog::spdlog_ex
Definition: common.h:175
spdlog::async_logger
Definition: async_logger.h:43
spdlog::details::log_msg
Definition: log_msg.h:16
std
Definition: format.h:297
spdlog::async_logger::backend_flush_
void backend_flush_()
Definition: async_logger_impl.h:90
SPDLOG_CATCH_AND_HANDLE
#define SPDLOG_CATCH_AND_HANDLE
Definition: logger_impl.h:13
spdlog::async_logger::sink_it_
void sink_it_(details::log_msg &msg) override
Definition: async_logger_impl.h:39
fmt::v5::internal::begin
auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
spdlog::async_logger::backend_log_
void backend_log_(const details::log_msg &incoming_log_msg)
Definition: async_logger_impl.h:70
spdlog::async_overflow_policy
async_overflow_policy
Definition: async_logger.h:32