spdlog/tests/test_backtrace.cpp

66 lines
2.4 KiB
C++
Raw Normal View History

2019-08-27 23:06:27 +08:00
#include "includes.h"
#include "test_sink.h"
2019-08-28 00:45:21 +08:00
#include "spdlog/async.h"
2019-08-27 23:06:27 +08:00
TEST_CASE("bactrace1", "[bactrace]")
{
2019-08-28 01:22:07 +08:00
using spdlog::sinks::test_sink_st;
auto test_sink = std::make_shared<test_sink_st>();
2019-08-28 20:59:58 +08:00
size_t backtrace_size = 5;
2019-08-27 23:06:27 +08:00
2019-08-28 01:22:07 +08:00
spdlog::logger logger("test-backtrace", test_sink);
logger.set_pattern("%v");
logger.enable_backtrace(backtrace_size);
2019-08-27 23:06:27 +08:00
2019-08-28 01:22:07 +08:00
logger.info("info message");
for (int i = 0; i < 100; i++)
logger.debug("debug message {}", i);
2019-08-27 23:06:27 +08:00
2019-08-28 01:22:07 +08:00
REQUIRE(test_sink->lines().size() == 1);
REQUIRE(test_sink->lines()[0] == "info message");
2019-08-28 00:45:21 +08:00
2019-08-28 01:22:07 +08:00
logger.dump_backtrace();
REQUIRE(test_sink->lines().size() == backtrace_size + 3);
REQUIRE(test_sink->lines()[1] == "****************** Backtrace Start ******************");
REQUIRE(test_sink->lines()[2] == "debug message 95");
REQUIRE(test_sink->lines()[3] == "debug message 96");
REQUIRE(test_sink->lines()[4] == "debug message 97");
REQUIRE(test_sink->lines()[5] == "debug message 98");
REQUIRE(test_sink->lines()[6] == "debug message 99");
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
}
2019-08-28 00:45:21 +08:00
TEST_CASE("bactrace-async", "[bactrace]")
{
2019-08-28 01:22:07 +08:00
using spdlog::sinks::test_sink_mt;
auto test_sink = std::make_shared<test_sink_mt>();
using spdlog::details::os::sleep_for_millis;
2019-08-28 00:45:21 +08:00
2019-08-28 20:59:58 +08:00
size_t backtrace_size = 5;
2019-08-28 00:45:21 +08:00
2019-08-28 01:22:07 +08:00
spdlog::init_thread_pool(120, 1);
auto logger = std::make_shared<spdlog::async_logger>("test-bactrace-async", test_sink, spdlog::thread_pool());
logger->set_pattern("%v");
logger->enable_backtrace(backtrace_size);
2019-08-28 00:45:21 +08:00
2019-08-28 01:22:07 +08:00
logger->info("info message");
for (int i = 0; i < 100; i++)
logger->debug("debug message {}", i);
2019-08-28 00:45:21 +08:00
2021-06-08 03:39:42 +08:00
sleep_for_millis(100);
2019-08-28 01:22:07 +08:00
REQUIRE(test_sink->lines().size() == 1);
REQUIRE(test_sink->lines()[0] == "info message");
2019-08-27 23:06:27 +08:00
2019-08-28 01:22:07 +08:00
logger->dump_backtrace();
sleep_for_millis(100); // give time for the async dump to complete
REQUIRE(test_sink->lines().size() == backtrace_size + 3);
REQUIRE(test_sink->lines()[1] == "****************** Backtrace Start ******************");
REQUIRE(test_sink->lines()[2] == "debug message 95");
REQUIRE(test_sink->lines()[3] == "debug message 96");
REQUIRE(test_sink->lines()[4] == "debug message 97");
REQUIRE(test_sink->lines()[5] == "debug message 98");
REQUIRE(test_sink->lines()[6] == "debug message 99");
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
}