2018-02-23 22:40:45 +08:00
|
|
|
#include "includes.h"
|
2019-11-14 18:28:23 +08:00
|
|
|
#include "test_sink.h"
|
2018-02-23 22:40:45 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
using spdlog::memory_buf_t;
|
2022-11-13 05:07:11 +08:00
|
|
|
using spdlog::details::to_string_view;
|
2019-08-28 23:46:09 +08:00
|
|
|
|
2018-02-23 22:40:45 +08:00
|
|
|
// log to str and return it
|
2023-09-25 21:08:29 +08:00
|
|
|
template <typename... Args>
|
|
|
|
static std::string log_to_str(const std::string &msg, const Args &...args) {
|
2018-02-24 00:33:03 +08:00
|
|
|
std::ostringstream oss;
|
|
|
|
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
|
|
|
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
|
|
|
oss_logger.set_level(spdlog::level::info);
|
2018-07-23 02:52:46 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
oss_logger.set_formatter(
|
|
|
|
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
|
2018-06-24 06:32:39 +08:00
|
|
|
|
2018-02-24 00:33:03 +08:00
|
|
|
oss_logger.info(msg);
|
|
|
|
return oss.str();
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
2018-02-25 07:54:14 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("custom eol", "[pattern_formatter]") {
|
2018-02-24 00:33:03 +08:00
|
|
|
std::string msg = "Hello custom eol test";
|
|
|
|
std::string eol = ";)";
|
2018-06-24 06:32:39 +08:00
|
|
|
REQUIRE(log_to_str(msg, "%v", spdlog::pattern_time_type::local, ";)") == msg + eol);
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("empty format", "[pattern_formatter]") {
|
2020-04-08 23:17:21 +08:00
|
|
|
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "").empty());
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("empty format2", "[pattern_formatter]") {
|
2018-06-24 06:32:39 +08:00
|
|
|
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "\n") == "\n");
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("level", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[info] Some message\n");
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short level", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[I] Some message\n");
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("name", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pattern_tester] Some message\n");
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
|
2018-02-24 00:33:03 +08:00
|
|
|
auto now_tm = spdlog::details::os::localtime();
|
|
|
|
std::stringstream oss;
|
2023-09-25 21:08:29 +08:00
|
|
|
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << std::setw(2)
|
|
|
|
<< now_tm.tm_mday << "/" << std::setw(2) << (now_tm.tm_year + 1900) % 1000
|
|
|
|
<< " Some message\n";
|
|
|
|
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
oss.str());
|
2018-02-23 22:40:45 +08:00
|
|
|
}
|
2018-04-06 08:42:24 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test1", "[pattern_formatter]") {
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
|
|
|
"%^%v%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-19 07:15:50 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t buf;
|
2021-11-14 00:29:05 +08:00
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(buf), "Hello");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-10-19 07:15:50 +08:00
|
|
|
std::string logger_name = "test";
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info,
|
|
|
|
spdlog::string_view_t(buf.data(), buf.size()));
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 5);
|
2018-06-24 06:32:39 +08:00
|
|
|
REQUIRE(log_to_str("hello", "%^%v%$", spdlog::pattern_time_type::local, "\n") == "hello\n");
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test2", "[pattern_formatter]") {
|
|
|
|
auto formatter =
|
|
|
|
std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-30 06:54:22 +08:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 0);
|
2018-06-24 06:32:39 +08:00
|
|
|
REQUIRE(log_to_str("", "%^%$", spdlog::pattern_time_type::local, "\n") == "\n");
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test3", "[pattern_formatter]") {
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
2018-10-30 06:54:22 +08:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 3);
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test4", "[pattern_formatter]") {
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
|
|
|
"XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-19 07:15:50 +08:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2018-10-19 07:15:50 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 2);
|
|
|
|
REQUIRE(msg.color_range_end == 5);
|
2023-09-25 21:08:29 +08:00
|
|
|
REQUIRE(log_to_str("ignored", "XX%^YYY%$", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"XXYYY\n");
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test5", "[pattern_formatter]") {
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
2018-10-30 06:54:22 +08:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 2);
|
|
|
|
REQUIRE(msg.color_range_end == 0);
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("color range test6", "[pattern_formatter]") {
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
2018-10-30 06:54:22 +08:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2018-06-24 06:32:39 +08:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 2);
|
2018-04-06 08:42:24 +08:00
|
|
|
}
|
2018-11-11 07:43:40 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Test padding
|
|
|
|
//
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("level_left_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ info] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ info] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("level_right_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[info ] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[info ] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("level_center_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ info ] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ info ] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short level_left_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ I] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ I] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short level_right_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[I ] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[I ] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short level_center_padded", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ I ] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[ I ] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("left_padded_short", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pattern_tester] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pat] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("right_padded_short", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pattern_tester] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pat] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("center_padded_short", "[pattern_formatter]") {
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pattern_tester] Some message\n");
|
|
|
|
REQUIRE(log_to_str("Some message", "[%=3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
|
|
|
"[pat] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("left_padded_huge", "[pattern_formatter]") {
|
2019-07-05 15:45:37 +08:00
|
|
|
REQUIRE(log_to_str("Some message", "[%-300n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-11-29 18:50:54 +08:00
|
|
|
"[pattern_tester ] Some message\n");
|
2019-11-14 18:28:23 +08:00
|
|
|
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-300!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-11-29 18:50:54 +08:00
|
|
|
"[pattern_tester ] Some message\n");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("left_padded_max", "[pattern_formatter]") {
|
2019-07-05 15:45:37 +08:00
|
|
|
REQUIRE(log_to_str("Some message", "[%-64n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-11-29 18:50:54 +08:00
|
|
|
"[pattern_tester ] Some message\n");
|
2019-11-14 18:28:23 +08:00
|
|
|
|
|
|
|
REQUIRE(log_to_str("Some message", "[%-64!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-11-29 18:50:54 +08:00
|
|
|
"[pattern_tester ] Some message\n");
|
2019-11-14 18:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test padding + truncate flag
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("paddinng_truncate", "[pattern_formatter]") {
|
2019-11-14 18:28:23 +08:00
|
|
|
REQUIRE(log_to_str("123456", "%6!v", spdlog::pattern_time_type::local, "\n") == "123456\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%5!v", spdlog::pattern_time_type::local, "\n") == "12345\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%7!v", spdlog::pattern_time_type::local, "\n") == " 123456\n");
|
|
|
|
|
|
|
|
REQUIRE(log_to_str("123456", "%-6!v", spdlog::pattern_time_type::local, "\n") == "123456\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%-5!v", spdlog::pattern_time_type::local, "\n") == "12345\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%-7!v", spdlog::pattern_time_type::local, "\n") == "123456 \n");
|
|
|
|
|
|
|
|
REQUIRE(log_to_str("123456", "%=6!v", spdlog::pattern_time_type::local, "\n") == "123456\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%=5!v", spdlog::pattern_time_type::local, "\n") == "12345\n");
|
|
|
|
REQUIRE(log_to_str("123456", "%=7!v", spdlog::pattern_time_type::local, "\n") == "123456 \n");
|
|
|
|
|
|
|
|
REQUIRE(log_to_str("123456", "%0!v", spdlog::pattern_time_type::local, "\n") == "\n");
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("padding_truncate_funcname", "[pattern_formatter]") {
|
2019-11-14 18:28:23 +08:00
|
|
|
spdlog::sinks::test_sink_st test_sink;
|
|
|
|
|
2019-11-29 18:50:54 +08:00
|
|
|
const char *pattern = "%v [%5!!]";
|
2019-11-14 18:28:23 +08:00
|
|
|
auto formatter = std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(pattern));
|
|
|
|
test_sink.set_formatter(std::move(formatter));
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger",
|
|
|
|
spdlog::level::info, "message"};
|
2019-11-14 18:28:23 +08:00
|
|
|
test_sink.log(msg1);
|
2020-08-30 20:18:33 +08:00
|
|
|
REQUIRE(test_sink.lines()[0] == "message [ func]");
|
2019-11-14 18:28:23 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "function"}, "test_logger",
|
|
|
|
spdlog::level::info, "message"};
|
2019-11-14 18:28:23 +08:00
|
|
|
test_sink.log(msg2);
|
2020-08-30 20:18:33 +08:00
|
|
|
REQUIRE(test_sink.lines()[1] == "message [funct]");
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("padding_funcname", "[pattern_formatter]") {
|
2020-08-30 20:18:33 +08:00
|
|
|
spdlog::sinks::test_sink_st test_sink;
|
2019-11-14 18:28:23 +08:00
|
|
|
|
2020-08-30 20:18:33 +08:00
|
|
|
const char *pattern = "%v [%10!]";
|
|
|
|
auto formatter = std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(pattern));
|
|
|
|
test_sink.set_formatter(std::move(formatter));
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger",
|
|
|
|
spdlog::level::info, "message"};
|
2020-08-30 20:18:33 +08:00
|
|
|
test_sink.log(msg1);
|
|
|
|
REQUIRE(test_sink.lines()[0] == "message [ func]");
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "func567890123"}, "test_logger",
|
|
|
|
spdlog::level::info, "message"};
|
2020-08-30 20:18:33 +08:00
|
|
|
test_sink.log(msg2);
|
|
|
|
REQUIRE(test_sink.lines()[1] == "message [func567890123]");
|
2018-11-11 07:43:40 +08:00
|
|
|
}
|
2018-11-12 21:59:03 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("clone-default-formatter", "[pattern_formatter]") {
|
2018-11-12 21:59:03 +08:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 21:59:03 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 21:59:03 +08:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
2018-11-12 22:00:32 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("clone-default-formatter2", "[pattern_formatter]") {
|
2018-11-12 22:00:32 +08:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%+");
|
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 22:00:32 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 22:00:32 +08:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
2018-11-12 21:59:03 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
2018-11-12 21:59:03 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("clone-formatter", "[pattern_formatter]") {
|
2018-11-12 22:04:09 +08:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v");
|
2018-11-12 21:59:03 +08:00
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 21:59:03 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 21:59:03 +08:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
2018-11-12 21:59:03 +08:00
|
|
|
}
|
2018-11-12 22:04:09 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("clone-formatter-2", "[pattern_formatter]") {
|
2018-11-12 22:04:09 +08:00
|
|
|
using spdlog::pattern_time_type;
|
2023-09-25 21:08:29 +08:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>(
|
|
|
|
"%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n");
|
2018-11-12 22:04:09 +08:00
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test2";
|
2019-06-04 21:10:49 +08:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 22:04:09 +08:00
|
|
|
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 22:04:09 +08:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
2018-11-12 22:04:09 +08:00
|
|
|
}
|
2019-06-14 05:23:25 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
class custom_test_flag : public spdlog::custom_flag_formatter {
|
2020-03-20 22:09:30 +08:00
|
|
|
public:
|
2020-04-10 20:35:58 +08:00
|
|
|
explicit custom_test_flag(std::string txt)
|
2023-09-25 21:08:29 +08:00
|
|
|
: some_txt{std::move(txt)} {}
|
2020-03-20 22:09:30 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
void format(const spdlog::details::log_msg &,
|
|
|
|
const std::tm &tm,
|
|
|
|
spdlog::memory_buf_t &dest) override {
|
|
|
|
if (some_txt == "throw_me") {
|
2020-03-21 21:03:41 +08:00
|
|
|
throw spdlog::spdlog_ex("custom_flag_exception_test");
|
2023-09-25 21:08:29 +08:00
|
|
|
} else if (some_txt == "time") {
|
|
|
|
auto formatted = spdlog::fmt_lib::format("{:d}:{:02d}{:s}", tm.tm_hour % 12, tm.tm_min,
|
|
|
|
tm.tm_hour / 12 ? "PM" : "AM");
|
2022-05-07 20:39:10 +08:00
|
|
|
dest.append(formatted.data(), formatted.data() + formatted.size());
|
|
|
|
return;
|
|
|
|
}
|
2020-03-21 21:25:26 +08:00
|
|
|
some_txt = std::string(padinfo_.width_, ' ') + some_txt;
|
2020-03-20 22:09:30 +08:00
|
|
|
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
|
|
|
}
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::padding_info get_padding_info() { return padinfo_; }
|
2020-03-20 22:09:30 +08:00
|
|
|
|
|
|
|
std::string some_txt;
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
std::unique_ptr<custom_flag_formatter> clone() const override {
|
2020-03-20 22:09:30 +08:00
|
|
|
return spdlog::details::make_unique<custom_test_flag>(some_txt);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// test clone with custom flag formatters
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("clone-custom_formatter", "[pattern_formatter]") {
|
2020-03-21 21:03:41 +08:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter_1->add_flag<custom_test_flag>('t', "custom_output").set_pattern("[%n] [%t] %v");
|
2020-03-20 22:09:30 +08:00
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "logger-name";
|
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
|
|
|
|
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
|
|
|
formatter_1->format(msg, formatted_1);
|
2020-03-21 21:03:41 +08:00
|
|
|
formatter_2->format(msg, formatted_2);
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
auto expected = spdlog::fmt_lib::format("[logger-name] [custom_output] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted_1) == expected);
|
|
|
|
REQUIRE(to_string_view(formatted_2) == expected);
|
2020-03-20 22:09:30 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 05:23:25 +08:00
|
|
|
//
|
|
|
|
// Test source location formatting
|
|
|
|
//
|
2019-06-14 23:45:16 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-01-06 16:55:46 +08:00
|
|
|
static const char *const test_path = "\\a\\b\\c/myfile.cpp";
|
2019-06-14 23:45:16 +08:00
|
|
|
#else
|
2020-04-10 20:35:58 +08:00
|
|
|
static const char *const test_path = "/a/b//myfile.cpp";
|
2019-06-14 23:45:16 +08:00
|
|
|
#endif
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short filename formatter-1", "[pattern_formatter]") {
|
2019-06-14 05:23:25 +08:00
|
|
|
spdlog::pattern_formatter formatter("%s", spdlog::pattern_time_type::local, "");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2019-06-14 05:23:25 +08:00
|
|
|
std::string logger_name = "logger-name";
|
2019-06-18 22:05:27 +08:00
|
|
|
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
2019-06-14 05:23:25 +08:00
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == "myfile.cpp");
|
2019-06-14 05:23:25 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short filename formatter-2", "[pattern_formatter]") {
|
2019-06-14 05:23:25 +08:00
|
|
|
spdlog::pattern_formatter formatter("%s:%#", spdlog::pattern_time_type::local, "");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2019-06-14 05:23:25 +08:00
|
|
|
std::string logger_name = "logger-name";
|
|
|
|
spdlog::source_loc source_loc{"myfile.cpp", 123, "some_func()"};
|
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == "myfile.cpp:123");
|
2019-06-14 05:23:25 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("short filename formatter-3", "[pattern_formatter]") {
|
2019-06-14 05:23:25 +08:00
|
|
|
spdlog::pattern_formatter formatter("%s %v", spdlog::pattern_time_type::local, "");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2019-06-14 05:23:25 +08:00
|
|
|
std::string logger_name = "logger-name";
|
|
|
|
spdlog::source_loc source_loc{"", 123, "some_func()"};
|
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == " Hello");
|
2019-06-14 05:23:25 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("full filename formatter", "[pattern_formatter]") {
|
2019-06-14 23:45:16 +08:00
|
|
|
spdlog::pattern_formatter formatter("%g", spdlog::pattern_time_type::local, "");
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2019-06-14 05:23:25 +08:00
|
|
|
std::string logger_name = "logger-name";
|
2019-06-18 22:05:27 +08:00
|
|
|
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
2019-06-14 05:23:25 +08:00
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == test_path);
|
2019-06-14 05:23:25 +08:00
|
|
|
}
|
2020-03-20 22:09:30 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("custom flags", "[pattern_formatter]") {
|
2020-03-21 21:03:41 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
2023-09-25 21:08:29 +08:00
|
|
|
formatter->add_flag<custom_test_flag>('t', "custom1")
|
|
|
|
.add_flag<custom_test_flag>('u', "custom2")
|
|
|
|
.set_pattern("[%n] [%t] [%u] %v");
|
2020-03-21 21:03:41 +08:00
|
|
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
2020-03-21 21:03:41 +08:00
|
|
|
formatter->format(msg, formatted);
|
2023-09-25 21:08:29 +08:00
|
|
|
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [custom2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
2020-03-21 21:03:41 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("custom flags-padding", "[pattern_formatter]") {
|
2020-03-21 21:03:41 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
2023-09-25 21:08:29 +08:00
|
|
|
formatter->add_flag<custom_test_flag>('t', "custom1")
|
|
|
|
.add_flag<custom_test_flag>('u', "custom2")
|
|
|
|
.set_pattern("[%n] [%t] [%5u] %v");
|
2020-03-21 21:03:41 +08:00
|
|
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
2020-03-21 21:03:41 +08:00
|
|
|
formatter->format(msg, formatted);
|
2023-09-25 21:08:29 +08:00
|
|
|
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [ custom2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
2021-11-14 00:29:05 +08:00
|
|
|
|
2022-04-22 09:46:58 +08:00
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
2020-03-21 21:03:41 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("custom flags-exception", "[pattern_formatter]") {
|
2020-03-21 21:03:41 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
2023-09-25 21:08:29 +08:00
|
|
|
formatter->add_flag<custom_test_flag>('t', "throw_me")
|
|
|
|
.add_flag<custom_test_flag>('u', "custom2")
|
|
|
|
.set_pattern("[%n] [%t] [%u] %v");
|
2020-03-21 21:03:41 +08:00
|
|
|
|
2020-03-20 22:09:30 +08:00
|
|
|
memory_buf_t formatted;
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
2020-03-21 21:03:41 +08:00
|
|
|
CHECK_THROWS_AS(formatter->format(msg, formatted), spdlog::spdlog_ex);
|
2020-03-20 22:09:30 +08:00
|
|
|
}
|
2022-05-07 20:39:10 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
TEST_CASE("override need_localtime", "[pattern_formatter]") {
|
|
|
|
auto formatter =
|
|
|
|
std::make_shared<spdlog::pattern_formatter>(spdlog::pattern_time_type::local, "\n");
|
2022-05-07 20:39:10 +08:00
|
|
|
formatter->add_flag<custom_test_flag>('t', "time").set_pattern("%t> %v");
|
|
|
|
|
|
|
|
{
|
|
|
|
memory_buf_t formatted;
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
2022-05-07 20:39:10 +08:00
|
|
|
formatter->format(msg, formatted);
|
|
|
|
REQUIRE(to_string_view(formatted) == "0:00AM> some message\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
formatter->need_localtime();
|
|
|
|
|
|
|
|
auto now_tm = spdlog::details::os::localtime();
|
|
|
|
std::stringstream oss;
|
2023-09-25 21:08:29 +08:00
|
|
|
oss << (now_tm.tm_hour % 12) << ":" << std::setfill('0') << std::setw(2) << now_tm.tm_min
|
|
|
|
<< (now_tm.tm_hour / 12 ? "PM" : "AM") << "> some message\n";
|
2022-05-07 20:39:10 +08:00
|
|
|
|
|
|
|
memory_buf_t formatted;
|
2023-09-25 21:08:29 +08:00
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
2022-05-07 20:39:10 +08:00
|
|
|
formatter->format(msg, formatted);
|
|
|
|
REQUIRE(to_string_view(formatted) == oss.str());
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 23:41:46 +08:00
|
|
|
|
2024-09-13 03:27:25 +08:00
|
|
|
#ifndef SPDLOG_NO_TLS
|
2024-03-18 23:41:46 +08:00
|
|
|
TEST_CASE("mdc formatter test-1", "[pattern_formatter]") {
|
|
|
|
spdlog::mdc::put("mdc_key_1", "mdc_value_1");
|
|
|
|
spdlog::mdc::put("mdc_key_2", "mdc_value_2");
|
|
|
|
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter->set_pattern("[%n] [%l] [%&] %v");
|
|
|
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
|
|
|
formatter->format(msg, formatted);
|
|
|
|
|
|
|
|
auto expected = spdlog::fmt_lib::format(
|
|
|
|
"[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
|
|
|
|
|
|
|
SECTION("Tear down") { spdlog::mdc::clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("mdc formatter value update", "[pattern_formatter]") {
|
|
|
|
spdlog::mdc::put("mdc_key_1", "mdc_value_1");
|
|
|
|
spdlog::mdc::put("mdc_key_2", "mdc_value_2");
|
|
|
|
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter->set_pattern("[%n] [%l] [%&] %v");
|
|
|
|
|
|
|
|
memory_buf_t formatted_1;
|
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
|
|
|
formatter->format(msg, formatted_1);
|
|
|
|
|
|
|
|
auto expected = spdlog::fmt_lib::format(
|
|
|
|
"[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
|
|
|
|
REQUIRE(to_string_view(formatted_1) == expected);
|
|
|
|
|
|
|
|
spdlog::mdc::put("mdc_key_1", "new_mdc_value_1");
|
|
|
|
memory_buf_t formatted_2;
|
|
|
|
formatter->format(msg, formatted_2);
|
|
|
|
expected = spdlog::fmt_lib::format(
|
|
|
|
"[logger-name] [info] [mdc_key_1:new_mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
|
|
|
|
REQUIRE(to_string_view(formatted_2) == expected);
|
|
|
|
|
|
|
|
SECTION("Tear down") { spdlog::mdc::clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("mdc different threads", "[pattern_formatter]") {
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter->set_pattern("[%n] [%l] [%&] %v");
|
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
|
|
|
|
|
|
|
memory_buf_t formatted_2;
|
|
|
|
|
|
|
|
auto lambda_1 = [formatter, msg]() {
|
|
|
|
spdlog::mdc::put("mdc_key", "thread_1_id");
|
|
|
|
memory_buf_t formatted;
|
|
|
|
formatter->format(msg, formatted);
|
|
|
|
|
|
|
|
auto expected =
|
|
|
|
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_1_id] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
|
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto lambda_2 = [formatter, msg]() {
|
|
|
|
spdlog::mdc::put("mdc_key", "thread_2_id");
|
|
|
|
memory_buf_t formatted;
|
|
|
|
formatter->format(msg, formatted);
|
|
|
|
|
|
|
|
auto expected =
|
|
|
|
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_2_id] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
|
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread thread_1(lambda_1);
|
|
|
|
std::thread thread_2(lambda_2);
|
|
|
|
|
|
|
|
thread_1.join();
|
|
|
|
thread_2.join();
|
|
|
|
|
|
|
|
SECTION("Tear down") { spdlog::mdc::clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("mdc remove key", "[pattern_formatter]") {
|
|
|
|
spdlog::mdc::put("mdc_key_1", "mdc_value_1");
|
|
|
|
spdlog::mdc::put("mdc_key_2", "mdc_value_2");
|
|
|
|
spdlog::mdc::remove("mdc_key_1");
|
|
|
|
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter->set_pattern("[%n] [%l] [%&] %v");
|
|
|
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
|
|
|
formatter->format(msg, formatted);
|
|
|
|
|
|
|
|
auto expected =
|
|
|
|
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_2:mdc_value_2] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
|
|
|
|
|
|
|
SECTION("Tear down") { spdlog::mdc::clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("mdc empty", "[pattern_formatter]") {
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
formatter->set_pattern("[%n] [%l] [%&] %v");
|
|
|
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
|
|
|
"some message");
|
|
|
|
formatter->format(msg, formatted);
|
|
|
|
|
|
|
|
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [] some message{}",
|
|
|
|
spdlog::details::os::default_eol);
|
|
|
|
REQUIRE(to_string_view(formatted) == expected);
|
|
|
|
|
|
|
|
SECTION("Tear down") { spdlog::mdc::clear(); }
|
|
|
|
}
|
2024-09-13 03:27:25 +08:00
|
|
|
#endif
|