static lib wip

This commit is contained in:
gabime
2019-04-05 23:05:46 +03:00
parent 156b856a80
commit 17f9cdd401
14 changed files with 1455 additions and 1464 deletions

View File

@@ -41,13 +41,18 @@ TEST_CASE("default_error_handler", "[errors]]")
struct custom_ex
{
};
static void custom_handler(const std::string &msg)
{
throw custom_ex();
}
TEST_CASE("custom_error_handler", "[errors]]")
{
prepare_logdir();
std::string filename = "logs/simple_log.txt";
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->set_error_handler(custom_handler);
logger->info("Good message #1");
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
@@ -59,7 +64,7 @@ TEST_CASE("default_error_handler2", "[errors]]")
{
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->set_error_handler(custom_handler);
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
}
@@ -67,26 +72,26 @@ TEST_CASE("flush_error_handler", "[errors]]")
{
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->set_error_handler(custom_handler);
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
}
TEST_CASE("async_error_handler", "[errors]]")
{
prepare_logdir();
std::string err_msg("log failed with some msg");
std::string filename = "logs/simple_async_log.txt";
{
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string &) {
logger->set_error_handler([](const std::string &) {
std::ofstream ofs("logs/custom_err.txt");
if (!ofs)
{
throw std::runtime_error("Failed open logs/custom_err.txt");
}
ofs << err_msg;
ofs << "log failed with some msg";
});
logger->info("Good message #1");
logger->info("Bad format msg {} {}", "xxx");
@@ -95,27 +100,26 @@ TEST_CASE("async_error_handler", "[errors]]")
}
spdlog::init_thread_pool(128, 1);
REQUIRE(count_lines(filename) == 2);
REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
REQUIRE(file_contents("logs/custom_err.txt") == "log failed with some msg");
}
// Make sure async error handler is executed
TEST_CASE("async_error_handler2", "[errors]]")
{
prepare_logdir();
std::string err_msg("This is async handler error message");
prepare_logdir();
{
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) {
logger->set_error_handler([](const std::string &) {
std::ofstream ofs("logs/custom_err2.txt");
if (!ofs)
throw std::runtime_error("Failed open logs/custom_err2.txt");
ofs << err_msg;
ofs << "handler error message";
});
logger->info("Hello failure");
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
}
spdlog::init_thread_pool(128, 1);
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
REQUIRE(file_contents("logs/custom_err2.txt") == "handler error message");
}