spdlog/tests/test_misc.cpp

202 lines
7.7 KiB
C++
Raw Normal View History

2024-12-02 05:59:09 +08:00
#ifdef _WIN32 // to prevent fopen warning on windows
#define _CRT_SECURE_NO_WARNINGS
2024-11-30 21:52:22 +08:00
#endif
#include <cstdio>
#include <cstring>
2024-12-02 05:59:09 +08:00
2015-05-16 01:30:37 +08:00
#include "includes.h"
#include "spdlog/details/os.h"
2024-12-02 05:59:09 +08:00
#include "spdlog/sinks/ostream_sink.h"
2024-12-07 21:29:49 +08:00
#include "spdlog/sinks/async_sink.h"
#include "test_sink.h"
2019-08-29 06:05:23 +08:00
template <class T>
2024-12-02 05:59:09 +08:00
std::string log_info(const T& what, spdlog::level 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
2018-02-05 12:36:54 +08:00
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
2015-05-16 01:30:37 +08:00
}
TEST_CASE("basic_logging ", "[basic_logging]") {
// const char
2015-05-16 01:30:37 +08:00
REQUIRE(log_info("Hello") == "Hello");
2020-04-08 23:17:21 +08:00
REQUIRE(log_info("").empty());
2015-05-16 01:30:37 +08:00
// std::string
2015-05-16 01:30:37 +08:00
REQUIRE(log_info(std::string("Hello")) == "Hello");
2020-04-08 23:17:21 +08:00
REQUIRE(log_info(std::string()).empty());
2015-05-16 01:30:37 +08:00
}
TEST_CASE("log_levels", "[log_levels]") {
REQUIRE(log_info("Hello", spdlog::level::err).empty());
REQUIRE(log_info("Hello", spdlog::level::critical).empty());
REQUIRE(log_info("Hello", spdlog::level::info) == "Hello");
REQUIRE(log_info("Hello", spdlog::level::debug) == "Hello");
REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello");
2015-05-16 01:30:37 +08:00
}
TEST_CASE("level_to_string_view", "[convert_to_string_view]") {
REQUIRE(spdlog::to_string_view(spdlog::level::trace) == "trace");
REQUIRE(spdlog::to_string_view(spdlog::level::debug) == "debug");
REQUIRE(spdlog::to_string_view(spdlog::level::info) == "info");
REQUIRE(spdlog::to_string_view(spdlog::level::warn) == "warning");
REQUIRE(spdlog::to_string_view(spdlog::level::err) == "error");
REQUIRE(spdlog::to_string_view(spdlog::level::critical) == "critical");
REQUIRE(spdlog::to_string_view(spdlog::level::off) == "off");
}
2023-09-26 06:49:32 +08:00
TEST_CASE("to_short_string_view", "[convert_to_short_c_str]") {
REQUIRE(spdlog::to_short_string_view(spdlog::level::trace) == "T");
REQUIRE(spdlog::to_short_string_view(spdlog::level::debug) == "D");
REQUIRE(spdlog::to_short_string_view(spdlog::level::info) == "I");
REQUIRE(spdlog::to_short_string_view(spdlog::level::warn) == "W");
REQUIRE(spdlog::to_short_string_view(spdlog::level::err) == "E");
REQUIRE(spdlog::to_short_string_view(spdlog::level::critical) == "C");
REQUIRE(spdlog::to_short_string_view(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("warn") == 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("clone-logger", "[clone]") {
2020-04-08 23:17:21 +08:00
using spdlog::sinks::test_sink_mt;
auto test_sink = std::make_shared<test_sink_mt>();
2019-08-29 06:05:23 +08:00
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->log_level() == cloned->log_level());
2019-08-29 06:05:23 +08:00
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
2019-09-05 06:25:00 +08:00
REQUIRE(test_sink->lines().size() == 2);
2019-08-29 06:05:23 +08:00
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
}
TEST_CASE("clone async", "[clone]") {
2024-12-07 06:52:27 +08:00
// TODO: fix this test
2024-12-07 21:29:49 +08:00
using spdlog::sinks::test_sink_mt;
auto async_sink = std::make_shared<spdlog::sinks::async_sink_mt>(32);
auto test_sink = std::make_shared<test_sink_mt>();
async_sink->add_sink(test_sink);
auto logger = std::make_shared<spdlog::logger>("orig", async_sink);
logger->set_pattern("%v");
auto cloned = logger->clone("clone");
REQUIRE(cloned->name() == "clone");
REQUIRE(logger->sinks() == cloned->sinks());
REQUIRE(logger->log_level() == cloned->log_level());
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
spdlog::details::os::sleep_for_millis(100);
REQUIRE(test_sink->lines().size() == 2);
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
2019-08-29 06:05:23 +08:00
}
2024-12-07 01:21:42 +08:00
TEST_CASE("global logger API", "[global logger]") {
2018-10-12 08:04:55 +08:00
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
2024-12-07 01:21:42 +08:00
spdlog::set_global_logger(std::make_shared<spdlog::logger>("oss", oss_sink));
2018-10-14 00:23:11 +08:00
spdlog::set_pattern("*** %v");
2018-10-12 08:04:55 +08:00
2024-12-07 01:21:42 +08:00
spdlog::global_logger()->set_level(spdlog::level::trace);
2018-10-12 08:04:55 +08:00
spdlog::trace("hello trace");
2018-10-14 00:23:11 +08:00
REQUIRE(oss.str() == "*** hello trace" + std::string(spdlog::details::os::default_eol));
2018-10-12 08:04:55 +08:00
oss.str("");
spdlog::debug("hello debug");
2018-10-14 00:23:11 +08:00
REQUIRE(oss.str() == "*** hello debug" + std::string(spdlog::details::os::default_eol));
2018-10-12 08:04:55 +08:00
oss.str("");
spdlog::info("Hello");
2018-10-14 00:23:11 +08:00
REQUIRE(oss.str() == "*** Hello" + std::string(spdlog::details::os::default_eol));
2018-10-12 08:04:55 +08:00
oss.str("");
spdlog::warn("Hello again {}", 2);
2018-10-14 00:23:11 +08:00
REQUIRE(oss.str() == "*** Hello again 2" + std::string(spdlog::details::os::default_eol));
2018-10-12 08:04:55 +08:00
oss.str("");
spdlog::critical(std::string("some string"));
2018-10-14 00:23:11 +08:00
REQUIRE(oss.str() == "*** some string" + std::string(spdlog::details::os::default_eol));
2018-10-12 08:04:55 +08:00
oss.str("");
spdlog::set_level(spdlog::level::info);
2018-10-12 08:04:55 +08:00
spdlog::debug("should not be logged");
REQUIRE(oss.str().empty());
2018-10-14 00:23:11 +08:00
spdlog::set_pattern("%v");
2018-10-12 08:04:55 +08:00
}
2024-11-30 04:49:59 +08:00
#if defined(_WIN32)
TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") {
spdlog::wmemory_buf_t buffer;
spdlog::details::os::utf8_to_wstrbuf("", buffer);
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L""));
spdlog::details::os::utf8_to_wstrbuf("abc", buffer);
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"abc"));
2024-12-02 05:59:09 +08:00
spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence.
2024-11-30 04:49:59 +08:00
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\xfffd("));
2024-12-02 05:59:09 +08:00
spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana.
2024-11-30 04:49:59 +08:00
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053"));
}
#endif
struct auto_closer {
FILE* fp = nullptr;
2024-12-02 05:59:09 +08:00
explicit auto_closer(FILE* f)
: fp(f) {}
auto_closer(const auto_closer&) = delete;
auto_closer& operator=(const auto_closer&) = delete;
~auto_closer() {
2024-11-30 21:52:22 +08:00
if (fp != nullptr) (void)std::fclose(fp);
}
};
TEST_CASE("os::fwrite_bytes", "[os]") {
2024-11-30 21:55:23 +08:00
using spdlog::details::os::create_dir;
2024-12-02 05:59:09 +08:00
using spdlog::details::os::fwrite_bytes;
2024-11-30 21:55:23 +08:00
const char* filename = "log_tests/test_fwrite_bytes.txt";
2024-12-02 05:59:09 +08:00
const char* msg = "hello";
2024-11-30 21:55:23 +08:00
prepare_logdir();
REQUIRE(create_dir("log_tests"));
{
auto_closer closer(std::fopen(filename, "wb"));
REQUIRE(closer.fp != nullptr);
REQUIRE(fwrite_bytes(msg, std::strlen(msg), closer.fp) == true);
REQUIRE(fwrite_bytes(msg, 0, closer.fp) == true);
std::fflush(closer.fp);
REQUIRE(spdlog::details::os::filesize(closer.fp) == 5);
}
// fwrite_bytes should return false on write failure
auto_closer closer(std::fopen(filename, "r"));
REQUIRE(closer.fp != nullptr);
REQUIRE_FALSE(fwrite_bytes("Hello", 5, closer.fp));
}