test_errors.cpp 3.95 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

8
class failing_sink : public spdlog::sinks::base_sink<std::mutex>
gabime's avatar
gabime committed
9
{
10 11
public:
    failing_sink() = default;
12
    ~failing_sink() final = default;
13 14

protected:
15
    void sink_it_(const spdlog::details::log_msg &) final
gabime's avatar
gabime committed
16 17 18 19
    {
        throw std::runtime_error("some error happened during log");
    }

20
    void flush_() final
gabime's avatar
gabime committed
21 22 23
    {
        throw std::runtime_error("some error happened during flush");
    }
24 25
};

gabime's avatar
gabime committed
26 27
TEST_CASE("default_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
28
    prepare_logdir();
gabime's avatar
gabime committed
29
    std::string filename = "test_logs/simple_log.txt";
gabime's avatar
gabime committed
30

31
    auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
gabime's avatar
gabime committed
32 33 34 35
    logger->set_pattern("%v");
    logger->info("Test message {} {}", 1);
    logger->info("Test message {}", 2);
    logger->flush();
gabime's avatar
gabime committed
36

gabime's avatar
gabime committed
37 38
    REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
    REQUIRE(count_lines(filename) == 1);
gabime's avatar
gabime committed
39 40
}

41
struct custom_ex
42
{};
gabime's avatar
gabime committed
43 44
TEST_CASE("custom_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
45
    prepare_logdir();
gabime's avatar
gabime committed
46
    std::string filename = "test_logs/simple_log.txt";
47
    auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
gabime's avatar
gabime committed
48
    logger->flush_on(spdlog::level::info);
49
    logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
gabime's avatar
gabime committed
50
    logger->info("Good message #1");
gabime's avatar
gabime committed
51

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

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

65 66
TEST_CASE("flush_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
67 68
    spdlog::drop_all();
    auto logger = spdlog::create<failing_sink>("failed_logger");
69
    logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
gabime's avatar
gabime committed
70
    REQUIRE_THROWS_AS(logger->flush(), custom_ex);
71 72
}

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

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

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

119
    spdlog::init_thread_pool(128, 1);
gabime's avatar
gabime committed
120
    REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg);
gabime's avatar
gabime committed
121
}