bitz-server  2.0.0
async_logger_impl.h
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 "../async_logger.h"
12 #include "../details/async_log_helper.h"
13 
14 #include <chrono>
15 #include <functional>
16 #include <memory>
17 #include <string>
18 
19 template<class It>
20 inline spdlog::async_logger::async_logger(const std::string &logger_name, const It &begin, const It &end, size_t queue_size,
21  const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
22  const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
23  : logger(logger_name, begin, end)
24  , _async_log_helper(new details::async_log_helper(logger_name, _formatter, _sinks, queue_size, _err_handler, overflow_policy,
25  worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
26 {
27 }
28 
29 inline spdlog::async_logger::async_logger(const std::string &logger_name, sinks_init_list sinks_list, size_t queue_size,
30  const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
31  const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
32  : async_logger(logger_name, sinks_list.begin(), sinks_list.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms,
33  worker_teardown_cb)
34 {
35 }
36 
37 inline spdlog::async_logger::async_logger(const std::string &logger_name, sink_ptr single_sink, size_t queue_size,
38  const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
39  const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
40  : async_logger(
41  logger_name, {std::move(single_sink)}, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb)
42 {
43 }
44 
45 inline void spdlog::async_logger::flush()
46 {
47  _async_log_helper->flush();
48 }
49 
50 // Error handler
51 inline void spdlog::async_logger::set_error_handler(spdlog::log_err_handler err_handler)
52 {
53  _err_handler = err_handler;
54  _async_log_helper->set_error_handler(err_handler);
55 }
56 inline spdlog::log_err_handler spdlog::async_logger::error_handler()
57 {
58  return _err_handler;
59 }
60 
61 inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
62 {
63  _formatter = msg_formatter;
64  _async_log_helper->set_formatter(_formatter);
65 }
66 
67 inline void spdlog::async_logger::_set_pattern(const std::string &pattern, pattern_time_type pattern_time)
68 {
69  _formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
70  _async_log_helper->set_formatter(_formatter);
71 }
72 
73 inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
74 {
75  try
76  {
77 #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
78  _incr_msg_counter(msg);
79 #endif
80  _async_log_helper->log(msg);
81  if (_should_flush_on(msg))
82  {
83  _async_log_helper->flush(); // do async flush
84  }
85  }
86  catch (const std::exception &ex)
87  {
88  _err_handler(ex.what());
89  }
90  catch (...)
91  {
92  _err_handler("Unknown exception in logger " + _name);
93  throw;
94  }
95 }