Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <init/common.h>
6 : : #include <logging.h>
7 : : #include <logging/timer.h>
8 : : #include <scheduler.h>
9 : : #include <test/util/logging.h>
10 : : #include <test/util/setup_common.h>
11 : : #include <tinyformat.h>
12 : : #include <util/fs.h>
13 : : #include <util/fs_helpers.h>
14 : : #include <util/string.h>
15 : :
16 : : #include <chrono>
17 : : #include <fstream>
18 : : #include <future>
19 : : #include <ios>
20 : : #include <iostream>
21 : : #include <source_location>
22 : : #include <string>
23 : : #include <unordered_map>
24 : : #include <utility>
25 : : #include <vector>
26 : :
27 : : #include <boost/test/unit_test.hpp>
28 : :
29 : : using util::SplitString;
30 : : using util::TrimString;
31 : :
32 : : BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup)
33 : :
34 : 3 : static void ResetLogger()
35 : : {
36 : 3 : LogInstance().SetLogLevel(BCLog::DEFAULT_LOG_LEVEL);
37 [ + - ]: 3 : LogInstance().SetCategoryLogLevel({});
38 : 3 : }
39 : :
40 : 43 : static std::vector<std::string> ReadDebugLogLines()
41 : : {
42 : 43 : std::vector<std::string> lines;
43 [ + - + - ]: 43 : std::ifstream ifs{LogInstance().m_file_path.std_path()};
44 [ + + ]: 170 : for (std::string line; std::getline(ifs, line);) {
45 [ + - + - ]: 211 : lines.push_back(std::move(line));
46 : 0 : }
47 : 43 : return lines;
48 : 43 : }
49 : :
50 : : struct LogSetup : public BasicTestingSetup {
51 : : fs::path prev_log_path;
52 : : fs::path tmp_log_path;
53 : : bool prev_reopen_file;
54 : : bool prev_print_to_file;
55 : : bool prev_log_timestamps;
56 : : bool prev_log_threadnames;
57 : : bool prev_log_sourcelocations;
58 : : std::unordered_map<BCLog::LogFlags, BCLog::Level> prev_category_levels;
59 : : BCLog::Level prev_log_level;
60 : : BCLog::CategoryMask prev_category_mask;
61 : :
62 [ + - ]: 6 : LogSetup() : prev_log_path{LogInstance().m_file_path},
63 [ + - + - ]: 12 : tmp_log_path{m_args.GetDataDirBase() / "tmp_debug.log"},
64 [ + - + - ]: 6 : prev_reopen_file{LogInstance().m_reopen_file},
65 [ + - ]: 6 : prev_print_to_file{LogInstance().m_print_to_file},
66 [ + - ]: 6 : prev_log_timestamps{LogInstance().m_log_timestamps},
67 [ + - ]: 6 : prev_log_threadnames{LogInstance().m_log_threadnames},
68 [ + - ]: 6 : prev_log_sourcelocations{LogInstance().m_log_sourcelocations},
69 [ + - + - ]: 6 : prev_category_levels{LogInstance().CategoryLevels()},
70 [ + - + - ]: 6 : prev_log_level{LogInstance().LogLevel()},
71 [ + - + - : 18 : prev_category_mask{LogInstance().GetCategoryMask()}
+ - + - ]
72 : : {
73 [ + - + - ]: 6 : LogInstance().m_file_path = tmp_log_path;
74 [ + - + - ]: 6 : LogInstance().m_reopen_file = true;
75 [ + - ]: 6 : LogInstance().m_print_to_file = true;
76 [ + - ]: 6 : LogInstance().m_log_timestamps = false;
77 [ + - ]: 6 : LogInstance().m_log_threadnames = false;
78 : :
79 : : // Prevent tests from failing when the line number of the logs changes.
80 [ + - ]: 6 : LogInstance().m_log_sourcelocations = false;
81 : :
82 [ + - + - ]: 6 : LogInstance().SetLogLevel(BCLog::Level::Debug);
83 [ + - + - ]: 6 : LogInstance().DisableCategory(BCLog::LogFlags::ALL);
84 [ + - + - ]: 6 : LogInstance().SetCategoryLogLevel({});
85 [ + - + - ]: 6 : LogInstance().SetRateLimiting(nullptr);
86 : 6 : }
87 : :
88 : 6 : ~LogSetup()
89 : : {
90 : 6 : LogInstance().m_file_path = prev_log_path;
91 : 6 : LogInfo("Sentinel log to reopen log file");
92 : 6 : LogInstance().m_print_to_file = prev_print_to_file;
93 : 6 : LogInstance().m_reopen_file = prev_reopen_file;
94 : 6 : LogInstance().m_log_timestamps = prev_log_timestamps;
95 : 6 : LogInstance().m_log_threadnames = prev_log_threadnames;
96 : 6 : LogInstance().m_log_sourcelocations = prev_log_sourcelocations;
97 : 6 : LogInstance().SetLogLevel(prev_log_level);
98 : 6 : LogInstance().SetCategoryLogLevel(prev_category_levels);
99 [ - + ]: 6 : LogInstance().SetRateLimiting(nullptr);
100 : 6 : LogInstance().DisableCategory(BCLog::LogFlags::ALL);
101 : 6 : LogInstance().EnableCategory(BCLog::LogFlags{prev_category_mask});
102 : 18 : }
103 : : };
104 : :
105 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logging_timer)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
106 : : {
107 [ + - + - ]: 2 : auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
108 [ + - ]: 1 : const std::string_view result_prefix{"tests: msg ("};
109 [ + - + - : 1 : BOOST_CHECK_EQUAL(micro_timer.LogMsg("msg").substr(0, result_prefix.size()), result_prefix);
+ - + - +
- ]
110 : 1 : }
111 : :
112 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
113 : : {
114 : 1 : LogInstance().m_log_sourcelocations = true;
115 : :
116 : 12 : struct Case {
117 : : std::string msg;
118 : : BCLog::LogFlags category;
119 : : BCLog::Level level;
120 : : std::string prefix;
121 : : SourceLocation loc;
122 : : };
123 : :
124 : 1 : std::vector<Case> cases = {
125 : : {"foo1: bar1", BCLog::NET, BCLog::Level::Debug, "[net] ", SourceLocation{__func__}},
126 : : {"foo2: bar2", BCLog::NET, BCLog::Level::Info, "[net:info] ", SourceLocation{__func__}},
127 : : {"foo3: bar3", BCLog::ALL, BCLog::Level::Debug, "[debug] ", SourceLocation{__func__}},
128 : : {"foo4: bar4", BCLog::ALL, BCLog::Level::Info, "", SourceLocation{__func__}},
129 : : {"foo5: bar5", BCLog::NONE, BCLog::Level::Debug, "[debug] ", SourceLocation{__func__}},
130 : : {"foo6: bar6", BCLog::NONE, BCLog::Level::Info, "", SourceLocation{__func__}},
131 [ - + + + : 7 : };
- - ]
132 : :
133 : 1 : std::vector<std::string> expected;
134 [ + + ]: 7 : for (auto& [msg, category, level, prefix, loc] : cases) {
135 [ + - + - : 18 : expected.push_back(tfm::format("[%s:%s] [%s] %s%s", util::RemovePrefix(loc.file_name(), "./"), loc.line(), loc.function_name_short(), prefix, msg));
+ - ]
136 [ + - - + : 6 : LogInstance().LogPrintStr(msg, std::move(loc), category, level, /*should_ratelimit=*/false);
+ - ]
137 : : }
138 [ + - ]: 1 : std::vector<std::string> log_lines{ReadDebugLogLines()};
139 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
+ - ]
140 [ + - + - : 2 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
- + - - ]
141 : :
142 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
143 : : {
144 : 1 : LogInstance().EnableCategory(BCLog::NET);
145 [ - + ]: 1 : LogTrace(BCLog::NET, "foo6: %s", "bar6"); // not logged
146 [ + - ]: 1 : LogDebug(BCLog::NET, "foo7: %s", "bar7");
147 : 1 : LogInfo("foo8: %s", "bar8");
148 : 1 : LogWarning("foo9: %s", "bar9");
149 : 1 : LogError("foo10: %s", "bar10");
150 : 1 : std::vector<std::string> log_lines{ReadDebugLogLines()};
151 : 1 : std::vector<std::string> expected = {
152 : : "[net] foo7: bar7",
153 : : "foo8: bar8",
154 : : "[warning] foo9: bar9",
155 : : "[error] foo10: bar10",
156 [ + - ]: 1 : };
157 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
+ - ]
158 : 1 : }
159 : :
160 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
161 : : {
162 : 1 : LogInstance().EnableCategory(BCLog::LogFlags::ALL);
163 : 1 : const auto concatenated_category_names = LogInstance().LogCategoriesString();
164 : 1 : std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
165 [ - + + - ]: 1 : const auto category_names = SplitString(concatenated_category_names, ',');
166 [ + + ]: 31 : for (const auto& category_name : category_names) {
167 : 30 : BCLog::LogFlags category;
168 [ - + + - ]: 30 : const auto trimmed_category_name = TrimString(category_name);
169 [ + - - + : 60 : BOOST_REQUIRE(GetLogCategory(category, trimmed_category_name));
+ - + - +
- ]
170 [ + - ]: 30 : expected_category_names.emplace_back(category, trimmed_category_name);
171 : 30 : }
172 : :
173 : 1 : std::vector<std::string> expected;
174 [ + - + + ]: 31 : for (const auto& [category, name] : expected_category_names) {
175 [ + - + - : 30 : LogDebug(category, "foo: %s\n", "bar");
+ - ]
176 [ + - ]: 30 : std::string expected_log = "[";
177 [ - + ]: 30 : expected_log += name;
178 [ + - ]: 30 : expected_log += "] foo: bar";
179 [ + - ]: 30 : expected.push_back(expected_log);
180 : 30 : }
181 : :
182 [ + - ]: 1 : std::vector<std::string> log_lines{ReadDebugLogLines()};
183 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
+ - ]
184 : 1 : }
185 : :
186 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
187 : : {
188 : 1 : LogInstance().SetLogLevel(BCLog::Level::Debug);
189 : 1 : LogInstance().EnableCategory(BCLog::LogFlags::ALL);
190 : 1 : LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
191 : :
192 : : // Global log level
193 : 1 : LogInfo("info_%s", 1);
194 [ - + ]: 1 : LogTrace(BCLog::HTTP, "trace_%s. This log level is lower than the global one.", 2);
195 [ + - ]: 1 : LogDebug(BCLog::HTTP, "debug_%s", 3);
196 : 1 : LogWarning("warn_%s", 4);
197 : 1 : LogError("err_%s", 5);
198 : :
199 : : // Category-specific log level
200 [ - + ]: 1 : LogDebug(BCLog::NET, "debug_%s. This log level is the same as the global one but lower than the category-specific one, which takes precedence.", 6);
201 : :
202 : 1 : std::vector<std::string> expected = {
203 : : "info_1",
204 : : "[http] debug_3",
205 : : "[warning] warn_4",
206 : : "[error] err_5",
207 : 1 : };
208 [ + - ]: 1 : std::vector<std::string> log_lines{ReadDebugLogLines()};
209 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
+ - ]
210 : 1 : }
211 : :
212 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
213 : : {
214 : : // Set global log level
215 : 1 : {
216 : 1 : ResetLogger();
217 : 1 : ArgsManager args;
218 [ + - + - : 2 : args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
+ - ]
219 : 1 : const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
220 [ + - ]: 1 : std::string err;
221 [ + - + - : 2 : BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
+ - + - ]
222 : :
223 [ + - ]: 1 : auto result = init::SetLoggingLevel(args);
224 [ + - + - : 2 : BOOST_REQUIRE(result);
+ - ]
225 [ + - + - : 1 : BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
+ - ]
226 : 1 : }
227 : :
228 : : // Set category-specific log level
229 : 1 : {
230 : 1 : ResetLogger();
231 : 1 : ArgsManager args;
232 [ + - + - : 2 : args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
+ - ]
233 : 1 : const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
234 [ + - ]: 1 : std::string err;
235 [ + - + - : 2 : BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
+ - + - ]
236 : :
237 [ + - ]: 1 : auto result = init::SetLoggingLevel(args);
238 [ + - + - : 2 : BOOST_REQUIRE(result);
+ - ]
239 [ + - + - : 1 : BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::DEFAULT_LOG_LEVEL);
+ - ]
240 : :
241 [ + - + - ]: 1 : const auto& category_levels{LogInstance().CategoryLevels()};
242 : 1 : const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
243 [ + - + - : 3 : BOOST_REQUIRE(net_it != category_levels.end());
+ - + - ]
244 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
245 : 1 : }
246 : :
247 : : // Set both global log level and category-specific log level
248 : 1 : {
249 : 1 : ResetLogger();
250 : 1 : ArgsManager args;
251 [ + - + - : 2 : args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
+ - ]
252 : 1 : const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
253 [ + - ]: 1 : std::string err;
254 [ + - + - : 2 : BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
+ - + - ]
255 : :
256 [ + - ]: 1 : auto result = init::SetLoggingLevel(args);
257 [ + - + - : 2 : BOOST_REQUIRE(result);
+ - ]
258 [ + - + - : 1 : BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
+ - ]
259 : :
260 [ + - + - ]: 2 : const auto& category_levels{LogInstance().CategoryLevels()};
261 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(category_levels.size(), 2);
262 : :
263 : 1 : const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
264 [ + - + - : 3 : BOOST_CHECK(net_it != category_levels.end());
+ - + - ]
265 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
266 : :
267 : 1 : const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
268 [ + - + - : 3 : BOOST_CHECK(http_it != category_levels.end());
+ - + - ]
269 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(http_it->second, BCLog::Level::Info);
270 : 1 : }
271 : 1 : }
272 : :
273 : : struct ScopedScheduler {
274 : : CScheduler scheduler{};
275 : :
276 : 2 : ScopedScheduler()
277 : 2 : {
278 [ + - ]: 4 : scheduler.m_service_thread = std::thread([this] { scheduler.serviceQueue(); });
279 : 2 : }
280 : 2 : ~ScopedScheduler()
281 : : {
282 : 0 : scheduler.stop();
283 : 2 : }
284 : 2 : void MockForwardAndSync(std::chrono::seconds duration)
285 : : {
286 : 2 : scheduler.MockForward(duration);
287 : 2 : std::promise<void> promise;
288 [ + - ]: 4 : scheduler.scheduleFromNow([&promise] { promise.set_value(); }, 0ms);
289 [ + - + - ]: 4 : promise.get_future().wait();
290 : 2 : }
291 : 2 : std::shared_ptr<BCLog::LogRateLimiter> GetLimiter(size_t max_bytes, std::chrono::seconds window)
292 : : {
293 : 4 : auto sched_func = [this](auto func, auto w) {
294 [ + - ]: 2 : scheduler.scheduleEvery(std::move(func), w);
295 : 2 : };
296 [ + - ]: 4 : return BCLog::LogRateLimiter::Create(sched_func, max_bytes, window);
297 : : }
298 : : };
299 : :
300 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logging_log_rate_limiter)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
301 : : {
302 : 1 : uint64_t max_bytes{1024};
303 : 1 : auto reset_window{1min};
304 : 1 : ScopedScheduler scheduler{};
305 [ + - ]: 1 : auto limiter_{scheduler.GetLimiter(max_bytes, reset_window)};
306 [ - + + - ]: 1 : auto& limiter{*Assert(limiter_)};
307 : :
308 : 1 : using Status = BCLog::LogRateLimiter::Status;
309 [ + - ]: 1 : auto source_loc_1{SourceLocation{__func__}};
310 : 1 : auto source_loc_2{SourceLocation{__func__}};
311 : :
312 : : // A fresh limiter should not have any suppressions
313 [ + - + - : 2 : BOOST_CHECK(!limiter.SuppressionsActive());
+ - ]
314 : :
315 : : // Resetting an unused limiter is fine
316 [ + - ]: 1 : limiter.Reset();
317 [ + - + - : 2 : BOOST_CHECK(!limiter.SuppressionsActive());
+ - ]
318 : :
319 : : // No suppression should happen until more than max_bytes have been consumed
320 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, std::string(max_bytes - 1, 'a')), Status::UNSUPPRESSED);
+ - + - ]
321 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::UNSUPPRESSED);
+ - + - ]
322 [ + - + - : 2 : BOOST_CHECK(!limiter.SuppressionsActive());
+ - ]
323 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::NEWLY_SUPPRESSED);
+ - + - ]
324 [ + - + - : 2 : BOOST_CHECK(limiter.SuppressionsActive());
+ - ]
325 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::STILL_SUPPRESSED);
+ - + - ]
326 [ + - + - : 2 : BOOST_CHECK(limiter.SuppressionsActive());
+ - ]
327 : :
328 : : // Location 2 should not be affected by location 1's suppression
329 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
+ - + - ]
330 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, "a"), Status::NEWLY_SUPPRESSED);
+ - + - ]
331 [ + - + - ]: 2 : BOOST_CHECK(limiter.SuppressionsActive());
332 : :
333 : : // After reset_window time has passed, all suppressions should be cleared.
334 [ + - ]: 1 : scheduler.MockForwardAndSync(reset_window);
335 : :
336 [ + - + - : 2 : BOOST_CHECK(!limiter.SuppressionsActive());
+ - ]
337 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
+ - + - ]
338 [ + - + - : 1 : BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
+ - + - +
- ]
339 : 2 : }
340 : :
341 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logging_log_limit_stats)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
342 : : {
343 : 1 : BCLog::LogRateLimiter::Stats stats(BCLog::RATELIMIT_MAX_BYTES);
344 : :
345 : : // Check that stats gets initialized correctly.
346 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_available_bytes, BCLog::RATELIMIT_MAX_BYTES);
347 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_dropped_bytes, uint64_t{0});
348 : :
349 : 1 : const uint64_t MESSAGE_SIZE{BCLog::RATELIMIT_MAX_BYTES / 2};
350 [ + - + - ]: 2 : BOOST_CHECK(stats.Consume(MESSAGE_SIZE));
351 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_available_bytes, BCLog::RATELIMIT_MAX_BYTES - MESSAGE_SIZE);
352 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_dropped_bytes, uint64_t{0});
353 : :
354 [ + - + - ]: 2 : BOOST_CHECK(stats.Consume(MESSAGE_SIZE));
355 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_available_bytes, BCLog::RATELIMIT_MAX_BYTES - MESSAGE_SIZE * 2);
356 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_dropped_bytes, uint64_t{0});
357 : :
358 : : // Consuming more bytes after already having consumed RATELIMIT_MAX_BYTES should fail.
359 [ + - + - ]: 2 : BOOST_CHECK(!stats.Consume(500));
360 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_available_bytes, uint64_t{0});
361 [ + - ]: 1 : BOOST_CHECK_EQUAL(stats.m_dropped_bytes, uint64_t{500});
362 : 1 : }
363 : :
364 : : namespace {
365 : :
366 : : enum class Location {
367 : : INFO_1,
368 : : INFO_2,
369 : : DEBUG_LOG,
370 : : INFO_NOLIMIT,
371 : : };
372 : :
373 : 38 : void LogFromLocation(Location location, const std::string& message) {
374 [ + + + + : 38 : switch (location) {
- ]
375 : 13 : case Location::INFO_1:
376 : 13 : LogInfo("%s\n", message);
377 : 13 : return;
378 : 1 : case Location::INFO_2:
379 : 1 : LogInfo("%s\n", message);
380 : 1 : return;
381 : 12 : case Location::DEBUG_LOG:
382 [ + - ]: 12 : LogDebug(BCLog::LogFlags::HTTP, "%s\n", message);
383 : : return;
384 : 12 : case Location::INFO_NOLIMIT:
385 : 12 : LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/false, "%s\n", message);
386 : 12 : return;
387 : : } // no default case, so the compiler can warn about missing cases
388 : 0 : assert(false);
389 : : }
390 : :
391 : : /**
392 : : * For a given `location` and `message`, ensure that the on-disk debug log behaviour resembles what
393 : : * we'd expect it to be for `status` and `suppressions_active`.
394 : : */
395 : 38 : void TestLogFromLocation(Location location, const std::string& message,
396 : : BCLog::LogRateLimiter::Status status, bool suppressions_active,
397 : : std::source_location source = std::source_location::current())
398 : : {
399 [ + - + - : 151 : BOOST_TEST_INFO_SCOPE("TestLogFromLocation called from " << source.file_name() << ":" << source.line());
+ - ]
400 : 38 : using Status = BCLog::LogRateLimiter::Status;
401 [ + + - + ]: 38 : if (!suppressions_active) assert(status == Status::UNSUPPRESSED); // developer error
402 : :
403 [ + - + - ]: 38 : std::ofstream ofs(LogInstance().m_file_path.std_path(), std::ios::out | std::ios::trunc); // clear debug log
404 [ + - ]: 38 : LogFromLocation(location, message);
405 [ + - ]: 38 : auto log_lines{ReadDebugLogLines()};
406 [ - + + - : 38 : BOOST_TEST_INFO_SCOPE(log_lines.size() << " log_lines read: \n" << util::Join(log_lines, "\n"));
+ - + + ]
407 : :
408 [ + + ]: 38 : if (status == Status::STILL_SUPPRESSED) {
409 [ + - - + : 1 : BOOST_CHECK_EQUAL(log_lines.size(), 0);
+ - ]
410 : 1 : return;
411 : : }
412 : :
413 [ + + ]: 37 : if (status == Status::NEWLY_SUPPRESSED) {
414 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(log_lines.size(), 2);
+ - ]
415 [ + - - + : 2 : BOOST_CHECK(log_lines[0].starts_with("[*] [warning] Excessive logging detected"));
+ - ]
416 : 1 : log_lines.erase(log_lines.begin());
417 : : }
418 [ + - - + : 37 : BOOST_REQUIRE_EQUAL(log_lines.size(), 1);
+ - ]
419 : 37 : auto& payload{log_lines.back()};
420 [ + - - + : 37 : BOOST_CHECK_EQUAL(suppressions_active, payload.starts_with("[*]"));
+ - ]
421 [ + - - + : 74 : BOOST_CHECK(payload.ends_with(message));
- + + - ]
422 : 38 : }
423 : :
424 : : } // namespace
425 : :
426 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(logging_filesize_rate_limit, LogSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
427 : : {
428 : 1 : using Status = BCLog::LogRateLimiter::Status;
429 : 1 : LogInstance().m_log_timestamps = false;
430 : 1 : LogInstance().m_log_sourcelocations = false;
431 : 1 : LogInstance().m_log_threadnames = false;
432 : 1 : LogInstance().EnableCategory(BCLog::LogFlags::HTTP);
433 : :
434 : 1 : constexpr int64_t line_length{1024};
435 : 1 : constexpr int64_t num_lines{10};
436 : 1 : constexpr int64_t bytes_quota{line_length * num_lines};
437 : 1 : constexpr auto time_window{1h};
438 : :
439 : 1 : ScopedScheduler scheduler{};
440 [ + - ]: 1 : auto limiter{scheduler.GetLimiter(bytes_quota, time_window)};
441 [ + - + - : 2 : LogInstance().SetRateLimiting(limiter);
+ - ]
442 : :
443 [ + - ]: 1 : const std::string log_message(line_length - 1, 'a'); // subtract one for newline
444 : :
445 [ + + ]: 11 : for (int i = 0; i < num_lines; ++i) {
446 [ + - ]: 10 : TestLogFromLocation(Location::INFO_1, log_message, Status::UNSUPPRESSED, /*suppressions_active=*/false);
447 : : }
448 [ + - + - ]: 1 : TestLogFromLocation(Location::INFO_1, "a", Status::NEWLY_SUPPRESSED, /*suppressions_active=*/true);
449 [ + - + - ]: 1 : TestLogFromLocation(Location::INFO_1, "b", Status::STILL_SUPPRESSED, /*suppressions_active=*/true);
450 [ + - + - ]: 1 : TestLogFromLocation(Location::INFO_2, "c", Status::UNSUPPRESSED, /*suppressions_active=*/true);
451 : 1 : {
452 [ + - ]: 1 : scheduler.MockForwardAndSync(time_window);
453 [ + - + - : 2 : BOOST_CHECK(ReadDebugLogLines().back().starts_with("[warning] Restarting logging"));
- + + - +
- ]
454 : : }
455 : : // Check that logging from previously suppressed location is unsuppressed again.
456 [ + - ]: 1 : TestLogFromLocation(Location::INFO_1, log_message, Status::UNSUPPRESSED, /*suppressions_active=*/false);
457 : : // Check that conditional logging, and unconditional logging with should_ratelimit=false is
458 : : // not being ratelimited.
459 [ + + ]: 3 : for (Location location : {Location::DEBUG_LOG, Location::INFO_NOLIMIT}) {
460 [ + + ]: 26 : for (int i = 0; i < num_lines + 2; ++i) {
461 [ + - ]: 24 : TestLogFromLocation(location, log_message, Status::UNSUPPRESSED, /*suppressions_active=*/false);
462 : : }
463 : : }
464 [ + - ]: 3 : }
465 : :
466 : : BOOST_AUTO_TEST_SUITE_END()
|