| 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 |
|---|
| 9 | // desktop) |
|---|
| 10 | // Uses pre allocated lockfree queue for maximum throughput even under large |
|---|
| 11 | // number of threads. |
|---|
| 12 | // Creates a single back thread to pop messages from the queue and log them. |
|---|
| 13 | // |
|---|
| 14 | // Upon each log write the logger: |
|---|
| 15 | // 1. Checks if its log level is enough to log the message |
|---|
| 16 | // 2. Push a new copy of the message to a queue (or block the caller until |
|---|
| 17 | // space is available in the queue) |
|---|
| 18 | // 3. will throw spdlog_ex upon log exceptions |
|---|
| 19 | // Upon destruction, logs all remaining messages in the queue before |
|---|
| 20 | // destructing.. |
|---|
| 21 | |
|---|
| 22 | #include "spdlog/common.h" |
|---|
| 23 | #include "spdlog/logger.h" |
|---|
| 24 | |
|---|
| 25 | #include <chrono> |
|---|
| 26 | #include <memory> |
|---|
| 27 | #include <string> |
|---|
| 28 | |
|---|
| 29 | namespace spdlog { |
|---|
| 30 | |
|---|
| 31 | // Async overflow policy - block by default. |
|---|
| 32 | enum class async_overflow_policy |
|---|
| 33 | { |
|---|
| 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; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | class async_logger final : public std::enable_shared_from_this<async_logger>, public logger |
|---|
| 44 | { |
|---|
| 45 | friend class details::thread_pool; |
|---|
| 46 | |
|---|
| 47 | public: |
|---|
| 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; |
|---|
| 59 | |
|---|
| 60 | protected: |
|---|
| 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_(); |
|---|
| 66 | |
|---|
| 67 | private: |
|---|
| 68 | std::weak_ptr<details::thread_pool> thread_pool_; |
|---|
| 69 | async_overflow_policy overflow_policy_; |
|---|
| 70 | }; |
|---|
| 71 | } // namespace spdlog |
|---|
| 72 | |
|---|
| 73 | #include "details/async_logger_impl.h" |
|---|