Skip to content

Commit 350692e

Browse files
committed
Merge bitcoin/bitcoin#33388: test: don't throw from the destructor of DebugLogHelper
2427939 test: forbid copying of DebugLogHelper (Daniel Pfeifer) d6aa266 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 bitcoin/bitcoin#26812. It is an improvement on its own, so creating a separate PR for it following the discussion at bitcoin/bitcoin#32604 (comment). Getting it in will reduce the size of #26812. ACKs for top commit: Crypt-iQ: crACK 2427939 l0rinc: Code review reACK 2427939 optout21: crACK 2427939 furszy: utACK 2427939 Tree-SHA512: 918c1e40d2db4ded6213cd78a18490ad10a9f43c0533df64bdf09f0b216715415030e444712981e4407c32ebf552fbb0e3cce718e048df10c2b8937caf015564
2 parents 89144eb + 2427939 commit 350692e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/test/util/logging.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <noui.h>
99
#include <tinyformat.h>
1010

11-
#include <stdexcept>
11+
#include <cstdlib>
12+
#include <iostream>
1213

1314
DebugLogHelper::DebugLogHelper(std::string message, MatchFn match)
1415
: m_message{std::move(message)}, m_match(std::move(match))
@@ -21,11 +22,12 @@ DebugLogHelper::DebugLogHelper(std::string message, MatchFn match)
2122
noui_test_redirect();
2223
}
2324

24-
void DebugLogHelper::check_found()
25+
DebugLogHelper::~DebugLogHelper()
2526
{
2627
noui_reconnect();
2728
LogInstance().DeleteCallback(m_print_connection);
2829
if (!m_found && m_match(nullptr)) {
29-
throw std::runtime_error(strprintf("'%s' not found in debug log\n", m_message));
30+
tfm::format(std::cerr, "Fatal error: expected message not found in the debug log: '%s'\n", m_message);
31+
std::abort();
3032
}
3133
}

src/test/util/logging.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
class DebugLogHelper
1515
{
16-
const std::string m_message;
17-
bool m_found{false};
18-
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
19-
16+
public:
2017
//! Custom match checking function.
2118
//!
2219
//! Invoked with pointers to lines containing matching strings, and with
@@ -27,13 +24,19 @@ class DebugLogHelper
2724
//! (2) raising an error in check_found if no match was found
2825
//! Can return false to do the opposite in either case.
2926
using MatchFn = std::function<bool(const std::string* line)>;
30-
MatchFn m_match;
3127

32-
void check_found();
33-
34-
public:
3528
explicit DebugLogHelper(std::string message, MatchFn match = [](const std::string*){ return true; });
36-
~DebugLogHelper() noexcept(false) { check_found(); }
29+
30+
DebugLogHelper(const DebugLogHelper&) = delete;
31+
DebugLogHelper& operator=(const DebugLogHelper&) = delete;
32+
33+
~DebugLogHelper();
34+
35+
private:
36+
const std::string m_message;
37+
bool m_found{false};
38+
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
39+
MatchFn m_match;
3740
};
3841

3942
#define ASSERT_DEBUG_LOG(message) DebugLogHelper UNIQUE_NAME(debugloghelper)(message)

0 commit comments

Comments
 (0)