test: add ReadDebugLogLines helper function
What changed, and why it matters
This commit is a simple code cleanup in Bitcoin Core's test suite. It creates a small helper function that reads log files during testing, replacing four nearly identical blocks of test code. There is no change to the actual Bitcoin node software, no change in behavior, and no security relevance.
No action required. This is a benign test-only refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors src/test/logging_tests.cpp by introducing a static helper ReadDebugLogLines() that opens LogInstance().m_file_path and returns its lines as std::vector
Changed components
src/test/logging_tests.cppInspect captured patch +15 / −25
diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp
index 4d4141c1..888e1119 100644
--- a/src/test/logging_tests.cpp
+++ b/src/test/logging_tests.cpp
@@ -37,6 +37,16 @@ static void ResetLogger()
LogInstance().SetCategoryLogLevel({});
}
+static std::vector<std::string> ReadDebugLogLines()
+{
+ std::vector<std::string> lines;
+ std::ifstream ifs{LogInstance().m_file_path};
+ for (std::string line; std::getline(ifs, line);) {
+ lines.push_back(std::move(line));
+ }
+ return lines;
+}
+
struct LogSetup : public BasicTestingSetup {
fs::path prev_log_path;
fs::path tmp_log_path;
@@ -120,11 +130,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
expected.push_back(tfm::format("[%s:%s] [%s] %s%s", util::RemovePrefix(loc.file_name(), "./"), loc.line(), loc.function_name(), prefix, msg));
LogInstance().LogPrintStr(msg, std::move(loc), category, level, /*should_ratelimit=*/false);
}
- std::ifstream file{tmp_log_path};
- std::vector<std::string> log_lines;
- for (std::string log; std::getline(file, log);) {
- log_lines.push_back(log);
- }
+ std::vector<std::string> log_lines{ReadDebugLogLines()};
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
}
@@ -136,11 +142,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
- std::ifstream file{tmp_log_path};
- std::vector<std::string> log_lines;
- for (std::string log; std::getline(file, log);) {
- log_lines.push_back(log);
- }
+ std::vector<std::string> log_lines{ReadDebugLogLines()};
std::vector<std::string> expected = {
"foo5: bar5",
"[net] foo7: bar7",
@@ -158,11 +160,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
LogInfo("foo8: %s", "bar8");
LogWarning("foo9: %s", "bar9");
LogError("foo10: %s", "bar10");
- std::ifstream file{tmp_log_path};
- std::vector<std::string> log_lines;
- for (std::string log; std::getline(file, log);) {
- log_lines.push_back(log);
- }
+ std::vector<std::string> log_lines{ReadDebugLogLines()};
std::vector<std::string> expected = {
"[net] foo7: bar7",
"foo8: bar8",
@@ -194,11 +192,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
expected.push_back(expected_log);
}
- std::ifstream file{tmp_log_path};
- std::vector<std::string> log_lines;
- for (std::string log; std::getline(file, log);) {
- log_lines.push_back(log);
- }
+ std::vector<std::string> log_lines{ReadDebugLogLines()};
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
}
@@ -227,11 +221,7 @@ BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
"[net:warning] foo5: bar5",
"[net:error] foo7: bar7",
};
- std::ifstream file{tmp_log_path};
- std::vector<std::string> log_lines;
- for (std::string log; std::getline(file, log);) {
- log_lines.push_back(log);
- }
+ std::vector<std::string> log_lines{ReadDebugLogLines()};
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.