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
|
|
|
|
2023-09-25 21:08:29 +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");
|
2023-09-25 21:40:36 +08:00
|
|
|
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
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("bactrace-empty", "[bactrace]") {
|
2022-12-09 16:25:17 +08:00
|
|
|
using spdlog::sinks::test_sink_st;
|
|
|
|
auto test_sink = std::make_shared<test_sink_st>();
|
|
|
|
size_t backtrace_size = 5;
|
|
|
|
|
|
|
|
spdlog::logger logger("test-backtrace", test_sink);
|
|
|
|
logger.set_pattern("%v");
|
|
|
|
logger.enable_backtrace(backtrace_size);
|
|
|
|
logger.dump_backtrace();
|
|
|
|
REQUIRE(test_sink->lines().size() == 0);
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +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);
|
2023-09-25 21:08:29 +08:00
|
|
|
auto logger = std::make_shared<spdlog::async_logger>("test-bactrace-async", test_sink,
|
|
|
|
spdlog::thread_pool());
|
2019-08-28 01:22:07 +08:00
|
|
|
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");
|
2023-09-25 21:40:36 +08:00
|
|
|
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();
|
2023-09-25 21:40:36 +08:00
|
|
|
sleep_for_millis(100); // give time for the async dump to complete
|
2019-08-28 01:22:07 +08:00
|
|
|
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 ********************");
|
|
|
|
}
|