errors.cpp 3.75 KB
Newer Older
gabime's avatar
gabime committed
1
/*
gabime's avatar
gabime committed
2 3
 * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
 */
gabime's avatar
gabime committed
4 5
#include "includes.h"

gabime's avatar
gabime committed
6
#include <iostream>
gabime's avatar
gabime committed
7

gabime's avatar
gabime committed
8
class failing_sink : public spdlog::sinks::sink
gabime's avatar
gabime committed
9
{
10
    void log(const spdlog::details::log_msg &) override
gabime's avatar
gabime committed
11 12 13 14
    {
        throw std::runtime_error("some error happened during log");
    }

15
    void flush() override
gabime's avatar
gabime committed
16 17 18
    {
        throw std::runtime_error("some error happened during flush");
    }
19 20
};

gabime's avatar
gabime committed
21 22
TEST_CASE("default_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
23

gabime's avatar
gabime committed
24 25
    prepare_logdir();
    std::string filename = "logs/simple_log.txt";
gabime's avatar
gabime committed
26

gabime's avatar
gabime committed
27
    auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("test-error", filename, true);
gabime's avatar
gabime committed
28 29 30 31
    logger->set_pattern("%v");
    logger->info("Test message {} {}", 1);
    logger->info("Test message {}", 2);
    logger->flush();
gabime's avatar
gabime committed
32

gabime's avatar
gabime committed
33 34
    REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
    REQUIRE(count_lines(filename) == 1);
gabime's avatar
gabime committed
35 36
}

37
struct custom_ex
gabime's avatar
gabime committed
38 39
{
};
gabime's avatar
gabime committed
40 41
TEST_CASE("custom_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
42 43 44 45
    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);
46
    logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
gabime's avatar
gabime committed
47
    logger->info("Good message #1");
gabime's avatar
gabime committed
48

gabime's avatar
gabime committed
49 50 51
    REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
    logger->info("Good message #2");
    REQUIRE(count_lines(filename) == 2);
52 53 54 55
}

TEST_CASE("default_error_handler2", "[errors]]")
{
gabime's avatar
gabime committed
56
    spdlog::drop_all();
gabime's avatar
gabime committed
57
    auto logger = spdlog::create<failing_sink>("failed_logger");
58
    logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
gabime's avatar
gabime committed
59
    REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
gabime's avatar
gabime committed
60 61
}

62 63
TEST_CASE("flush_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
64 65 66 67
    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);
68 69
}

gabime's avatar
gabime committed
70 71
TEST_CASE("async_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
72 73
    prepare_logdir();
    std::string err_msg("log failed with some msg");
74

gabime's avatar
gabime committed
75 76
    std::string filename = "logs/simple_async_log.txt";
    {
77
        spdlog::init_thread_pool(128, 1);
gabime's avatar
gabime committed
78
        auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
79
        logger->set_error_handler([=](const std::string &) {
gabime's avatar
gabime committed
80
            std::ofstream ofs("logs/custom_err.txt");
gabime's avatar
gabime committed
81 82
            if (!ofs)
                throw std::runtime_error("Failed open logs/custom_err.txt");
gabime's avatar
gabime committed
83 84 85 86 87
            ofs << err_msg;
        });
        logger->info("Good message #1");
        logger->info("Bad format msg {} {}", "xxx");
        logger->info("Good message #2");
gabime's avatar
gabime committed
88
        spdlog::drop("logger"); // force logger to drain the queue and shutdown
gabime's avatar
gabime committed
89
    }
90
    spdlog::init_thread_pool(128, 1);
gabime's avatar
gabime committed
91 92
    REQUIRE(count_lines(filename) == 2);
    REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
93 94 95 96 97
}

// Make sure async error handler is executed
TEST_CASE("async_error_handler2", "[errors]]")
{
gabime's avatar
gabime committed
98 99 100
    prepare_logdir();
    std::string err_msg("This is async handler error message");
    {
101
        spdlog::init_thread_pool(128, 1);
gabime's avatar
gabime committed
102
        auto logger = spdlog::create_async_logger<failing_sink>("failed_logger");
103
        logger->set_error_handler([=](const std::string &) {
gabime's avatar
gabime committed
104
            std::ofstream ofs("logs/custom_err2.txt");
gabime's avatar
gabime committed
105 106
            if (!ofs)
                throw std::runtime_error("Failed open logs/custom_err2.txt");
gabime's avatar
gabime committed
107 108 109
            ofs << err_msg;
        });
        logger->info("Hello failure");
gabime's avatar
gabime committed
110
        spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
gabime's avatar
gabime committed
111 112
    }

113
    spdlog::init_thread_pool(128, 1);
gabime's avatar
gabime committed
114
    REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
gabime's avatar
gabime committed
115
}