spdlog/tests/format.cpp

74 lines
1.7 KiB
C++
Raw Normal View History

2015-05-16 01:30:37 +08:00
#include "includes.h"
template<class T>
2015-05-16 02:45:10 +08:00
std::string log_info(const T& what, spdlog::level::level_enum logger_level = spdlog::level::info)
{
2015-05-16 01:30:37 +08:00
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
oss_logger.set_level(logger_level);
oss_logger.set_pattern("%v");
2016-07-03 08:43:55 +08:00
oss_logger.info(what);
2016-05-15 06:01:01 +08:00
2016-05-15 05:53:35 +08:00
return oss.str().substr(0, oss.str().length() - spdlog::details::os::eol_size);
2015-05-16 01:30:37 +08:00
}
2015-05-16 02:45:10 +08:00
TEST_CASE("basic_logging ", "[basic_logging]")
{
2015-05-16 01:30:37 +08:00
//const char
REQUIRE(log_info("Hello") == "Hello");
REQUIRE(log_info("") == "");
//std::string
REQUIRE(log_info(std::string("Hello")) == "Hello");
REQUIRE(log_info(std::string()) == std::string());
//Numbers
REQUIRE(log_info(5) == "5");
REQUIRE(log_info(5.6) == "5.6");
//User defined class
2016-07-03 08:43:55 +08:00
//REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
2015-05-16 01:30:37 +08:00
}
TEST_CASE("log_levels", "[log_levels]")
{
REQUIRE(log_info("Hello", spdlog::level::err) == "");
2016-07-09 05:59:51 +08:00
REQUIRE(log_info("Hello", spdlog::level::critical) == "");
2015-05-16 01:30:37 +08:00
REQUIRE(log_info("Hello", spdlog::level::info) == "Hello");
REQUIRE(log_info("Hello", spdlog::level::debug) == "Hello");
REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello");
}
TEST_CASE("invalid_format", "[format]")
{
using namespace spdlog::sinks;
spdlog::logger null_logger("null_logger", std::make_shared<null_sink_st>());
REQUIRE_THROWS_AS(
null_logger.info("{} {}", "first"),
spdlog::spdlog_ex);
REQUIRE_THROWS_AS(
null_logger.info("{0:f}", "aads"),
spdlog::spdlog_ex);
REQUIRE_THROWS_AS(
null_logger.info("{0:kk}", 123),
spdlog::spdlog_ex);
}