Merge bitcoin/bitcoin#33388: test: don't throw from the destructor of DebugLogHelper

2427939935 test: forbid copying of DebugLogHelper (Daniel Pfeifer)
d6aa266d43 test: don't throw from the destructor of DebugLogHelper (Vasil Dimov)

Pull request description:

  Throwing an exception from the destructor of a class is a bad practice because the destructor will be called when an object of that type is alive on the stack and another exception is thrown, which will result in "exception during the exception". This would terminate the program without any messages.

  Instead print the message to the standard error output and call `std::abort()`.

  ---

  This change is part of https://github.com/bitcoin/bitcoin/pull/26812. It is an improvement on its own, so creating a separate PR for it following the discussion at https://github.com/bitcoin/bitcoin/pull/32604#discussion_r2345091587. Getting it in will reduce the size of #26812.

ACKs for top commit:
  Crypt-iQ:
    crACK 2427939
  l0rinc:
    Code review reACK 2427939935
  optout21:
    crACK 2427939935
  furszy:
    utACK 2427939935

Tree-SHA512: 918c1e40d2db4ded6213cd78a18490ad10a9f43c0533df64bdf09f0b216715415030e444712981e4407c32ebf552fbb0e3cce718e048df10c2b8937caf015564
This commit is contained in:
merge-script
2025-09-23 15:01:52 -04:00
2 changed files with 17 additions and 12 deletions

View File

@@ -8,7 +8,8 @@
#include <noui.h>
#include <tinyformat.h>
#include <stdexcept>
#include <cstdlib>
#include <iostream>
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();
}
}

View File

@@ -13,10 +13,7 @@
class DebugLogHelper
{
const std::string m_message;
bool m_found{false};
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
public:
//! Custom match checking function.
//!
//! Invoked with pointers to lines containing matching strings, and with
@@ -27,13 +24,19 @@ 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<bool(const std::string* line)>;
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(const DebugLogHelper&) = delete;
DebugLogHelper& operator=(const DebugLogHelper&) = delete;
~DebugLogHelper();
private:
const std::string m_message;
bool m_found{false};
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
MatchFn m_match;
};
#define ASSERT_DEBUG_LOG(message) DebugLogHelper UNIQUE_NAME(debugloghelper)(message)