Livox SDK API  V2.2.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 
auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
async_overflow_policy
Definition: async_logger.h:32
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
Definition: async.h:27
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:83
auto end(const C &c) -> decltype(c.end())
Definition: format.h:257