2016-08-04 20:56:40 -04:00
|
|
|
/*
|
2018-03-09 08:26:33 -05:00
|
|
|
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
|
|
|
*/
|
2016-08-04 20:56:40 -04:00
|
|
|
#include "includes.h"
|
|
|
|
|
2018-03-09 08:26:33 -05:00
|
|
|
#include <iostream>
|
2016-08-04 20:56:40 -04:00
|
|
|
|
2018-07-14 09:21:53 -04:00
|
|
|
class failing_sink : public spdlog::sinks::base_sink<std::mutex>
|
2017-03-27 19:08:18 -04:00
|
|
|
{
|
2018-07-14 09:21:53 -04:00
|
|
|
public:
|
|
|
|
failing_sink() = default;
|
2018-08-14 15:33:47 -04:00
|
|
|
~failing_sink() final = default;
|
2018-07-14 09:21:53 -04:00
|
|
|
|
|
|
|
protected:
|
2018-08-14 15:33:47 -04:00
|
|
|
void sink_it_(const spdlog::details::log_msg &) final
|
2017-03-27 19:08:18 -04:00
|
|
|
{
|
|
|
|
throw std::runtime_error("some error happened during log");
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:33:47 -04:00
|
|
|
void flush_() final
|
2018-05-26 11:48:39 -04:00
|
|
|
{
|
|
|
|
throw std::runtime_error("some error happened during flush");
|
|
|
|
}
|
2017-03-27 18:54:33 -04:00
|
|
|
};
|
|
|
|
|
2016-08-04 20:56:40 -04:00
|
|
|
TEST_CASE("default_error_handler", "[errors]]")
|
|
|
|
{
|
2017-03-27 19:08:18 -04:00
|
|
|
prepare_logdir();
|
|
|
|
std::string filename = "logs/simple_log.txt";
|
2016-08-04 20:56:40 -04:00
|
|
|
|
2018-07-07 05:12:45 -04:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
|
2017-03-27 19:08:18 -04:00
|
|
|
logger->set_pattern("%v");
|
|
|
|
logger->info("Test message {} {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
|
|
|
logger->flush();
|
2016-08-04 20:56:40 -04:00
|
|
|
|
2017-03-27 19:08:18 -04:00
|
|
|
REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
|
|
|
|
REQUIRE(count_lines(filename) == 1);
|
2016-08-04 20:56:40 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 18:54:33 -04:00
|
|
|
struct custom_ex
|
2018-03-09 08:26:33 -05:00
|
|
|
{
|
|
|
|
};
|
2019-04-05 16:05:46 -04:00
|
|
|
|
|
|
|
static void custom_handler(const std::string &msg)
|
|
|
|
{
|
|
|
|
throw custom_ex();
|
|
|
|
}
|
2016-08-04 20:56:40 -04:00
|
|
|
TEST_CASE("custom_error_handler", "[errors]]")
|
|
|
|
{
|
2017-03-27 19:08:18 -04:00
|
|
|
prepare_logdir();
|
|
|
|
std::string filename = "logs/simple_log.txt";
|
2018-07-07 05:12:45 -04:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
2017-03-27 19:08:18 -04:00
|
|
|
logger->flush_on(spdlog::level::info);
|
2019-04-05 16:05:46 -04:00
|
|
|
logger->set_error_handler(custom_handler);
|
2017-03-27 19:08:18 -04:00
|
|
|
logger->info("Good message #1");
|
2018-06-12 11:48:22 -04:00
|
|
|
|
2017-03-27 19:08:18 -04:00
|
|
|
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
|
|
|
logger->info("Good message #2");
|
|
|
|
REQUIRE(count_lines(filename) == 2);
|
2017-03-27 18:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("default_error_handler2", "[errors]]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
spdlog::drop_all();
|
2017-03-27 19:08:18 -04:00
|
|
|
auto logger = spdlog::create<failing_sink>("failed_logger");
|
2019-04-05 16:05:46 -04:00
|
|
|
logger->set_error_handler(custom_handler);
|
2017-03-27 19:08:18 -04:00
|
|
|
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
2016-08-04 20:56:40 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 14:59:14 -04:00
|
|
|
TEST_CASE("flush_error_handler", "[errors]]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
spdlog::drop_all();
|
|
|
|
auto logger = spdlog::create<failing_sink>("failed_logger");
|
2019-04-05 16:05:46 -04:00
|
|
|
logger->set_error_handler(custom_handler);
|
2018-05-26 11:48:39 -04:00
|
|
|
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
2018-05-22 14:59:14 -04:00
|
|
|
}
|
|
|
|
|
2016-08-04 20:56:40 -04:00
|
|
|
TEST_CASE("async_error_handler", "[errors]]")
|
|
|
|
{
|
2017-03-27 19:08:18 -04:00
|
|
|
prepare_logdir();
|
2019-04-05 16:05:46 -04:00
|
|
|
|
2018-04-13 20:34:57 -04:00
|
|
|
|
2017-03-27 19:08:18 -04:00
|
|
|
std::string filename = "logs/simple_async_log.txt";
|
|
|
|
{
|
2018-04-13 20:34:57 -04:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2018-07-07 05:12:45 -04:00
|
|
|
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
2019-04-05 16:05:46 -04:00
|
|
|
logger->set_error_handler([](const std::string &) {
|
2017-03-27 19:08:18 -04:00
|
|
|
std::ofstream ofs("logs/custom_err.txt");
|
2018-03-09 08:26:33 -05:00
|
|
|
if (!ofs)
|
2018-10-05 08:20:14 -04:00
|
|
|
{
|
2018-03-09 08:26:33 -05:00
|
|
|
throw std::runtime_error("Failed open logs/custom_err.txt");
|
2018-10-05 08:20:14 -04:00
|
|
|
}
|
2019-04-05 16:05:46 -04:00
|
|
|
ofs << "log failed with some msg";
|
2017-03-27 19:08:18 -04:00
|
|
|
});
|
|
|
|
logger->info("Good message #1");
|
|
|
|
logger->info("Bad format msg {} {}", "xxx");
|
|
|
|
logger->info("Good message #2");
|
2018-03-09 08:26:33 -05:00
|
|
|
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
2017-03-27 19:08:18 -04:00
|
|
|
}
|
2018-04-13 20:34:57 -04:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2017-03-27 19:08:18 -04:00
|
|
|
REQUIRE(count_lines(filename) == 2);
|
2019-04-05 16:05:46 -04:00
|
|
|
REQUIRE(file_contents("logs/custom_err.txt") == "log failed with some msg");
|
2017-03-27 18:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure async error handler is executed
|
|
|
|
TEST_CASE("async_error_handler2", "[errors]]")
|
|
|
|
{
|
2019-04-05 16:05:46 -04:00
|
|
|
prepare_logdir();
|
2017-03-27 19:08:18 -04:00
|
|
|
{
|
2018-04-13 20:34:57 -04:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2018-07-07 05:12:45 -04:00
|
|
|
auto logger = spdlog::create_async<failing_sink>("failed_logger");
|
2019-04-05 16:05:46 -04:00
|
|
|
logger->set_error_handler([](const std::string &) {
|
2017-03-27 19:08:18 -04:00
|
|
|
std::ofstream ofs("logs/custom_err2.txt");
|
2018-03-09 08:26:33 -05:00
|
|
|
if (!ofs)
|
|
|
|
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
2019-04-05 16:05:46 -04:00
|
|
|
ofs << "handler error message";
|
2017-03-27 19:08:18 -04:00
|
|
|
});
|
|
|
|
logger->info("Hello failure");
|
2018-03-09 08:26:33 -05:00
|
|
|
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
2017-03-27 19:08:18 -04:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:34:57 -04:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2019-04-05 16:05:46 -04:00
|
|
|
REQUIRE(file_contents("logs/custom_err2.txt") == "handler error message");
|
2016-08-04 20:56:40 -04:00
|
|
|
}
|