spdlog/tests/test_pattern_formatter.cpp

62 lines
2.4 KiB
C++
Raw Normal View History

2018-02-23 09:40:45 -05:00
#include "includes.h"
// log to str and return it
2018-02-24 18:54:14 -05:00
static std::string log_to_str(const std::string& msg, const std::shared_ptr<spdlog::formatter>& formatter = nullptr)
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);
if (formatter) oss_logger.set_formatter(formatter);
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 = ";)";
auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)");
2018-02-23 09:40:45 -05:00
2018-02-23 11:33:03 -05:00
REQUIRE(log_to_str(msg, formatter) == msg + eol);
2018-02-23 09:40:45 -05:00
}
TEST_CASE("empty format", "[pattern_formatter]")
{
2018-02-23 11:33:03 -05:00
auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "");
REQUIRE(log_to_str("Some message", formatter) == "");
2018-02-23 09:40:45 -05:00
}
TEST_CASE("empty format2", "[pattern_formatter]")
{
2018-02-23 11:33:03 -05:00
auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "\n");
2018-02-23 09:40:45 -05:00
}
TEST_CASE("level", "[pattern_formatter]")
{
2018-02-23 11:33:03 -05:00
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%l] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n");
2018-02-23 09:40:45 -05:00
}
TEST_CASE("short level", "[pattern_formatter]")
{
2018-02-23 11:33:03 -05:00
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[I] Some message\n");
2018-02-23 09:40:45 -05:00
}
TEST_CASE("name", "[pattern_formatter]")
{
2018-02-23 11:33:03 -05:00
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%n] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[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 formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");
auto now_tm = spdlog::details::os::localtime();
std::stringstream oss;
2018-03-06 03:31:36 -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-02-23 11:33:03 -05:00
REQUIRE(log_to_str("Some message", formatter) == oss.str());
2018-02-23 09:40:45 -05:00
}