Livox SDK API  V2.0.0
async_logger.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 // Very fast asynchronous logger (millions of logs per second on an average
9 // desktop)
10 // Uses pre allocated lockfree queue for maximum throughput even under large
11 // number of threads.
12 // Creates a single back thread to pop messages from the queue and log them.
13 //
14 // Upon each log write the logger:
15 // 1. Checks if its log level is enough to log the message
16 // 2. Push a new copy of the message to a queue (or block the caller until
17 // space is available in the queue)
18 // 3. will throw spdlog_ex upon log exceptions
19 // Upon destruction, logs all remaining messages in the queue before
20 // destructing..
21 
22 #include "spdlog/common.h"
23 #include "spdlog/logger.h"
24 
25 #include <chrono>
26 #include <memory>
27 #include <string>
28 
29 namespace spdlog {
30 
31 // Async overflow policy - block by default.
33 {
34  block, // Block until message can be enqueued
35  overrun_oldest // Discard oldest message in the queue if full when trying to
36  // add new item.
37 };
38 
39 namespace details {
40 class thread_pool;
41 }
42 
43 class async_logger final : public std::enable_shared_from_this<async_logger>, public logger
44 {
45  friend class details::thread_pool;
46 
47 public:
48  template<typename It>
49  async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp,
51 
52  async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp,
54 
55  async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
57 
58  std::shared_ptr<logger> clone(std::string new_name) override;
59 
60 protected:
61  void sink_it_(details::log_msg &msg) override;
62  void flush_() override;
63 
64  void backend_log_(const details::log_msg &incoming_log_msg);
65  void backend_flush_();
66 
67 private:
68  std::weak_ptr<details::thread_pool> thread_pool_;
69  async_overflow_policy overflow_policy_;
70 };
71 } // namespace spdlog
72 
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
logger.h
async_logger_impl.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::details::thread_pool
Definition: thread_pool.h:118
spdlog
Definition: async.h:27
spdlog::async_overflow_policy::overrun_oldest
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::async_overflow_policy::block
spdlog::async_logger
Definition: async_logger.h:43
common.h
spdlog::details::log_msg
Definition: log_msg.h:16
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:83
spdlog::async_logger::backend_flush_
void backend_flush_()
Definition: async_logger_impl.h:90
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