spdlog/tests/test_misc.cpp

223 lines
7.8 KiB
C++
Raw Normal View History

2015-05-15 13:30:37 -04:00
#include "includes.h"
#include "test_sink.h"
2019-08-28 18:05:23 -04:00
#include "spdlog/fmt/bin_to_hex.h"
2018-03-16 11:35:56 -04:00
template<class T>
std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
2015-05-15 14:45:10 -04:00
{
2015-05-15 13:30:37 -04: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-02 20:43:55 -04:00
oss_logger.info(what);
2016-05-14 18:01:01 -04:00
2018-02-04 23:36:54 -05:00
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
2015-05-15 13:30:37 -04:00
}
2015-05-15 14:45:10 -04:00
TEST_CASE("basic_logging ", "[basic_logging]")
{
// const char
2015-05-15 13:30:37 -04:00
REQUIRE(log_info("Hello") == "Hello");
REQUIRE(log_info("") == "");
// std::string
2015-05-15 13:30:37 -04:00
REQUIRE(log_info(std::string("Hello")) == "Hello");
REQUIRE(log_info(std::string()) == std::string());
// Numbers
2015-05-15 13:30:37 -04:00
REQUIRE(log_info(5) == "5");
REQUIRE(log_info(5.6) == "5.6");
// User defined class
// REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
2015-05-15 13:30:37 -04:00
}
TEST_CASE("log_levels", "[log_levels]")
{
REQUIRE(log_info("Hello", spdlog::level::err) == "");
2016-07-08 18:00:16 -04:00
REQUIRE(log_info("Hello", spdlog::level::critical) == "");
2015-05-15 13:30:37 -04: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");
}
2018-11-24 17:36:14 -05:00
TEST_CASE("level_to_string_view", "[convert_to_string_view")
{
2018-11-24 17:36:14 -05:00
REQUIRE(spdlog::level::to_string_view(spdlog::level::trace) == "trace");
REQUIRE(spdlog::level::to_string_view(spdlog::level::debug) == "debug");
REQUIRE(spdlog::level::to_string_view(spdlog::level::info) == "info");
REQUIRE(spdlog::level::to_string_view(spdlog::level::warn) == "warning");
REQUIRE(spdlog::level::to_string_view(spdlog::level::err) == "error");
REQUIRE(spdlog::level::to_string_view(spdlog::level::critical) == "critical");
REQUIRE(spdlog::level::to_string_view(spdlog::level::off) == "off");
}
2018-07-04 02:59:26 -04:00
TEST_CASE("to_short_c_str", "[convert_to_short_c_str]")
{
2018-07-04 02:59:26 -04:00
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::trace)) == "T");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::debug)) == "D");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::info)) == "I");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::warn)) == "W");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::err)) == "E");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::critical)) == "C");
REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::off)) == "O");
}
TEST_CASE("to_level_enum", "[convert_to_level_enum]")
{
REQUIRE(spdlog::level::from_str("trace") == spdlog::level::trace);
REQUIRE(spdlog::level::from_str("debug") == spdlog::level::debug);
REQUIRE(spdlog::level::from_str("info") == spdlog::level::info);
REQUIRE(spdlog::level::from_str("warning") == spdlog::level::warn);
REQUIRE(spdlog::level::from_str("error") == spdlog::level::err);
REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
REQUIRE(spdlog::level::from_str("null") == spdlog::level::off);
}
TEST_CASE("periodic flush", "[periodic_flush]")
{
using namespace spdlog;
auto logger = spdlog::create<sinks::test_sink_mt>("periodic_flush");
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
spdlog::flush_every(std::chrono::seconds(1));
2018-12-02 12:04:44 -05:00
std::this_thread::sleep_for(std::chrono::milliseconds(1250));
REQUIRE(test_sink->flush_counter() == 1);
spdlog::flush_every(std::chrono::seconds(0));
spdlog::drop_all();
}
2018-08-25 11:10:42 -04:00
2019-08-28 18:05:23 -04:00
TEST_CASE("clone-logger", "[clone]")
{
using namespace spdlog;
auto test_sink = std::make_shared<sinks::test_sink_mt>();
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
logger->set_pattern("%v");
auto cloned = logger->clone("clone");
REQUIRE(cloned->name() == "clone");
REQUIRE(logger->sinks() == cloned->sinks());
REQUIRE(logger->level() == cloned->level());
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
2019-09-04 18:25:00 -04:00
REQUIRE(test_sink->lines().size() == 2);
2019-08-28 18:05:23 -04:00
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
spdlog::drop_all();
}
TEST_CASE("clone async", "[clone]")
{
using namespace spdlog;
spdlog::init_thread_pool(4, 1);
2019-09-04 18:25:00 -04:00
auto test_sink = std::make_shared<sinks::test_sink_st>();
2019-08-28 18:05:23 -04:00
auto logger = std::make_shared<spdlog::async_logger>("orig", test_sink, spdlog::thread_pool());
logger->set_pattern("%v");
auto cloned = logger->clone("clone");
REQUIRE(cloned->name() == "clone");
REQUIRE(logger->sinks() == cloned->sinks());
REQUIRE(logger->level() == cloned->level());
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
spdlog::details::os::sleep_for_millis(10);
2019-09-04 18:25:00 -04:00
REQUIRE(test_sink->lines().size() == 2);
2019-08-28 18:05:23 -04:00
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
spdlog::drop_all();
}
TEST_CASE("to_hex", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
2018-09-24 18:11:36 -04:00
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_upper", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
2018-09-24 18:11:36 -04:00
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:X}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_no_delimiter", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
2018-09-24 18:11:36 -04:00
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:sX}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
}
2018-10-11 20:04:55 -04:00
TEST_CASE("default logger API", "[default logger]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::set_default_logger(std::make_shared<spdlog::logger>("oss", oss_sink));
2018-10-13 12:23:11 -04:00
spdlog::set_pattern("*** %v");
2018-10-11 20:04:55 -04:00
2018-10-13 12:23:11 -04:00
spdlog::default_logger()->set_level(spdlog::level::trace);
2018-10-11 20:04:55 -04:00
spdlog::trace("hello trace");
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** hello trace" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::debug("hello debug");
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** hello debug" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::info("Hello");
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** Hello" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::warn("Hello again {}", 2);
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** Hello again 2" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::error(123);
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** 123" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::critical(std::string("some string"));
2018-10-13 12:23:11 -04:00
REQUIRE(oss.str() == "*** some string" + std::string(spdlog::details::os::default_eol));
2018-10-11 20:04:55 -04:00
oss.str("");
spdlog::set_level(spdlog::level::info);
spdlog::debug("should not be logged");
REQUIRE(oss.str().empty());
spdlog::drop_all();
2018-10-13 12:23:11 -04:00
spdlog::set_pattern("%v");
2018-10-11 20:04:55 -04:00
}