Skip to content

Commit 4ed7a51

Browse files
stickies-vfanquake
authored andcommitted
test: add ReadDebugLogLines helper function
Deduplicates repeated usage of the same functionality. Github-Pull: #33011 Rebased-From: 05d7c22
1 parent acfa83d commit 4ed7a51

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/test/logging_tests.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ static void ResetLogger()
3737
LogInstance().SetCategoryLogLevel({});
3838
}
3939

40+
static std::vector<std::string> ReadDebugLogLines()
41+
{
42+
std::vector<std::string> lines;
43+
std::ifstream ifs{LogInstance().m_file_path};
44+
for (std::string line; std::getline(ifs, line);) {
45+
lines.push_back(std::move(line));
46+
}
47+
return lines;
48+
}
49+
4050
struct LogSetup : public BasicTestingSetup {
4151
fs::path prev_log_path;
4252
fs::path tmp_log_path;
@@ -120,11 +130,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
120130
expected.push_back(tfm::format("[%s:%s] [%s] %s%s", util::RemovePrefix(loc.file_name(), "./"), loc.line(), loc.function_name(), prefix, msg));
121131
LogInstance().LogPrintStr(msg, std::move(loc), category, level, /*should_ratelimit=*/false);
122132
}
123-
std::ifstream file{tmp_log_path};
124-
std::vector<std::string> log_lines;
125-
for (std::string log; std::getline(file, log);) {
126-
log_lines.push_back(log);
127-
}
133+
std::vector<std::string> log_lines{ReadDebugLogLines()};
128134
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
129135
}
130136

@@ -136,11 +142,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
136142
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
137143
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
138144
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
139-
std::ifstream file{tmp_log_path};
140-
std::vector<std::string> log_lines;
141-
for (std::string log; std::getline(file, log);) {
142-
log_lines.push_back(log);
143-
}
145+
std::vector<std::string> log_lines{ReadDebugLogLines()};
144146
std::vector<std::string> expected = {
145147
"foo5: bar5",
146148
"[net] foo7: bar7",
@@ -158,11 +160,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
158160
LogInfo("foo8: %s", "bar8");
159161
LogWarning("foo9: %s", "bar9");
160162
LogError("foo10: %s", "bar10");
161-
std::ifstream file{tmp_log_path};
162-
std::vector<std::string> log_lines;
163-
for (std::string log; std::getline(file, log);) {
164-
log_lines.push_back(log);
165-
}
163+
std::vector<std::string> log_lines{ReadDebugLogLines()};
166164
std::vector<std::string> expected = {
167165
"[net] foo7: bar7",
168166
"foo8: bar8",
@@ -194,11 +192,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
194192
expected.push_back(expected_log);
195193
}
196194

197-
std::ifstream file{tmp_log_path};
198-
std::vector<std::string> log_lines;
199-
for (std::string log; std::getline(file, log);) {
200-
log_lines.push_back(log);
201-
}
195+
std::vector<std::string> log_lines{ReadDebugLogLines()};
202196
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
203197
}
204198

@@ -227,11 +221,7 @@ BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
227221
"[net:warning] foo5: bar5",
228222
"[net:error] foo7: bar7",
229223
};
230-
std::ifstream file{tmp_log_path};
231-
std::vector<std::string> log_lines;
232-
for (std::string log; std::getline(file, log);) {
233-
log_lines.push_back(log);
234-
}
224+
std::vector<std::string> log_lines{ReadDebugLogLines()};
235225
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
236226
}
237227

0 commit comments

Comments
 (0)