2018-02-23 22:40:45 +08:00
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
// log to str and return it
|
2018-06-24 06:32:39 +08:00
|
|
|
template<typename... Args>
|
|
|
|
|
|
|
|
static std::string log_to_str(const std::string &msg, const Args &... args)
|
2018-02-23 22:40:45 +08:00
|
|
|
{
|
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
|
|
|
|
2018-07-23 05:13:52 +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
|
|
|
|
2018-02-23 22:40:45 +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
|
|
|
// auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)");
|
|
|
|
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter("%v", spdlog::pattern_time_type::local, ";)"));
|
2018-02-23 22:40:45 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("empty format", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-24 06:32:39 +08:00
|
|
|
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "") == "");
|
2018-02-23 22:40:45 +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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("level", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-24 06:32:39 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("short level", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-24 06:32:39 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("name", "[pattern_formatter]")
|
|
|
|
{
|
2018-06-24 06:32:39 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-03-09 21:26:33 +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";
|
2018-06-24 06:32:39 +08:00
|
|
|
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
|
|
|
|
|
|
|
TEST_CASE("color range test1", "[pattern_formatter]")
|
2018-04-06 09:06:02 +08:00
|
|
|
{
|
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::format_to(msg.raw, "Hello");
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test2", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test3", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test4", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::format_to(msg.raw, "ignored");
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
formatter->format(msg, formatted);
|
2018-04-06 09:06:02 +08:00
|
|
|
REQUIRE(msg.color_range_start == 2);
|
|
|
|
REQUIRE(msg.color_range_end == 5);
|
2018-06-24 06:32:39 +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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test5", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("color range test6", "[pattern_formatter]")
|
|
|
|
{
|
2018-04-06 09:06:02 +08:00
|
|
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
|
|
|
spdlog::details::log_msg msg;
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
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
|
|
|
}
|