async_logger_impl.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 // Async Logger implementation
9 // Use an async_sink (queue per logger) to perform the logging in a worker thread
10 
13 
14 #include <string>
15 #include <functional>
16 #include <chrono>
17 #include <memory>
18 
19 template<class It>
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 
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 
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 
55 inline void spdlog::async_logger::flush()
56 {
57  _async_log_helper->flush(true);
58 }
59 
60 // Error handler
62 {
63  _err_handler = err_handler;
64  _async_log_helper->set_error_handler(err_handler);
65 
66 }
67 inline spdlog::log_err_handler spdlog::async_logger::error_handler()
68 {
69  return _err_handler;
70 }
71 
72 
73 inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
74 {
75  _formatter = msg_formatter;
76  _async_log_helper->set_formatter(_formatter);
77 }
78 
79 inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time)
80 {
81  _formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
82  _async_log_helper->set_formatter(_formatter);
83 }
84 
85 
86 inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
87 {
88  try
89  {
90 #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
91  msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed);
92 #endif
93  _async_log_helper->log(msg);
94  if (_should_flush_on(msg))
95  _async_log_helper->flush(false); // do async flush
96  }
97  catch (const std::exception &ex)
98  {
99  _err_handler(ex.what());
100  }
101  catch (...)
102  {
103  _err_handler("Unknown exception");
104  }
105 }
std::unique_ptr< details::async_log_helper > _async_log_helper
Definition: async_logger.h:77
std::shared_ptr< spdlog::formatter > formatter_ptr
std::shared_ptr< sinks::sink > sink_ptr
async_logger(const std::string &name, const It &begin, const It &end, size_t queue_size, const async_overflow_policy overflow_policy=async_overflow_policy::block_retry, const std::function< void()> &worker_warmup_cb=nullptr, const std::chrono::milliseconds &flush_interval_ms=std::chrono::milliseconds::zero(), const std::function< void()> &worker_teardown_cb=nullptr)
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
void set_error_handler(log_err_handler)
Definition: spdlog_impl.h:239
std::atomic< size_t > _msg_counter
formatter_ptr _formatter
std::initializer_list< sink_ptr > sinks_init_list
bool _should_flush_on(const details::log_msg &)
Definition: logger_impl.h:554
std::function< void(const std::string &err_msg)> log_err_handler
log_err_handler _err_handler


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:03