Agora  1.2.0
Agora project
async_logger-inl.h
Go to the documentation of this file.
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 #ifndef SPDLOG_HEADER_ONLY
7 # include <spdlog/async_logger.h>
8 #endif
9 
10 #include <spdlog/sinks/sink.h>
12 
13 #include <memory>
14 #include <string>
15 
17  std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
18  : async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy)
19 {}
20 
22  std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
23  : async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy)
24 {}
25 
26 // send the log message to the thread pool
28 {
29  if (auto pool_ptr = thread_pool_.lock())
30  {
31  pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
32  }
33  else
34  {
35  throw_spdlog_ex("async log: thread pool doesn't exist anymore");
36  }
37 }
38 
39 // send flush request to the thread pool
41 {
42  if (auto pool_ptr = thread_pool_.lock())
43  {
44  pool_ptr->post_flush(shared_from_this(), overflow_policy_);
45  }
46  else
47  {
48  throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
49  }
50 }
51 
52 //
53 // backend functions - called from the thread pool to do the actual job
54 //
56 {
57  for (auto &sink : sinks_)
58  {
59  if (sink->should_log(msg.level))
60  {
62  {
63  sink->log(msg);
64  }
66  }
67  }
68 
69  if (should_flush_(msg))
70  {
71  backend_flush_();
72  }
73 }
74 
76 {
77  for (auto &sink : sinks_)
78  {
80  {
81  sink->flush();
82  }
84  }
85 }
86 
87 SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
88 {
89  auto cloned = std::make_shared<spdlog::async_logger>(*this);
90  cloned->name_ = std::move(new_name);
91  return cloned;
92 }
spdlog::async_logger::backend_sink_it_
void backend_sink_it_(const details::log_msg &incoming_log_msg)
Definition: async_logger-inl.h:55
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.h:39
SPDLOG_LOGGER_CATCH
#define SPDLOG_LOGGER_CATCH(location)
Definition: logger.h:31
thread_pool.h
spdlog::sink_ptr
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:127
spdlog::async_logger::backend_flush_
void backend_flush_()
Definition: async_logger-inl.h:75
spdlog::sinks_init_list
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:128
spdlog::async_logger::clone
std::shared_ptr< logger > clone(std::string new_name) override
Definition: async_logger-inl.h:87
spdlog::throw_spdlog_ex
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition: common-inl.h:72
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::begin
begin_tag begin(T &&...)
spdlog::source_loc
Definition: common.h:290
SPDLOG_INLINE
#define SPDLOG_INLINE
Definition: common.h:42
spdlog::async_logger::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: async_logger-inl.h:27
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
spdlog::async_logger
Definition: async_logger.h:33
async_logger.h
spdlog::details::log_msg
Definition: log_msg.h:11
sink.h
std
Definition: json.hpp:5213
spdlog::async_logger::flush_
void flush_() override
Definition: async_logger-inl.h:40
spdlog::details::log_msg::source
source_loc source
Definition: log_msg.h:29
SPDLOG_TRY
#define SPDLOG_TRY
Definition: common.h:102
spdlog::async_overflow_policy
async_overflow_policy
Definition: async_logger.h:22
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::end
end_tag end(T &&...)