mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 09:59:33 +08:00
@@ -1,86 +1,98 @@
|
||||
#include <tuple>
|
||||
|
||||
#include "includes.h"
|
||||
#include "spdlog/async.h"
|
||||
#include "spdlog/sinks/async_sink.h"
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
#define TEST_FILENAME "test_logs/async_test.log"
|
||||
|
||||
using spdlog::sinks::async_sink;
|
||||
using spdlog::sinks::sink;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
auto creat_async_logger(size_t queue_size, std::shared_ptr<sink> backend_sink) {
|
||||
async_sink::config cfg;
|
||||
cfg.queue_size = queue_size;
|
||||
cfg.sinks.push_back(std::move(backend_sink));
|
||||
auto s = std::make_shared<async_sink>(cfg);
|
||||
auto logger = std::make_shared<spdlog::logger>("async_logger", s);
|
||||
return std::make_tuple(logger, s);
|
||||
}
|
||||
|
||||
TEST_CASE("basic async test ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
const auto test_sink = std::make_shared<test_sink_mt>();
|
||||
size_t overrun_counter = 0;
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
constexpr size_t queue_size = 16;
|
||||
auto [logger, async_sink] = creat_async_logger(queue_size, test_sink);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
overrun_counter = tp->overrun_counter();
|
||||
overrun_counter = async_sink->get_overrun_counter();
|
||||
}
|
||||
// logger and async_sink are destroyed here so the queue should be emptied
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
REQUIRE(overrun_counter == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
async_sink::config config;
|
||||
config.queue_size = 4;
|
||||
config.policy = async_sink::overflow_policy::overrun_oldest;
|
||||
config.sinks.push_back(test_sink);
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::overrun_oldest);
|
||||
auto as = std::make_shared<async_sink>(config);
|
||||
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||
REQUIRE(as->get_discard_counter() == 0);
|
||||
REQUIRE(as->get_overrun_counter() == 0);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->overrun_counter() > 0);
|
||||
REQUIRE(as->get_overrun_counter() > 0);
|
||||
as->reset_overrun_counter();
|
||||
REQUIRE(as->get_overrun_counter() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy discard_new ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
async_sink::config config;
|
||||
config.queue_size = 4;
|
||||
config.policy = async_sink::overflow_policy::discard_new;
|
||||
config.sinks.push_back(test_sink);
|
||||
size_t messages = 1024;
|
||||
auto as = std::make_shared<async_sink>(config);
|
||||
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::discard_new);
|
||||
|
||||
REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new);
|
||||
REQUIRE(as->get_discard_counter() == 0);
|
||||
REQUIRE(as->get_overrun_counter() == 0);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->discard_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy using factory ", "[async]") {
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
spdlog::init_thread_pool(queue_size, 1);
|
||||
|
||||
auto logger = spdlog::create_async_nb<spdlog::sinks::test_sink_mt>("as2");
|
||||
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
test_sink->set_delay(std::chrono::milliseconds(3));
|
||||
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(as->get_discard_counter() > 0);
|
||||
as->reset_discard_counter();
|
||||
REQUIRE(as->get_discard_counter() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("flush", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 256;
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
constexpr size_t queue_size = 256;
|
||||
auto [logger, async_sink] = creat_async_logger(queue_size, test_sink);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
}
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
@@ -88,18 +100,24 @@ TEST_CASE("flush", "[async]") {
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
TEST_CASE("wait_dtor ", "[async]") {
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(5));
|
||||
async_sink::config config;
|
||||
config.sinks.push_back(test_sink);
|
||||
config.queue_size = 4;
|
||||
config.policy = async_sink::overflow_policy::block;
|
||||
size_t messages = 100;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
{
|
||||
auto as = std::make_shared<async_sink>(config);
|
||||
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
REQUIRE(as->get_overrun_counter() == 0);
|
||||
REQUIRE(as->get_discard_counter() == 0);
|
||||
}
|
||||
logger->flush();
|
||||
tp.reset();
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
@@ -107,18 +125,17 @@ TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||
|
||||
TEST_CASE("multi threads", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
size_t n_threads = 10;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
constexpr size_t queue_size = 128;
|
||||
auto [logger, async_sink] = creat_async_logger(queue_size, test_sink);
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (size_t i = 0; i < n_threads; i++) {
|
||||
threads.emplace_back([logger, messages] {
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
threads.emplace_back([l = logger, msgs = messages] {
|
||||
for (size_t j = 0; j < msgs; j++) {
|
||||
l->info("Hello message #{}", j);
|
||||
}
|
||||
});
|
||||
logger->flush();
|
||||
@@ -128,7 +145,6 @@ TEST_CASE("multi threads", "[async]") {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
||||
REQUIRE(test_sink->flush_counter() == n_threads);
|
||||
}
|
||||
@@ -136,45 +152,146 @@ TEST_CASE("multi threads", "[async]") {
|
||||
TEST_CASE("to_file", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024;
|
||||
size_t tp_threads = 1;
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
{
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
auto [logger, async_sink] = creat_async_logger(messages, file_sink);
|
||||
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
|
||||
require_message_count(TEST_FILENAME, messages);
|
||||
auto contents = file_contents(TEST_FILENAME);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_file multi-workers", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024 * 10;
|
||||
size_t tp_threads = 10;
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
|
||||
TEST_CASE("bad_ctor", "[async]") {
|
||||
async_sink::config cfg;
|
||||
cfg.queue_size = 0;
|
||||
REQUIRE_THROWS_AS(std::make_shared<async_sink>(cfg), spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_ctor2", "[async]") {
|
||||
async_sink::config cfg;
|
||||
cfg.queue_size = async_sink::max_queue_size + 1;
|
||||
REQUIRE_THROWS_AS(std::make_shared<async_sink>(cfg), spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("start_stop_clbks", "[async]") {
|
||||
bool start_called = false;
|
||||
bool stop_called = false;
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
async_sink::config cfg;
|
||||
cfg.on_thread_start = [&] { start_called = true; };
|
||||
cfg.on_thread_stop = [&] { stop_called = true; };
|
||||
auto sink = std::make_shared<async_sink>(cfg);
|
||||
}
|
||||
REQUIRE(start_called);
|
||||
REQUIRE(stop_called);
|
||||
}
|
||||
|
||||
TEST_CASE("start_stop_clbks2", "[async]") {
|
||||
bool start_called = false;
|
||||
bool stop_called = false;
|
||||
{
|
||||
async_sink::config cfg;
|
||||
cfg.on_thread_start = [&] { start_called = true; };
|
||||
auto sink = std::make_shared<async_sink>(cfg);
|
||||
}
|
||||
REQUIRE(start_called);
|
||||
REQUIRE_FALSE(stop_called);
|
||||
}
|
||||
|
||||
TEST_CASE("start_stop_clbks3", "[async]") {
|
||||
bool start_called = false;
|
||||
bool stop_called = false;
|
||||
{
|
||||
async_sink::config cfg;
|
||||
cfg.on_thread_start = nullptr;
|
||||
cfg.on_thread_stop = [&] { stop_called = true; };
|
||||
auto sink = std::make_shared<async_sink>(cfg);
|
||||
}
|
||||
REQUIRE_FALSE(start_called);
|
||||
REQUIRE(stop_called);
|
||||
}
|
||||
|
||||
TEST_CASE("start_stop_clbks4", "[async]") {
|
||||
bool start_called = false;
|
||||
bool stop_called = false;
|
||||
{
|
||||
async_sink::config cfg;
|
||||
cfg.on_thread_start = [&] { start_called = true; };
|
||||
cfg.on_thread_stop = [&] { stop_called = true; };
|
||||
cfg.queue_size = 128;
|
||||
auto sink = std::make_shared<async_sink>(cfg);
|
||||
}
|
||||
REQUIRE(start_called);
|
||||
REQUIRE(stop_called);
|
||||
}
|
||||
|
||||
// should not start threads if queue size is invalid
|
||||
TEST_CASE("start_stop_clbks5", "[async]") {
|
||||
bool start_called = false;
|
||||
bool stop_called = false;
|
||||
{
|
||||
async_sink::config cfg;
|
||||
cfg.on_thread_start = [&] { start_called = true; };
|
||||
cfg.on_thread_stop = [&] { stop_called = true; };
|
||||
cfg.queue_size = 0;
|
||||
REQUIRE_THROWS_AS(std::make_shared<async_sink>(cfg), spdlog::spdlog_ex);
|
||||
}
|
||||
REQUIRE_FALSE(start_called);
|
||||
REQUIRE_FALSE(stop_called);
|
||||
}
|
||||
|
||||
TEST_CASE("multi-sinks", "[async]") {
|
||||
prepare_logdir();
|
||||
auto test_sink1 = std::make_shared<test_sink_mt>();
|
||||
auto test_sink2 = std::make_shared<test_sink_mt>();
|
||||
auto test_sink3 = std::make_shared<test_sink_mt>();
|
||||
size_t messages = 1024;
|
||||
{
|
||||
async_sink::config cfg;
|
||||
cfg.sinks.push_back(test_sink1);
|
||||
cfg.sinks.push_back(test_sink2);
|
||||
cfg.sinks.push_back(test_sink3);
|
||||
auto as = std::make_shared<async_sink>(cfg);
|
||||
spdlog::logger l("async_logger", as);
|
||||
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
l.info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
require_message_count(TEST_FILENAME, messages);
|
||||
REQUIRE(test_sink1->msg_counter() == messages);
|
||||
REQUIRE(test_sink2->msg_counter() == messages);
|
||||
REQUIRE(test_sink3->msg_counter() == messages);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_tp", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, empty_tp);
|
||||
logger->info("Please throw an exception");
|
||||
TEST_CASE("level-off", "[async]") {
|
||||
const auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_level(spdlog::level::critical);
|
||||
{
|
||||
constexpr size_t messages = 256;
|
||||
constexpr size_t queue_size = 16;
|
||||
auto [logger, async_sink] = creat_async_logger(queue_size, test_sink);
|
||||
logger->flush_on(spdlog::level::critical);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
}
|
||||
// logger and async_sink are destroyed here so the queue should be emptied
|
||||
REQUIRE(test_sink->msg_counter() == 0);
|
||||
REQUIRE(test_sink->flush_counter() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("backend_ex", "[async]") {
|
||||
const auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_exception(std::runtime_error("test backend exception"));
|
||||
constexpr size_t queue_size = 16;
|
||||
auto [logger, async_sink] = creat_async_logger(queue_size, test_sink);
|
||||
REQUIRE_NOTHROW(logger->info("Hello message"));
|
||||
REQUIRE_NOTHROW(logger->flush());
|
||||
}
|
||||
|
Reference in New Issue
Block a user