2016-08-05 08:56:40 +08:00
|
|
|
/*
|
2018-03-09 21:26:33 +08:00
|
|
|
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
|
|
|
*/
|
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
|
|
|
|
2018-03-09 21:26:33 +08:00
|
|
|
class failing_sink : public spdlog::sinks::sink
|
2017-03-28 07:08:18 +08:00
|
|
|
{
|
2018-05-23 02:59:14 +08:00
|
|
|
void log(const spdlog::details::log_msg &) override
|
2017-03-28 07:08:18 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("some error happened during log");
|
|
|
|
}
|
|
|
|
|
2018-05-23 02:59:14 +08:00
|
|
|
void flush() override
|
2018-05-26 23:48:39 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("some error happened during flush");
|
|
|
|
}
|
2017-03-28 06:54:33 +08:00
|
|
|
};
|
|
|
|
|
2016-08-05 08:56:40 +08:00
|
|
|
TEST_CASE("default_error_handler", "[errors]]")
|
|
|
|
{
|
2018-06-01 22:30:38 +08:00
|
|
|
|
2017-03-28 07:08:18 +08:00
|
|
|
prepare_logdir();
|
|
|
|
std::string filename = "logs/simple_log.txt";
|
2016-08-05 08:56:40 +08:00
|
|
|
|
2018-06-01 22:30:38 +08:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("test-error", filename, true);
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->set_pattern("%v");
|
2017-10-05 19:20:21 +08:00
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->info("Test message {} {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
2017-10-05 19:20:21 +08:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d %d", 1);
|
|
|
|
logger->info("Test message %d", 2);
|
|
|
|
#endif
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->flush();
|
2016-08-05 08:56:40 +08:00
|
|
|
|
2017-03-28 07:08:18 +08:00
|
|
|
REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
|
|
|
|
REQUIRE(count_lines(filename) == 1);
|
2016-08-05 08:56:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 06:54:33 +08:00
|
|
|
struct custom_ex
|
2018-03-09 21:26:33 +08:00
|
|
|
{
|
|
|
|
};
|
2016-08-05 08:56:40 +08:00
|
|
|
TEST_CASE("custom_error_handler", "[errors]]")
|
|
|
|
{
|
2017-03-28 07:08:18 +08:00
|
|
|
prepare_logdir();
|
|
|
|
std::string filename = "logs/simple_log.txt";
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
|
|
|
logger->flush_on(spdlog::level::info);
|
2018-05-23 02:59:14 +08:00
|
|
|
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->info("Good message #1");
|
2017-10-05 19:20:21 +08:00
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2017-03-28 07:08:18 +08:00
|
|
|
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
2017-10-05 19:20:21 +08:00
|
|
|
#else
|
|
|
|
REQUIRE_THROWS_AS(logger->info("Bad format msg %s %s", "xxx"), custom_ex);
|
|
|
|
#endif
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->info("Good message #2");
|
|
|
|
REQUIRE(count_lines(filename) == 2);
|
2017-03-28 06:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("default_error_handler2", "[errors]]")
|
|
|
|
{
|
2018-05-26 23:48:39 +08:00
|
|
|
spdlog::drop_all();
|
2017-03-28 07:08:18 +08:00
|
|
|
auto logger = spdlog::create<failing_sink>("failed_logger");
|
2018-05-23 02:59:14 +08:00
|
|
|
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
2017-03-28 07:08:18 +08:00
|
|
|
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
2016-08-05 08:56:40 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 02:59:14 +08:00
|
|
|
TEST_CASE("flush_error_handler", "[errors]]")
|
|
|
|
{
|
2018-05-26 23:48:39 +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);
|
2018-05-23 02:59:14 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 08:56:40 +08:00
|
|
|
TEST_CASE("async_error_handler", "[errors]]")
|
|
|
|
{
|
2017-03-28 07:08:18 +08:00
|
|
|
prepare_logdir();
|
|
|
|
std::string err_msg("log failed with some msg");
|
2018-04-14 08:34:57 +08:00
|
|
|
|
2017-03-28 07:08:18 +08:00
|
|
|
std::string filename = "logs/simple_async_log.txt";
|
|
|
|
{
|
2018-04-14 08:34:57 +08:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2018-04-20 08:04:53 +08:00
|
|
|
auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
2018-05-23 02:59:14 +08:00
|
|
|
logger->set_error_handler([=](const std::string &) {
|
2017-03-28 07:08:18 +08:00
|
|
|
std::ofstream ofs("logs/custom_err.txt");
|
2018-03-09 21:26:33 +08:00
|
|
|
if (!ofs)
|
|
|
|
throw std::runtime_error("Failed open logs/custom_err.txt");
|
2017-03-28 07:08:18 +08:00
|
|
|
ofs << err_msg;
|
|
|
|
});
|
|
|
|
logger->info("Good message #1");
|
2017-10-05 19:20:21 +08:00
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->info("Bad format msg {} {}", "xxx");
|
2017-10-05 19:20:21 +08:00
|
|
|
#else
|
|
|
|
logger->info("Bad format msg %s %s", "xxx");
|
|
|
|
#endif
|
2017-03-28 07:08:18 +08:00
|
|
|
logger->info("Good message #2");
|
2018-03-09 21:26:33 +08:00
|
|
|
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
2017-03-28 07:08:18 +08:00
|
|
|
}
|
2018-04-14 08:34:57 +08:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2017-03-28 07:08:18 +08:00
|
|
|
REQUIRE(count_lines(filename) == 2);
|
|
|
|
REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
|
2017-03-28 06:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure async error handler is executed
|
|
|
|
TEST_CASE("async_error_handler2", "[errors]]")
|
|
|
|
{
|
2017-03-28 07:08:18 +08:00
|
|
|
prepare_logdir();
|
|
|
|
std::string err_msg("This is async handler error message");
|
|
|
|
{
|
2018-04-14 08:34:57 +08:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2018-04-20 08:04:53 +08:00
|
|
|
auto logger = spdlog::create_async_logger<failing_sink>("failed_logger");
|
2018-05-23 02:59:14 +08:00
|
|
|
logger->set_error_handler([=](const std::string &) {
|
2017-03-28 07:08:18 +08:00
|
|
|
std::ofstream ofs("logs/custom_err2.txt");
|
2018-03-09 21:26:33 +08:00
|
|
|
if (!ofs)
|
|
|
|
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
2017-03-28 07:08:18 +08:00
|
|
|
ofs << err_msg;
|
|
|
|
});
|
|
|
|
logger->info("Hello failure");
|
2018-03-09 21:26:33 +08:00
|
|
|
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
2017-03-28 07:08:18 +08:00
|
|
|
}
|
|
|
|
|
2018-04-14 08:34:57 +08:00
|
|
|
spdlog::init_thread_pool(128, 1);
|
2017-03-28 07:08:18 +08:00
|
|
|
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
|
2016-08-05 08:56:40 +08:00
|
|
|
}
|