|
5 | 5 |
|
6 | 6 | #pragma once |
7 | 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. |
| 8 | +// Very fast asynchronous logger (millions of logs per second on an average |
| 9 | +// desktop) |
| 10 | +// Uses pre allocated lockfree queue for maximum throughput even under large |
| 11 | +// number of threads. |
10 | 12 | // Creates a single back thread to pop messages from the queue and log them. |
11 | 13 | // |
12 | 14 | // Upon each log write the logger: |
13 | 15 | // 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) |
| 16 | +// 2. Push a new copy of the message to a queue (or block the caller until |
| 17 | +// space is available in the queue) |
15 | 18 | // 3. will throw spdlog_ex upon log exceptions |
16 | | -// Upon destruction, logs all remaining messages in the queue before destructing.. |
| 19 | +// Upon destruction, logs all remaining messages in the queue before |
| 20 | +// destructing.. |
17 | 21 |
|
18 | | -#include "common.h" |
19 | | -#include "logger.h" |
| 22 | +#include "spdlog/common.h" |
| 23 | +#include "spdlog/logger.h" |
20 | 24 |
|
21 | 25 | #include <chrono> |
22 | | -#include <functional> |
23 | | -#include <string> |
24 | 26 | #include <memory> |
| 27 | +#include <string> |
25 | 28 |
|
26 | | -namespace spdlog |
27 | | -{ |
| 29 | +namespace spdlog { |
28 | 30 |
|
29 | | -namespace details |
| 31 | +// Async overflow policy - block by default. |
| 32 | +enum class async_overflow_policy |
30 | 33 | { |
31 | | -class async_log_helper; |
| 34 | + block, // Block until message can be enqueued |
| 35 | + overrun_oldest // Discard oldest message in the queue if full when trying to |
| 36 | + // add new item. |
| 37 | +}; |
| 38 | + |
| 39 | +namespace details { |
| 40 | +class thread_pool; |
32 | 41 | } |
33 | 42 |
|
34 | | -class async_logger SPDLOG_FINAL :public logger |
| 43 | +class async_logger final : public std::enable_shared_from_this<async_logger>, public logger |
35 | 44 | { |
| 45 | + friend class details::thread_pool; |
| 46 | + |
36 | 47 | 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 potentially last forever as we wait it to complete |
65 | | - void flush() override; |
66 | | - |
67 | | - // Error handler |
68 | | - virtual void set_error_handler(log_err_handler) override; |
69 | | - virtual log_err_handler error_handler() override; |
| 48 | + template<typename It> |
| 49 | + async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp, |
| 50 | + async_overflow_policy overflow_policy = async_overflow_policy::block); |
| 51 | + |
| 52 | + async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, |
| 53 | + async_overflow_policy overflow_policy = async_overflow_policy::block); |
| 54 | + |
| 55 | + async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp, |
| 56 | + async_overflow_policy overflow_policy = async_overflow_policy::block); |
| 57 | + |
| 58 | + std::shared_ptr<logger> clone(std::string new_name) override; |
70 | 59 |
|
71 | 60 | protected: |
72 | | - void _sink_it(details::log_msg& msg) override; |
73 | | - void _set_formatter(spdlog::formatter_ptr msg_formatter) override; |
74 | | - void _set_pattern(const std::string& pattern, pattern_time_type pattern_time) override; |
| 61 | + void sink_it_(details::log_msg &msg) override; |
| 62 | + void flush_() override; |
| 63 | + |
| 64 | + void backend_log_(const details::log_msg &incoming_log_msg); |
| 65 | + void backend_flush_(); |
75 | 66 |
|
76 | 67 | private: |
77 | | - std::unique_ptr<details::async_log_helper> _async_log_helper; |
| 68 | + std::weak_ptr<details::thread_pool> thread_pool_; |
| 69 | + async_overflow_policy overflow_policy_; |
78 | 70 | }; |
79 | | -} |
80 | | - |
| 71 | +} // namespace spdlog |
81 | 72 |
|
82 | 73 | #include "details/async_logger_impl.h" |
0 commit comments