From 1fba68bfe238629733b49d4c9d576556f7788585 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 27 May 2023 15:33:02 +0300 Subject: [PATCH 1/5] Catch exceptions from async logger. Fix #2618 --- include/spdlog/async_logger-inl.h | 32 +++++++++++++++++++------------ tests/test_async.cpp | 10 +++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index a1c27a59..13da5940 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -26,27 +26,35 @@ SPDLOG_INLINE spdlog::async_logger::async_logger( // send the log message to the thread pool SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg) { - if (auto pool_ptr = thread_pool_.lock()) + SPDLOG_TRY { - pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); - } - else - { - throw_spdlog_ex("async log: thread pool doesn't exist anymore"); + if (auto pool_ptr = thread_pool_.lock()) + { + pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); + } + else + { + throw_spdlog_ex("async log: thread pool doesn't exist anymore"); + } } + SPDLOG_LOGGER_CATCH(msg.source) } // send flush request to the thread pool SPDLOG_INLINE void spdlog::async_logger::flush_() { - if (auto pool_ptr = thread_pool_.lock()) + SPDLOG_TRY { - pool_ptr->post_flush(shared_from_this(), overflow_policy_); - } - else - { - throw_spdlog_ex("async flush: thread pool doesn't exist anymore"); + if (auto pool_ptr = thread_pool_.lock()) + { + pool_ptr->post_flush(shared_from_this(), overflow_policy_); + } + else + { + throw_spdlog_ex("async flush: thread pool doesn't exist anymore"); + } } + SPDLOG_LOGGER_CATCH(source_loc()) } // diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 5265bca4..06c5c921 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -185,6 +185,14 @@ TEST_CASE("to_file multi-workers", "[async]") logger->info("Hello message #{}", j); } } - require_message_count(TEST_FILENAME, messages); } + +TEST_CASE("bad_tp", "[async]") +{ + auto test_sink = std::make_shared(); + std::shared_ptr const empty_tp; + auto logger = std::make_shared("as", test_sink, empty_tp); + logger->info("Please throw an exception"); + REQUIRE(test_sink->msg_counter() == 0); +} From ea1af20840dd052b368a5f24630426709409a7a5 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 27 May 2023 15:34:33 +0300 Subject: [PATCH 2/5] Update error message in default error handler --- include/spdlog/logger-inl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index ff82db4c..227cec43 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -248,9 +248,9 @@ SPDLOG_INLINE void logger::err_handler_(const std::string &msg) char date_buf[64]; std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); #if defined(USING_R) && defined(R_R_H) // if in R environment - REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str()); + REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str()); #else - std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str()); + std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str()); #endif } } From 62a4b8ce4ee985e7c7f3c2bbce2195634c5d7ae3 Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Sat, 27 May 2023 22:28:22 +0300 Subject: [PATCH 3/5] Fix fmt build (#2744) --- include/spdlog/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 1fafdabd..825f5d06 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -371,7 +371,7 @@ protected: #ifdef SPDLOG_USE_STD_FORMAT fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(std::forward(args)...)); #else - fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(std::forward(args)...)); + fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(args...)); #endif details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); From 8222ca4837ddee50b1f0f2154bb273f68f842aee Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 27 May 2023 22:46:27 +0300 Subject: [PATCH 4/5] Update test_stopwatch.cpp --- tests/test_stopwatch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_stopwatch.cpp b/tests/test_stopwatch.cpp index 61e37698..33436fd5 100644 --- a/tests/test_stopwatch.cpp +++ b/tests/test_stopwatch.cpp @@ -39,6 +39,6 @@ TEST_CASE("stopwatch2", "[stopwatch]") auto val = std::stod(test_sink->lines()[0]); auto diff_duration = duration_cast>(stop - start); - REQUIRE(val >= diff_duration.count()); + REQUIRE(val >= (diff_duration - milliseconds(3)).count()); REQUIRE(val <= (diff_duration + tolerance_duration).count()); } From c174c151383214769e86780e9fabb36fc8597a13 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 27 May 2023 23:05:49 +0300 Subject: [PATCH 5/5] Update test_stopwatch.cpp --- tests/test_stopwatch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_stopwatch.cpp b/tests/test_stopwatch.cpp index 33436fd5..81827b87 100644 --- a/tests/test_stopwatch.cpp +++ b/tests/test_stopwatch.cpp @@ -39,6 +39,6 @@ TEST_CASE("stopwatch2", "[stopwatch]") auto val = std::stod(test_sink->lines()[0]); auto diff_duration = duration_cast>(stop - start); - REQUIRE(val >= (diff_duration - milliseconds(3)).count()); + REQUIRE(val >= (diff_duration).count() - 0.001); REQUIRE(val <= (diff_duration + tolerance_duration).count()); }