2019-06-04 00:09:16 +03:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2016-08-22 22:09:23 +03:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-11 13:52:46 +03:00
|
|
|
// Fast asynchronous logger.
|
|
|
|
|
// Uses pre allocated queue.
|
2016-08-22 22:09:23 +03:00
|
|
|
// Creates a single back thread to pop messages from the queue and log them.
|
|
|
|
|
//
|
|
|
|
|
// Upon each log write the logger:
|
|
|
|
|
// 1. Checks if its log level is enough to log the message
|
2018-07-21 23:48:07 +03:00
|
|
|
// 2. Push a new copy of the message to a queue (or block the caller until
|
|
|
|
|
// space is available in the queue)
|
|
|
|
|
// Upon destruction, logs all remaining messages in the queue before
|
2023-09-28 23:45:45 +03:00
|
|
|
// destructing
|
2016-08-22 22:09:23 +03:00
|
|
|
|
2024-01-12 17:33:23 +02:00
|
|
|
#include "./logger.h"
|
2018-04-14 04:16:05 +03:00
|
|
|
|
2018-03-09 15:26:33 +02:00
|
|
|
namespace spdlog {
|
2018-07-20 23:20:48 +03:00
|
|
|
|
|
|
|
|
// Async overflow policy - block by default.
|
2023-09-25 02:35:55 +03:00
|
|
|
enum class async_overflow_policy {
|
2023-09-25 16:40:05 +03:00
|
|
|
block, // Block until message can be enqueued
|
2024-01-13 18:53:19 +02:00
|
|
|
overrun_oldest, // Discard the oldest message in the queue if full when trying to
|
2023-09-25 16:40:05 +03:00
|
|
|
// add new item.
|
|
|
|
|
discard_new // Discard new message if the queue is full when trying to add new item.
|
2018-07-20 23:20:48 +03:00
|
|
|
};
|
|
|
|
|
|
2018-03-09 15:26:33 +02:00
|
|
|
namespace details {
|
2018-04-14 03:34:57 +03:00
|
|
|
class thread_pool;
|
2016-08-22 22:09:23 +03:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 09:37:32 +02:00
|
|
|
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger {
|
2018-04-14 03:34:57 +03:00
|
|
|
friend class details::thread_pool;
|
|
|
|
|
|
2016-08-22 22:09:23 +03:00
|
|
|
public:
|
2023-09-25 02:35:55 +03:00
|
|
|
template <typename It>
|
|
|
|
|
async_logger(std::string logger_name,
|
|
|
|
|
It begin,
|
|
|
|
|
It end,
|
|
|
|
|
std::weak_ptr<details::thread_pool> tp,
|
|
|
|
|
async_overflow_policy overflow_policy = async_overflow_policy::block)
|
2023-09-25 16:19:10 +03:00
|
|
|
: logger(std::move(logger_name), begin, end),
|
|
|
|
|
thread_pool_(std::move(tp)),
|
|
|
|
|
overflow_policy_(overflow_policy) {}
|
2016-08-22 22:09:23 +03:00
|
|
|
|
2023-09-25 02:35:55 +03:00
|
|
|
async_logger(std::string logger_name,
|
|
|
|
|
sinks_init_list sinks_list,
|
|
|
|
|
std::weak_ptr<details::thread_pool> tp,
|
|
|
|
|
async_overflow_policy overflow_policy = async_overflow_policy::block);
|
2016-08-22 22:09:23 +03:00
|
|
|
|
2023-09-25 02:35:55 +03:00
|
|
|
async_logger(std::string logger_name,
|
|
|
|
|
sink_ptr single_sink,
|
|
|
|
|
std::weak_ptr<details::thread_pool> tp,
|
|
|
|
|
async_overflow_policy overflow_policy = async_overflow_policy::block);
|
2017-03-28 01:54:33 +03:00
|
|
|
|
2019-08-29 01:05:23 +03:00
|
|
|
std::shared_ptr<logger> clone(std::string new_name) override;
|
|
|
|
|
|
2016-08-22 22:09:23 +03:00
|
|
|
protected:
|
2019-08-22 19:57:59 +03:00
|
|
|
void sink_it_(const details::log_msg &msg) override;
|
2018-06-10 22:59:17 +03:00
|
|
|
void flush_() override;
|
2019-08-26 00:13:43 +03:00
|
|
|
void backend_sink_it_(const details::log_msg &incoming_log_msg);
|
2018-06-10 22:59:17 +03:00
|
|
|
void backend_flush_();
|
2016-08-22 22:09:23 +03:00
|
|
|
|
|
|
|
|
private:
|
2018-06-10 22:59:17 +03:00
|
|
|
std::weak_ptr<details::thread_pool> thread_pool_;
|
|
|
|
|
async_overflow_policy overflow_policy_;
|
2016-08-22 22:09:23 +03:00
|
|
|
};
|
2023-09-25 16:40:05 +03:00
|
|
|
} // namespace spdlog
|