bitz-server  2.0.1
async_logger.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 // 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 "common.h"
19 #include "logger.h"
20 
21 #include <chrono>
22 #include <functional>
23 #include <memory>
24 #include <string>
25 
26 namespace spdlog {
27 
28 namespace details {
29 class async_log_helper;
30 }
31 
32 class async_logger SPDLOG_FINAL : public logger
33 {
34 public:
35  template<class It>
36  async_logger(const std::string &logger_name, const It &begin, const It &end, size_t queue_size,
37  const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
38  const std::function<void()> &worker_warmup_cb = nullptr,
39  const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
40  const std::function<void()> &worker_teardown_cb = nullptr);
41 
42  async_logger(const std::string &logger_name, sinks_init_list sinks, size_t queue_size,
43  const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
44  const std::function<void()> &worker_warmup_cb = nullptr,
45  const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
46  const std::function<void()> &worker_teardown_cb = nullptr);
47 
48  async_logger(const std::string &logger_name, sink_ptr single_sink, size_t queue_size,
49  const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
50  const std::function<void()> &worker_warmup_cb = nullptr,
51  const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
52  const std::function<void()> &worker_teardown_cb = nullptr);
53 
54  // Wait for the queue to be empty, and flush synchronously
55  // Warning: this can potentially last forever as we wait it to complete
56  void flush() override;
57 
58  // Error handler
59  void set_error_handler(log_err_handler) override;
60  log_err_handler error_handler() override;
61 
62 protected:
63  void _sink_it(details::log_msg &msg) override;
64  void _set_formatter(spdlog::formatter_ptr msg_formatter) override;
65  void _set_pattern(const std::string &pattern, pattern_time_type pattern_time) override;
66 
67 private:
68  std::unique_ptr<details::async_log_helper> _async_log_helper;
69 };
70 } // namespace spdlog
71 
72 #include "details/async_logger_impl.h"
Definition: async_logger.h:32
Definition: logger.h:24
Definition: async_logger.h:26
Definition: log_msg.h:16