From 11472eddbc494ea766d62418b809097903fa599b Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 17 Jul 2019 16:01:30 +0300 Subject: [PATCH] Catch sink exceptions without affecting other sinks --- include/spdlog/async_logger-inl.h | 18 ++---------- include/spdlog/logger-inl.h | 40 +++++++++----------------- include/spdlog/logger.h | 47 +++++++------------------------ 3 files changed, 25 insertions(+), 80 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 6a945a2f..0dc211af 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -65,14 +65,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in } } } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + SPDLOG_LOGGER_CATCH() if (should_flush_(incoming_log_msg)) { @@ -89,14 +82,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() sink->flush(); } } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + SPDLOG_LOGGER_CATCH() } SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::string new_name) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index ca44855d..0f7b485d 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -65,19 +65,8 @@ SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, string_vie return; } - try - { - details::log_msg log_msg(loc, string_view_t(name_), lvl, msg); - sink_it_(log_msg); - } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + details::log_msg log_msg(loc, string_view_t(name_), lvl, msg); + sink_it_(log_msg); } SPDLOG_INLINE void logger::log(level::level_enum lvl, string_view_t msg) @@ -137,18 +126,7 @@ SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type ti // flush functions SPDLOG_INLINE void logger::flush() { - try - { - flush_(); - } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + flush_(); } SPDLOG_INLINE void logger::flush_on(level::level_enum log_level) @@ -193,7 +171,11 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg) { if (sink->should_log(msg.level)) { - sink->log(msg); + try + { + sink->log(msg); + } + SPDLOG_LOGGER_CATCH() } } @@ -207,7 +189,11 @@ SPDLOG_INLINE void logger::flush_() { for (auto &sink : sinks_) { - sink->flush(); + try + { + sink->flush(); + } + SPDLOG_LOGGER_CATCH() } } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 5c07931a..e9de47cf 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -27,6 +27,10 @@ #include #include +#define SPDLOG_LOGGER_CATCH() \ +catch (const std::exception &ex) { err_handler_(ex.what());} \ +catch (...) {err_handler_("Unknown exception in logger");} + namespace spdlog { class logger { @@ -72,14 +76,7 @@ public: details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); sink_it_(log_msg); } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + SPDLOG_LOGGER_CATCH() } template @@ -150,19 +147,9 @@ public: { return; } - try - { - details::log_msg log_msg(loc, name_, lvl, msg); - sink_it_(log_msg); - } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + + details::log_msg log_msg(loc, name_, lvl, msg); + sink_it_(log_msg); } // T cannot be statically converted to string_view @@ -180,14 +167,7 @@ public: details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); sink_it_(log_msg); } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + SPDLOG_LOGGER_CATCH() } template @@ -250,14 +230,7 @@ public: details::log_msg log_msg(source, name_, lvl, string_view_t(buf.data(), buf.size())); sink_it_(log_msg); } - catch (const std::exception &ex) - { - err_handler_(ex.what()); - } - catch (...) - { - err_handler_("Unknown exception in logger"); - } + SPDLOG_LOGGER_CATCH() } template