errors.cpp 3.68 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
{
gabime's avatar
gabime committed
10
    void log(const spdlog::details::log_msg &msg) override
gabime's avatar
gabime committed
11 12 13 14
    {
        throw std::runtime_error("some error happened during log");
    }

gabime's avatar
gabime committed
15
    void flush() override {}
16 17
};

gabime's avatar
gabime committed
18 19
TEST_CASE("default_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
20 21
    prepare_logdir();
    std::string filename = "logs/simple_log.txt";
gabime's avatar
gabime committed
22

gabime's avatar
gabime committed
23 24
    auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
    logger->set_pattern("%v");
fogo's avatar
fogo committed
25
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
26 27
    logger->info("Test message {} {}", 1);
    logger->info("Test message {}", 2);
fogo's avatar
fogo committed
28 29 30 31
#else
    logger->info("Test message %d %d", 1);
    logger->info("Test message %d", 2);
#endif
gabime's avatar
gabime committed
32
    logger->flush();
gabime's avatar
gabime committed
33

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

38
struct custom_ex
gabime's avatar
gabime committed
39 40
{
};
gabime's avatar
gabime committed
41 42
TEST_CASE("custom_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
43 44 45 46
    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);
gabime's avatar
gabime committed
47
    logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
gabime's avatar
gabime committed
48
    logger->info("Good message #1");
fogo's avatar
fogo committed
49
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
50
    REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
fogo's avatar
fogo committed
51 52 53
#else
    REQUIRE_THROWS_AS(logger->info("Bad format msg %s %s", "xxx"), custom_ex);
#endif
gabime's avatar
gabime committed
54 55
    logger->info("Good message #2");
    REQUIRE(count_lines(filename) == 2);
56 57 58 59 60
}

TEST_CASE("default_error_handler2", "[errors]]")
{

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

TEST_CASE("async_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
68 69 70 71 72 73
    prepare_logdir();
    std::string err_msg("log failed with some msg");
    spdlog::set_async_mode(128);
    std::string filename = "logs/simple_async_log.txt";
    {
        auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
gabime's avatar
gabime committed
74
        logger->set_error_handler([=](const std::string &msg) {
gabime's avatar
gabime committed
75
            std::ofstream ofs("logs/custom_err.txt");
gabime's avatar
gabime committed
76 77
            if (!ofs)
                throw std::runtime_error("Failed open logs/custom_err.txt");
gabime's avatar
gabime committed
78 79 80
            ofs << err_msg;
        });
        logger->info("Good message #1");
fogo's avatar
fogo committed
81
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
82
        logger->info("Bad format msg {} {}", "xxx");
fogo's avatar
fogo committed
83 84 85
#else
        logger->info("Bad format msg %s %s", "xxx");
#endif
gabime's avatar
gabime committed
86
        logger->info("Good message #2");
gabime's avatar
gabime committed
87
        spdlog::drop("logger"); // force logger to drain the queue and shutdown
gabime's avatar
gabime committed
88 89 90 91
        spdlog::set_sync_mode();
    }
    REQUIRE(count_lines(filename) == 2);
    REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
92 93 94 95 96
}

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

    REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
gabime's avatar
gabime committed
114
}