xref: /aosp_15_r20/external/gmmlib/Source/GmmLib/Utility/GmmLog/spdlog/async_logger.h (revision 35ffd701415c9e32e53136d61a677a8d0a8fc4a5)
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 desktop)
9 // Uses pre allocated lockfree queue for maximum throughput even under large number of threads.
10 // Creates a single back thread to pop messages from the queue and log them.
11 //
12 // Upon each log write the logger:
13 //    1. Checks if its log level is enough to log the message
14 //    2. Push a new copy of the message to a queue (or block the caller until space is available in the queue)
15 //    3. will throw spdlog_ex upon log exceptions
16 // Upon destruction, logs all remaining messages in the queue before destructing..
17 
18 #include <spdlog/common.h>
19 #include <spdlog/logger.h>
20 
21 #include <chrono>
22 #include <functional>
23 #include <string>
24 #include <memory>
25 
26 namespace spdlog
27 {
28 
29 namespace details
30 {
31 class async_log_helper;
32 }
33 
34 class async_logger :public logger
35 {
36 public:
37     template<class It>
38     async_logger(const std::string& name,
39                  const It& begin,
40                  const It& end,
41                  size_t queue_size,
42                  const async_overflow_policy overflow_policy =  async_overflow_policy::block_retry,
43                  const std::function<void()>& worker_warmup_cb = nullptr,
44                  const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
45                  const std::function<void()>& worker_teardown_cb = nullptr);
46 
47     async_logger(const std::string& logger_name,
48                  sinks_init_list sinks,
49                  size_t queue_size,
50                  const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
51                  const std::function<void()>& worker_warmup_cb = nullptr,
52                  const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
53                  const std::function<void()>& worker_teardown_cb = nullptr);
54 
55     async_logger(const std::string& logger_name,
56                  sink_ptr single_sink,
57                  size_t queue_size,
58                  const async_overflow_policy overflow_policy =  async_overflow_policy::block_retry,
59                  const std::function<void()>& worker_warmup_cb = nullptr,
60                  const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
61                  const std::function<void()>& worker_teardown_cb = nullptr);
62 
63     //Wait for the queue to be empty, and flush synchronously
64     //Warning: this can potentialy last forever as we wait it to complete
65     void flush() override;
66 protected:
67     void _sink_it(details::log_msg& msg) override;
68     void _set_formatter(spdlog::formatter_ptr msg_formatter) override;
69     void _set_pattern(const std::string& pattern) override;
70 
71 private:
72     std::unique_ptr<details::async_log_helper> _async_log_helper;
73 };
74 }
75 
76 
77 #include <spdlog/details/async_logger_impl.h>
78