2016-08-05 08:56:40 +08:00
|
|
|
/*
|
2023-09-25 21:05:07 +08:00
|
|
|
* This content is released under the MIT License as specified in
|
2023-10-01 17:20:48 +08:00
|
|
|
* https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
|
2023-06-07 18:23:44 +08:00
|
|
|
*/
|
2018-03-09 21:26:33 +08:00
|
|
|
#include <iostream>
|
2016-08-05 08:56:40 +08:00
|
|
|
|
2023-09-29 05:20:26 +08:00
|
|
|
#include "includes.h"
|
2023-09-29 05:45:09 +08:00
|
|
|
#include "spdlog/sinks/basic_file_sink.h"
|
2023-09-29 05:20:26 +08:00
|
|
|
|
2025-01-05 08:17:31 +08:00
|
|
|
static spdlog::filename_t log_filename = SPDLOG_FILENAME_T("test_logs/simple_log.txt");
|
|
|
|
static std::string log_err_msg = "Error during log";
|
|
|
|
static std::string flush_err_msg = "Error during flush";
|
2021-01-07 09:00:59 +08:00
|
|
|
|
2024-12-07 01:21:42 +08:00
|
|
|
class failing_sink final : public spdlog::sinks::base_sink<std::mutex> {
|
2018-07-14 21:21:53 +08:00
|
|
|
protected:
|
2025-01-05 08:17:31 +08:00
|
|
|
void sink_it_(const spdlog::details::log_msg &) override { throw std::runtime_error(log_err_msg.c_str()); }
|
|
|
|
void flush_() override { throw std::runtime_error(flush_err_msg.c_str()); }
|
2017-03-28 06:54:33 +08:00
|
|
|
};
|
2023-09-25 21:05:07 +08:00
|
|
|
struct custom_ex {};
|
2017-03-28 06:54:33 +08:00
|
|
|
|
2025-01-05 08:17:31 +08:00
|
|
|
using namespace spdlog::sinks;
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("default_error_handler", "[errors]") {
|
2023-06-07 18:23:44 +08:00
|
|
|
prepare_logdir();
|
2025-01-05 08:17:31 +08:00
|
|
|
auto logger = spdlog::create<basic_file_sink_mt>("test-error", log_filename);
|
2023-06-07 18:23:44 +08:00
|
|
|
logger->set_pattern("%v");
|
2023-10-21 18:56:34 +08:00
|
|
|
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
|
2023-06-07 18:23:44 +08:00
|
|
|
logger->info("Test message {}", 2);
|
|
|
|
logger->flush();
|
|
|
|
using spdlog::details::os::default_eol;
|
2025-01-05 08:17:31 +08:00
|
|
|
REQUIRE(file_contents(log_filename) == spdlog::fmt_lib::format("Test message 2{}", default_eol));
|
|
|
|
REQUIRE(count_lines(log_filename) == 1);
|
2016-08-05 08:56:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("custom_error_handler", "[errors]") {
|
2023-06-07 18:23:44 +08:00
|
|
|
prepare_logdir();
|
2025-01-06 16:35:08 +08:00
|
|
|
auto logger = spdlog::create<basic_file_sink_mt>("test-format-error", log_filename);
|
2023-09-23 22:59:51 +08:00
|
|
|
logger->flush_on(spdlog::level::info);
|
2025-01-18 02:59:46 +08:00
|
|
|
logger->set_error_handler([=](const std::string &msg) { REQUIRE(msg == "argument not found"); });
|
2023-06-07 18:23:44 +08:00
|
|
|
logger->info("Good message #1");
|
2025-01-06 16:35:08 +08:00
|
|
|
REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"));
|
2023-06-07 18:23:44 +08:00
|
|
|
logger->info("Good message #2");
|
2025-01-05 08:17:31 +08:00
|
|
|
require_message_count(log_filename, 2);
|
2017-03-28 06:54:33 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("default_error_handler2", "[errors]") {
|
2025-01-06 16:35:08 +08:00
|
|
|
auto logger = std::make_shared<spdlog::logger>("test-failing-sink", std::make_shared<failing_sink>());
|
2025-01-05 08:17:31 +08:00
|
|
|
logger->set_error_handler([=](const std::string &msg) {
|
|
|
|
REQUIRE(msg == log_err_msg);
|
|
|
|
throw custom_ex();
|
|
|
|
});
|
2025-01-06 16:35:08 +08:00
|
|
|
REQUIRE_NOTHROW(logger->info("Some message"));
|
2016-08-05 08:56:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("flush_error_handler", "[errors]") {
|
2025-01-06 16:35:08 +08:00
|
|
|
auto logger = spdlog::create<failing_sink>("test-failing-sink");
|
2025-01-05 08:17:31 +08:00
|
|
|
logger->set_error_handler([=](const std::string &msg) {
|
|
|
|
REQUIRE(msg == flush_err_msg);
|
|
|
|
throw custom_ex();
|
|
|
|
});
|
2025-01-06 16:35:08 +08:00
|
|
|
REQUIRE_NOTHROW(logger->flush());
|
2018-05-23 02:59:14 +08:00
|
|
|
}
|