2018-02-23 09:40:45 -05:00
|
|
|
#include "includes.h"
|
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
using spdlog::memory_buf_t;
|
|
|
|
|
2018-02-23 09:40:45 -05:00
|
|
|
// log to str and return it
|
2018-06-23 18:32:39 -04:00
|
|
|
template<typename... Args>
|
|
|
|
static std::string log_to_str(const std::string &msg, const Args &... args)
|
2018-02-23 09:40:45 -05:00
|
|
|
{
|
2018-02-23 11:33:03 -05: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-22 14:52:46 -04:00
|
|
|
|
2018-07-22 17:13:52 -04:00
|
|
|
oss_logger.set_formatter(std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
|
2018-06-23 18:32:39 -04:00
|
|
|
|
2018-02-23 11:33:03 -05:00
|
|
|
oss_logger.info(msg);
|
|
|
|
return oss.str();
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
2018-02-24 18:54:14 -05:00
|
|
|
|
2018-02-23 09:40:45 -05:00
|
|
|
TEST_CASE("custom eol", "[pattern_formatter]")
|
|
|
|
{
|
2018-02-23 11:33:03 -05:00
|
|
|
std::string msg = "Hello custom eol test";
|
|
|
|
std::string eol = ";)";
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str(msg, "%v", spdlog::pattern_time_type::local, ";)") == msg + eol);
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("empty format", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "") == "");
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("empty format2", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "\n") == "\n");
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("level", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") == "[info] Some message\n");
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("short level", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") == "[I] Some message\n");
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("name", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n");
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
|
|
|
{
|
2018-02-23 11:33:03 -05:00
|
|
|
auto now_tm = spdlog::details::os::localtime();
|
|
|
|
std::stringstream oss;
|
2018-03-09 08:26:33 -05: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";
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
|
2018-02-23 09:40:45 -05:00
|
|
|
}
|
2018-04-05 20:42:24 -04:00
|
|
|
|
|
|
|
TEST_CASE("color range test1", "[pattern_formatter]")
|
2018-04-05 21:06:02 -04:00
|
|
|
{
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-18 19:15:50 -04:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t buf;
|
2018-10-18 19:15:50 -04:00
|
|
|
fmt::format_to(buf, "Hello");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-10-18 19:15:50 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size()));
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 5);
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("hello", "%^%v%$", spdlog::pattern_time_type::local, "\n") == "hello\n");
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test2", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-05 21:06:02 -04:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-29 18:54:22 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 0);
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("", "%^%$", spdlog::pattern_time_type::local, "\n") == "\n");
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test3", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-05 21:06:02 -04:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
2018-10-29 18:54:22 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 3);
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test4", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-05 21:06:02 -04:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
2018-10-18 19:15:50 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2018-10-18 19:15:50 -04:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 2);
|
|
|
|
REQUIRE(msg.color_range_end == 5);
|
2018-06-23 18:32:39 -04:00
|
|
|
REQUIRE(log_to_str("ignored", "XX%^YYY%$", spdlog::pattern_time_type::local, "\n") == "XXYYY\n");
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test5", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-05 21:06:02 -04:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
2018-10-29 18:54:22 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 2);
|
|
|
|
REQUIRE(msg.color_range_end == 0);
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test6", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-05 21:06:02 -04:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
2018-10-29 18:54:22 -04:00
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2018-06-23 18:32:39 -04:00
|
|
|
formatter->format(msg, formatted);
|
2018-04-05 21:06:02 -04:00
|
|
|
REQUIRE(msg.color_range_start == 0);
|
|
|
|
REQUIRE(msg.color_range_end == 2);
|
2018-04-05 20:42:24 -04:00
|
|
|
}
|
2018-11-10 18:43:40 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Test padding
|
|
|
|
//
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("left_padded_huge", "[pattern_formatter]")
|
|
|
|
{
|
2019-07-05 03:45:37 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "[%-300n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-06-27 18:41:11 -04:00
|
|
|
"[pattern_tester ] Some message\n");
|
2018-11-10 18:43:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("left_padded_max", "[pattern_formatter]")
|
|
|
|
{
|
2019-07-05 03:45:37 -04:00
|
|
|
REQUIRE(log_to_str("Some message", "[%-64n] %v", spdlog::pattern_time_type::local, "\n") ==
|
2019-06-27 18:41:11 -04:00
|
|
|
"[pattern_tester ] Some message\n");
|
2018-11-10 18:43:40 -05:00
|
|
|
}
|
2018-11-12 08:59:03 -05:00
|
|
|
|
|
|
|
TEST_CASE("clone-default-formatter", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 08:59:03 -05:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 08:59:03 -05:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
|
|
|
|
2018-11-16 05:56:57 -05:00
|
|
|
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
|
2018-11-12 09:00:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%+");
|
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 09:00:32 -05:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 09:00:32 -05:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
2018-11-12 08:59:03 -05:00
|
|
|
|
2018-11-16 05:56:57 -05:00
|
|
|
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
|
2018-11-12 08:59:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("clone-formatter", "[pattern_formatter]")
|
|
|
|
{
|
2018-11-12 09:04:09 -05:00
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v");
|
2018-11-12 08:59:03 -05:00
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 08:59:03 -05:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 08:59:03 -05:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
|
|
|
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
|
|
|
|
}
|
2018-11-12 09:04:09 -05:00
|
|
|
|
|
|
|
TEST_CASE("clone-formatter-2", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
using spdlog::pattern_time_type;
|
|
|
|
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n");
|
|
|
|
auto formatter_2 = formatter_1->clone();
|
|
|
|
std::string logger_name = "test2";
|
2019-06-04 09:10:49 -04:00
|
|
|
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
2018-11-12 09:04:09 -05:00
|
|
|
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted_1;
|
|
|
|
memory_buf_t formatted_2;
|
2018-11-12 09:04:09 -05:00
|
|
|
formatter_1->format(msg, formatted_1);
|
|
|
|
formatter_2->format(msg, formatted_2);
|
|
|
|
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
|
|
|
|
}
|
2019-06-13 17:23:25 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Test source location formatting
|
|
|
|
//
|
2019-06-14 11:45:16 -04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static const char *test_path = "\\a\\b\\myfile.cpp";
|
|
|
|
#else
|
|
|
|
static const char *test_path = "/a/b//myfile.cpp";
|
|
|
|
#endif
|
|
|
|
|
2019-06-13 17:23:25 -04:00
|
|
|
TEST_CASE("short filename formatter-1", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
spdlog::pattern_formatter formatter("%s", spdlog::pattern_time_type::local, "");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2019-06-13 17:23:25 -04:00
|
|
|
std::string logger_name = "logger-name";
|
2019-06-18 10:05:27 -04:00
|
|
|
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
2019-06-13 17:23:25 -04:00
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
|
|
|
REQUIRE(fmt::to_string(formatted) == "myfile.cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("short filename formatter-2", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
spdlog::pattern_formatter formatter("%s:%#", spdlog::pattern_time_type::local, "");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2019-06-13 17:23:25 -04: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);
|
|
|
|
REQUIRE(fmt::to_string(formatted) == "myfile.cpp:123");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("short filename formatter-3", "[pattern_formatter]")
|
|
|
|
{
|
|
|
|
spdlog::pattern_formatter formatter("%s %v", spdlog::pattern_time_type::local, "");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2019-06-13 17:23:25 -04: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);
|
|
|
|
REQUIRE(fmt::to_string(formatted) == " Hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("full filename formatter", "[pattern_formatter]")
|
|
|
|
{
|
2019-06-14 11:45:16 -04:00
|
|
|
spdlog::pattern_formatter formatter("%g", spdlog::pattern_time_type::local, "");
|
2019-08-28 11:46:09 -04:00
|
|
|
memory_buf_t formatted;
|
2019-06-13 17:23:25 -04:00
|
|
|
std::string logger_name = "logger-name";
|
2019-06-18 10:05:27 -04:00
|
|
|
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
2019-06-13 17:23:25 -04:00
|
|
|
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
|
|
|
formatter.format(msg, formatted);
|
2019-06-14 11:45:16 -04:00
|
|
|
REQUIRE(fmt::to_string(formatted) == test_path);
|
2019-06-13 17:23:25 -04:00
|
|
|
}
|