From d6aa266d432f24c1f1bf7ece64aeba342cabeaf2 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Fri, 23 Dec 2022 10:03:26 +0100 Subject: [PATCH] test: don't throw from the destructor of DebugLogHelper Throwing an exception from the destructor of a class is a bad practice, avoid that and instead print the message to the standard error output and call `std::abort()`. --- src/test/util/logging.cpp | 8 +++++--- src/test/util/logging.h | 18 +++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/test/util/logging.cpp b/src/test/util/logging.cpp index 753e50d0541..78ad9c836a1 100644 --- a/src/test/util/logging.cpp +++ b/src/test/util/logging.cpp @@ -8,7 +8,8 @@ #include #include -#include +#include +#include DebugLogHelper::DebugLogHelper(std::string message, MatchFn match) : m_message{std::move(message)}, m_match(std::move(match)) @@ -21,11 +22,12 @@ DebugLogHelper::DebugLogHelper(std::string message, MatchFn match) noui_test_redirect(); } -void DebugLogHelper::check_found() +DebugLogHelper::~DebugLogHelper() { noui_reconnect(); LogInstance().DeleteCallback(m_print_connection); if (!m_found && m_match(nullptr)) { - throw std::runtime_error(strprintf("'%s' not found in debug log\n", m_message)); + tfm::format(std::cerr, "Fatal error: expected message not found in the debug log: '%s'\n", m_message); + std::abort(); } } diff --git a/src/test/util/logging.h b/src/test/util/logging.h index 62b11c28410..104a21f9228 100644 --- a/src/test/util/logging.h +++ b/src/test/util/logging.h @@ -13,10 +13,7 @@ class DebugLogHelper { - const std::string m_message; - bool m_found{false}; - std::list>::iterator m_print_connection; - +public: //! Custom match checking function. //! //! Invoked with pointers to lines containing matching strings, and with @@ -27,13 +24,16 @@ class DebugLogHelper //! (2) raising an error in check_found if no match was found //! Can return false to do the opposite in either case. using MatchFn = std::function; - MatchFn m_match; - void check_found(); - -public: explicit DebugLogHelper(std::string message, MatchFn match = [](const std::string*){ return true; }); - ~DebugLogHelper() noexcept(false) { check_found(); } + + ~DebugLogHelper(); + +private: + const std::string m_message; + bool m_found{false}; + std::list>::iterator m_print_connection; + MatchFn m_match; }; #define ASSERT_DEBUG_LOG(message) DebugLogHelper UNIQUE_NAME(debugloghelper)(message)