xref: /aosp_15_r20/external/gmmlib/Source/GmmLib/Utility/GmmLog/spdlog/details/async_logger_impl.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 // Async Logger implementation
9 // Use an async_sink (queue per logger) to perform the logging in a worker thread
10 
11 #include <spdlog/details/async_log_helper.h>
12 #include <spdlog/async_logger.h>
13 
14 #include <string>
15 #include <functional>
16 #include <chrono>
17 #include <memory>
18 
19 template<class It>
async_logger(const std::string & logger_name,const It & begin,const It & end,size_t queue_size,const async_overflow_policy overflow_policy,const std::function<void ()> & worker_warmup_cb,const std::chrono::milliseconds & flush_interval_ms,const std::function<void ()> & worker_teardown_cb)20 inline spdlog::async_logger::async_logger(const std::string& logger_name,
21         const It& begin,
22         const It& end,
23         size_t queue_size,
24         const  async_overflow_policy overflow_policy,
25         const std::function<void()>& worker_warmup_cb,
26         const std::chrono::milliseconds& flush_interval_ms,
27         const std::function<void()>& worker_teardown_cb) :
28     logger(logger_name, begin, end),
29     _async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
30 {
31 }
32 
async_logger(const std::string & logger_name,sinks_init_list sinks_list,size_t queue_size,const async_overflow_policy overflow_policy,const std::function<void ()> & worker_warmup_cb,const std::chrono::milliseconds & flush_interval_ms,const std::function<void ()> & worker_teardown_cb)33 inline spdlog::async_logger::async_logger(const std::string& logger_name,
34         sinks_init_list sinks_list,
35         size_t queue_size,
36         const  async_overflow_policy overflow_policy,
37         const std::function<void()>& worker_warmup_cb,
38         const std::chrono::milliseconds& flush_interval_ms,
39         const std::function<void()>& worker_teardown_cb) :
40     async_logger(logger_name, sinks_list.begin(), sinks_list.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
41 
async_logger(const std::string & logger_name,sink_ptr single_sink,size_t queue_size,const async_overflow_policy overflow_policy,const std::function<void ()> & worker_warmup_cb,const std::chrono::milliseconds & flush_interval_ms,const std::function<void ()> & worker_teardown_cb)42 inline spdlog::async_logger::async_logger(const std::string& logger_name,
43         sink_ptr single_sink,
44         size_t queue_size,
45         const  async_overflow_policy overflow_policy,
46         const std::function<void()>& worker_warmup_cb,
47         const std::chrono::milliseconds& flush_interval_ms,
48         const std::function<void()>& worker_teardown_cb) :
49     async_logger(logger_name,
50 {
51     single_sink
52 }, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
53 
54 
flush()55 inline void spdlog::async_logger::flush()
56 {
57     _async_log_helper->flush(true);
58 }
59 
_set_formatter(spdlog::formatter_ptr msg_formatter)60 inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
61 {
62     _formatter = msg_formatter;
63     _async_log_helper->set_formatter(_formatter);
64 }
65 
_set_pattern(const std::string & pattern)66 inline void spdlog::async_logger::_set_pattern(const std::string& pattern)
67 {
68     _formatter = std::make_shared<pattern_formatter>(pattern);
69     _async_log_helper->set_formatter(_formatter);
70 }
71 
72 
_sink_it(details::log_msg & msg)73 inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
74 {
75     try
76     {
77         _async_log_helper->log(msg);
78         if (_should_flush_on(msg))
79             _async_log_helper->flush(false); // do async flush
80     }
81     catch (const std::exception &ex)
82     {
83         _err_handler(ex.what());
84     }
85     catch (...)
86     {
87         _err_handler("Unknown exception");
88     }
89 }
90