Agora  1.2.0
Agora project
async_logger.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 // Fast asynchronous logger.
7 // Uses pre allocated queue.
8 // Creates a single back thread to pop messages from the queue and log them.
9 //
10 // Upon each log write the logger:
11 // 1. Checks if its log level is enough to log the message
12 // 2. Push a new copy of the message to a queue (or block the caller until
13 // space is available in the queue)
14 // Upon destruction, logs all remaining messages in the queue before
15 // destructing..
16 
17 #include <spdlog/logger.h>
18 
19 namespace spdlog {
20 
21 // Async overflow policy - block by default.
23 {
24  block, // Block until message can be enqueued
25  overrun_oldest // Discard oldest message in the queue if full when trying to
26  // add new item.
27 };
28 
29 namespace details {
30 class thread_pool;
31 }
32 
33 class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger
34 {
35  friend class details::thread_pool;
36 
37 public:
38  template<typename It>
39  async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp,
41  : logger(std::move(logger_name), begin, end)
42  , thread_pool_(std::move(tp))
43  , overflow_policy_(overflow_policy)
44  {}
45 
46  async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp,
48 
49  async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
51 
52  std::shared_ptr<logger> clone(std::string new_name) override;
53 
54 protected:
55  void sink_it_(const details::log_msg &msg) override;
56  void flush_() override;
57  void backend_sink_it_(const details::log_msg &incoming_log_msg);
58  void backend_flush_();
59 
60 private:
61  std::weak_ptr<details::thread_pool> thread_pool_;
63 };
64 } // namespace spdlog
65 
66 #ifdef SPDLOG_HEADER_ONLY
67 # include "async_logger-inl.h"
68 #endif
spdlog::async_logger::thread_pool_
std::weak_ptr< details::thread_pool > thread_pool_
Definition: async_logger.h:61
logger.h
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
async_logger-inl.h
spdlog::sink_ptr
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:127
spdlog::logger
Definition: logger.h:54
spdlog::sinks_init_list
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:128
spdlog::details::thread_pool
Definition: thread_pool.h:81
spdlog
Definition: async.h:25
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::begin
begin_tag begin(T &&...)
spdlog::async_overflow_policy::overrun_oldest
@ overrun_oldest
spdlog::async_overflow_policy::block
@ block
spdlog::async_logger
Definition: async_logger.h:33
spdlog::details::log_msg
Definition: log_msg.h:11
std
Definition: json.hpp:5213
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:95
spdlog::async_logger::overflow_policy_
async_overflow_policy overflow_policy_
Definition: async_logger.h:62
SPDLOG_API
#define SPDLOG_API
Definition: common.h:40
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 &&...)