spdlog/tests/test_errors.cpp

113 lines
4.3 KiB
C++
Raw Normal View History

2016-08-05 08:56:40 +08:00
/*
2023-09-25 21:08:29 +08:00
* This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
2023-06-07 18:23:44 +08:00
*/
2016-08-05 08:56:40 +08:00
#include "includes.h"
2018-03-09 21:26:33 +08:00
#include <iostream>
2016-08-05 08:56:40 +08:00
#define SIMPLE_LOG "test_logs/simple_log.txt"
#define SIMPLE_ASYNC_LOG "test_logs/simple_async_log.txt"
2023-09-25 21:08:29 +08:00
class failing_sink : public spdlog::sinks::base_sink<std::mutex> {
protected:
2023-09-25 21:08:29 +08:00
void sink_it_(const spdlog::details::log_msg &) final {
2023-06-07 18:23:44 +08:00
throw std::runtime_error("some error happened during log");
}
2017-03-28 07:08:18 +08:00
2023-09-25 21:08:29 +08:00
void flush_() final { throw std::runtime_error("some error happened during flush"); }
};
2023-09-25 21:08:29 +08:00
struct custom_ex {};
2024-02-13 05:02:31 +08:00
#if !defined(SPDLOG_USE_STD_FORMAT) // std format doesn't fully support runtime strings
2023-09-25 21:08:29 +08:00
TEST_CASE("default_error_handler", "[errors]") {
2023-06-07 18:23:44 +08:00
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
2016-08-05 08:56:40 +08:00
2023-06-07 18:23:44 +08:00
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v");
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
logger->info("Test message {}", 2);
logger->flush();
using spdlog::details::os::default_eol;
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 2{}", default_eol));
REQUIRE(count_lines(SIMPLE_LOG) == 1);
2016-08-05 08:56:40 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("custom_error_handler", "[errors]") {
2023-06-07 18:23:44 +08:00
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1");
2018-06-12 23:48:22 +08:00
2023-06-07 18:23:44 +08:00
REQUIRE_THROWS_AS(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"), custom_ex);
logger->info("Good message #2");
require_message_count(SIMPLE_LOG, 2);
}
2023-03-06 03:30:29 +08:00
#endif
2023-09-25 21:08:29 +08:00
TEST_CASE("default_error_handler2", "[errors]") {
2023-06-07 18:23:44 +08:00
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
2016-08-05 08:56:40 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("flush_error_handler", "[errors]") {
2023-06-07 18:23:44 +08:00
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
}
2023-03-06 03:30:29 +08:00
#if !defined(SPDLOG_USE_STD_FORMAT)
2023-09-25 21:08:29 +08:00
TEST_CASE("async_error_handler", "[errors]") {
2023-06-07 18:23:44 +08:00
prepare_logdir();
std::string err_msg("log failed with some msg");
2018-04-14 08:34:57 +08:00
2023-06-07 18:23:44 +08:00
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG);
{
spdlog::init_thread_pool(128, 1);
2023-09-25 21:08:29 +08:00
auto logger =
spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
2023-06-07 18:23:44 +08:00
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err.txt");
2023-09-25 21:08:29 +08:00
if (!ofs) {
2023-06-07 18:23:44 +08:00
throw std::runtime_error("Failed open test_logs/custom_err.txt");
}
ofs << err_msg;
});
logger->info("Good message #1");
logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx");
logger->info("Good message #2");
2023-09-25 21:40:36 +08:00
spdlog::drop("logger"); // force logger to drain the queue and shutdown
2023-06-07 18:23:44 +08:00
}
spdlog::init_thread_pool(128, 1);
require_message_count(SIMPLE_ASYNC_LOG, 2);
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
}
2023-03-06 03:30:29 +08:00
#endif
// Make sure async error handler is executed
2023-09-25 21:08:29 +08:00
TEST_CASE("async_error_handler2", "[errors]") {
2023-06-07 18:23:44 +08:00
prepare_logdir();
std::string err_msg("This is async handler error message");
{
spdlog::details::os::create_dir(SPDLOG_FILENAME_T("test_logs"));
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err2.txt");
2023-09-25 21:40:36 +08:00
if (!ofs) throw std::runtime_error("Failed open test_logs/custom_err2.txt");
2023-06-07 18:23:44 +08:00
ofs << err_msg;
});
logger->info("Hello failure");
2023-09-25 21:40:36 +08:00
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
2023-06-07 18:23:44 +08:00
}
2017-03-28 07:08:18 +08:00
2023-06-07 18:23:44 +08:00
spdlog::init_thread_pool(128, 1);
REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg);
2016-08-05 08:56:40 +08:00
}