Skip to content

Commit d6aa266

Browse files
committed
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()`.
1 parent d20f10a commit d6aa266

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-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: 9 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,16 @@ 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;
31-
32-
void check_found();
3327

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

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

0 commit comments

Comments
 (0)