errors.cpp 4.02 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 16 17 18
	void flush() override
	{
		throw std::runtime_error("some error happened during flush");
	}
19
};
20
using namespace std;
gabime's avatar
gabime committed
21 22
TEST_CASE("default_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
23 24
    prepare_logdir();
    std::string filename = "logs/simple_log.txt";
gabime's avatar
gabime committed
25

gabime's avatar
gabime committed
26 27
    auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
    logger->set_pattern("%v");
fogo's avatar
fogo committed
28
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
29 30
    logger->info("Test message {} {}", 1);
    logger->info("Test message {}", 2);
fogo's avatar
fogo committed
31 32 33 34
#else
    logger->info("Test message %d %d", 1);
    logger->info("Test message %d", 2);
#endif
gabime's avatar
gabime committed
35
    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
gabime's avatar
gabime committed
42 43
{
};
gabime's avatar
gabime committed
44 45
TEST_CASE("custom_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
46 47 48 49
    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);
50
    logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
gabime's avatar
gabime committed
51
    logger->info("Good message #1");
fogo's avatar
fogo committed
52
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
53
    REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
fogo's avatar
fogo committed
54 55 56
#else
    REQUIRE_THROWS_AS(logger->info("Bad format msg %s %s", "xxx"), custom_ex);
#endif
gabime's avatar
gabime committed
57 58
    logger->info("Good message #2");
    REQUIRE(count_lines(filename) == 2);
59 60 61 62
}

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

68 69 70 71 72 73 74 75
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(); });
	REQUIRE_THROWS_AS(logger->flush(), custom_ex);
}

gabime's avatar
gabime committed
76 77
TEST_CASE("async_error_handler", "[errors]]")
{
gabime's avatar
gabime committed
78 79 80 81 82 83
    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);
84
        logger->set_error_handler([=](const std::string &) {
gabime's avatar
gabime committed
85
            std::ofstream ofs("logs/custom_err.txt");
gabime's avatar
gabime committed
86 87
            if (!ofs)
                throw std::runtime_error("Failed open logs/custom_err.txt");
gabime's avatar
gabime committed
88 89 90
            ofs << err_msg;
        });
        logger->info("Good message #1");
fogo's avatar
fogo committed
91
#if !defined(SPDLOG_FMT_PRINTF)
gabime's avatar
gabime committed
92
        logger->info("Bad format msg {} {}", "xxx");
fogo's avatar
fogo committed
93 94 95
#else
        logger->info("Bad format msg %s %s", "xxx");
#endif
gabime's avatar
gabime committed
96
        logger->info("Good message #2");
gabime's avatar
gabime committed
97
        spdlog::drop("logger"); // force logger to drain the queue and shutdown
gabime's avatar
gabime committed
98 99 100 101
        spdlog::set_sync_mode();
    }
    REQUIRE(count_lines(filename) == 2);
    REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
102 103 104 105 106
}

// Make sure async error handler is executed
TEST_CASE("async_error_handler2", "[errors]]")
{
gabime's avatar
gabime committed
107 108 109 110 111
    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");
112
        logger->set_error_handler([=](const std::string &) {
gabime's avatar
gabime committed
113
            std::ofstream ofs("logs/custom_err2.txt");
gabime's avatar
gabime committed
114 115
            if (!ofs)
                throw std::runtime_error("Failed open logs/custom_err2.txt");
gabime's avatar
gabime committed
116 117
            ofs << err_msg;
        });
118
        logger->info("Hello failure");		
gabime's avatar
gabime committed
119
        spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
gabime's avatar
gabime committed
120
        spdlog::set_sync_mode();
121
		logger.reset();
gabime's avatar
gabime committed
122 123 124
    }

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