spdlog
Loading...
Searching...
No Matches
test_errors.cpp
Go to the documentation of this file.
1/*
2 * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
3 */
4#include "includes.h"
5
6#include <iostream>
7
8#define SIMPLE_LOG "test_logs/simple_log.txt"
9#define SIMPLE_ASYNC_LOG "test_logs/simple_async_log.txt"
10
11class failing_sink : public spdlog::sinks::base_sink<std::mutex>
12{
13protected:
15 {
16 throw std::runtime_error("some error happened during log");
17 }
18
19 void flush_() final
20 {
21 throw std::runtime_error("some error happened during flush");
22 }
23};
24
25TEST_CASE("default_error_handler", "[errors]]")
26{
29
30 auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
31 logger->set_pattern("%v");
32 logger->info(fmt::runtime("Test message {} {}"), 1);
33 logger->info("Test message {}", 2);
34 logger->flush();
35
37 REQUIRE(file_contents(SIMPLE_LOG) == fmt::format("Test message 2{}", default_eol));
39}
40
42{};
43TEST_CASE("custom_error_handler", "[errors]]")
44{
47 auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
48 logger->flush_on(spdlog::level::info);
49 logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
50 logger->info("Good message #1");
51
52 REQUIRE_THROWS_AS(logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"), custom_ex);
53 logger->info("Good message #2");
55}
56
57TEST_CASE("default_error_handler2", "[errors]]")
58{
60 auto logger = spdlog::create<failing_sink>("failed_logger");
61 logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
62 REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
63}
64
65TEST_CASE("flush_error_handler", "[errors]]")
66{
68 auto logger = spdlog::create<failing_sink>("failed_logger");
69 logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
70 REQUIRE_THROWS_AS(logger->flush(), custom_ex);
71}
72
73TEST_CASE("async_error_handler", "[errors]]")
74{
76 std::string err_msg("log failed with some msg");
77
79 {
81 auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
82 logger->set_error_handler([=](const std::string &) {
83 std::ofstream ofs("test_logs/custom_err.txt");
84 if (!ofs)
85 {
86 throw std::runtime_error("Failed open test_logs/custom_err.txt");
87 }
88 ofs << err_msg;
89 });
90 logger->info("Good message #1");
91 logger->info(fmt::runtime("Bad format msg {} {}"), "xxx");
92 logger->info("Good message #2");
93 spdlog::drop("logger"); // force logger to drain the queue and shutdown
94 }
97 REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
98}
99
100// Make sure async error handler is executed
101TEST_CASE("async_error_handler2", "[errors]]")
102{
104 std::string err_msg("This is async handler error message");
105 {
108 auto logger = spdlog::create_async<failing_sink>("failed_logger");
109 logger->set_error_handler([=](const std::string &) {
110 std::ofstream ofs("test_logs/custom_err2.txt");
111 if (!ofs)
112 throw std::runtime_error("Failed open test_logs/custom_err2.txt");
113 ofs << err_msg;
114 });
115 logger->info("Hello failure");
116 spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
117 }
118
120 REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg);
121}
int count_lines(const char *filename)
#define TEST_CASE(...)
Definition catch.hpp:15119
#define REQUIRE(...)
Definition catch.hpp:15083
#define REQUIRE_THROWS_AS(expr, exceptionType)
Definition catch.hpp:15087
void flush_() final
void sink_it_(const spdlog::details::log_msg &) final
#define SPDLOG_FILENAME_T(s)
Definition common.h:107
static SPDLOG_CONSTEXPR const char * default_eol
Definition details/os.h:32
SPDLOG_INLINE bool create_dir(filename_t path)
Definition os-inl.h:534
void init_thread_pool(size_t q_size, size_t thread_count, std::function< void()> on_thread_start)
Definition async.h:76
SPDLOG_INLINE void drop(const std::string &name)
Definition spdlog-inl.h:90
SPDLOG_INLINE void drop_all()
Definition spdlog-inl.h:95
#define SIMPLE_LOG
#define SIMPLE_ASYNC_LOG
std::string file_contents(const std::string &filename)
Definition utils.cpp:24
void require_message_count(const std::string &filename, const std::size_t messages)
Definition utils.cpp:49
void prepare_logdir()
Definition utils.cpp:10