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  {
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 }
async_overflow_policy overflow_policy_
Definition: async_logger.h:69
level::level_enum flush_level() const
Definition: logger_impl.h:348
void incr_msg_counter_(details::log_msg &msg)
Definition: logger_impl.h:419
async_overflow_policy
Definition: async_logger.h:32
log_err_handler error_handler() const
Definition: logger_impl.h:329
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
#define SPDLOG_CATCH_AND_HANDLE
Definition: logger_impl.h:13
STL namespace.
std::weak_ptr< details::thread_pool > thread_pool_
Definition: async_logger.h:68
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
std::vector< sink_ptr > sinks_
Definition: logger.h:179
std::shared_ptr< logger > clone(std::string new_name) override
bool should_flush_(const details::log_msg &msg)
Definition: logger_impl.h:353
void flush_() override
level::level_enum level
Definition: log_msg.h:42
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)
def move(depos, offset)
Definition: depos.py:107
void sink_it_(details::log_msg &msg) override
level::level_enum level() const
Definition: logger_impl.h:364
void backend_log_(const details::log_msg &incoming_log_msg)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
static QCString * s
Definition: config.cpp:1042