mirror of
https://github.com/gabime/spdlog.git
synced 2025-02-28 11:15:50 +08:00
Added custom error handler support to async sink
This commit is contained in:
parent
b46b6dcb00
commit
bc2eed7913
@ -18,6 +18,5 @@ public:
|
|||||||
void handle_unknown_ex(const std::string& origin, const source_loc& loc) const noexcept;
|
void handle_unknown_ex(const std::string& origin, const source_loc& loc) const noexcept;
|
||||||
void set_err_handler(err_handler handler);
|
void set_err_handler(err_handler handler);
|
||||||
};
|
};
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
||||||
}} // namespace spdlog::details
|
|
||||||
|
@ -42,7 +42,7 @@ public:
|
|||||||
err_handler custom_err_handler = nullptr;
|
err_handler custom_err_handler = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit async_sink(config async_config);
|
explicit async_sink(const config &async_config);
|
||||||
|
|
||||||
// create an async_sink with one backend sink
|
// create an async_sink with one backend sink
|
||||||
template <typename Sink, typename... SinkArgs>
|
template <typename Sink, typename... SinkArgs>
|
||||||
|
@ -14,11 +14,15 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
async_sink::async_sink(config async_config)
|
async_sink::async_sink(const config &async_config)
|
||||||
: config_(std::move(async_config)) {
|
: config_(async_config) {
|
||||||
if (config_.queue_size == 0 || config_.queue_size > max_queue_size) {
|
if (config_.queue_size == 0 || config_.queue_size > max_queue_size) {
|
||||||
throw spdlog_ex("async_sink: invalid queue size");
|
throw spdlog_ex("async_sink: invalid queue size");
|
||||||
}
|
}
|
||||||
|
if (config_.custom_err_handler) {
|
||||||
|
err_helper_.set_err_handler(config_.custom_err_handler);
|
||||||
|
}
|
||||||
|
|
||||||
q_ = std::make_unique<queue_t>(config_.queue_size);
|
q_ = std::make_unique<queue_t>(config_.queue_size);
|
||||||
worker_thread_ = std::thread([this] {
|
worker_thread_ = std::thread([this] {
|
||||||
if (config_.on_thread_start) config_.on_thread_start();
|
if (config_.on_thread_start) config_.on_thread_start();
|
||||||
|
@ -295,3 +295,17 @@ TEST_CASE("backend_ex", "[async]") {
|
|||||||
REQUIRE_NOTHROW(logger->info("Hello message"));
|
REQUIRE_NOTHROW(logger->info("Hello message"));
|
||||||
REQUIRE_NOTHROW(logger->flush());
|
REQUIRE_NOTHROW(logger->flush());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test async custom error handler. trigger it using a backend exception and make sure it's called
|
||||||
|
TEST_CASE("custom_err_handler", "[async]") {
|
||||||
|
bool error_called = false;
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
test_sink->set_exception(std::runtime_error("test backend exception"));
|
||||||
|
async_sink::config config;
|
||||||
|
config.sinks.push_back(std::move(test_sink));
|
||||||
|
config.custom_err_handler = [&error_called](const std::string &) { error_called = true;};
|
||||||
|
auto asink = std::make_shared<async_sink>(config);
|
||||||
|
spdlog::logger ("async_logger", std::move(asink)).info("Test");
|
||||||
|
// lvalue logger so will be destructed here already so all messages were processed
|
||||||
|
REQUIRE(error_called);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user