Skip to content

Commit 04608cb

Browse files
committed
logging: move macros to util
Move logging macros to util/log.h so the entire codebase can use the same macros. Since (un)conditional logging is a node concept, it is no longer mentioned in the util macros. BCLog::Logger still maintains its logic to ensure the log level never drops below Info. LogAcceptCategory is now called for all logging levels as an early-exit check, not just for Debug and Trace.
1 parent db18cdf commit 04608cb

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

doc/developer-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ logging messages. They should be used as follows:
785785
options `-debug=category -loglevel=category:trace` or `-debug=1
786786
-loglevel=trace` are selected.
787787

788+
Be conservative when using `LogInfo`, `LogWarning` or `LogError` as they will
789+
always write to debug.log, unless basic rate limiting quotas are exceeded. It
790+
should not be the case that an inbound peer can fill up a user's storage with
791+
debug.log entries.
792+
788793
Note that the format strings and parameters of `LogDebug` and `LogTrace`
789794
are only evaluated if the logging category is enabled, so you must be
790795
careful to avoid side-effects in those expressions.

src/logging.h

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88

99
#include <crypto/siphash.h>
1010
#include <threadsafety.h>
11-
#include <tinyformat.h>
1211
#include <util/check.h>
1312
#include <util/fs.h>
1413
#include <util/log.h> // IWYU pragma: export
15-
#include <util/string.h>
1614
#include <util/time.h>
1715

1816
#include <atomic>
17+
#include <chrono>
1918
#include <cstdint>
20-
#include <cstring>
2119
#include <functional>
2220
#include <list>
2321
#include <memory>
24-
#include <mutex>
2522
#include <string>
2623
#include <unordered_map>
27-
#include <unordered_set>
2824
#include <vector>
2925

3026
static const bool DEFAULT_LOGTIMEMICROS = false;
@@ -306,35 +302,4 @@ BCLog::Logger& LogInstance();
306302
/** Return true if str parses as a log category and set the flag */
307303
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);
308304

309-
// Allow __func__ to be used in any context without warnings:
310-
// NOLINTNEXTLINE(bugprone-lambda-function-name)
311-
#define LogPrintLevel_(category, level, should_ratelimit, ...) util::log::g_dispatcher().Log(level, category, SourceLocation{__func__}, should_ratelimit, __VA_ARGS__)
312-
313-
// Log unconditionally. Uses basic rate limiting to mitigate disk filling attacks.
314-
// Be conservative when using functions that unconditionally log to debug.log!
315-
// It should not be the case that an inbound peer can fill up a user's storage
316-
// with debug.log entries.
317-
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
318-
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__)
319-
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
320-
321-
// Use a macro instead of a function for conditional logging to prevent
322-
// evaluating arguments when logging for the category is not enabled.
323-
324-
// Log by prefixing the output with the passed category name and severity level. This logs conditionally if
325-
// the category is allowed. No rate limiting is applied, because users specifying -debug are assumed to be
326-
// developers or power users who are aware that -debug may cause excessive disk usage due to logging.
327-
#define detail_LogIfCategoryAndLevelEnabled(category, level, ...) \
328-
do { \
329-
if (LogAcceptCategory((category), (level))) { \
330-
bool rate_limit{level >= BCLog::Level::Info}; \
331-
Assume(!rate_limit);/*Only called with the levels below*/ \
332-
LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \
333-
} \
334-
} while (0)
335-
336-
// Log conditionally, prefixing the output with the passed category name.
337-
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
338-
#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Trace, __VA_ARGS__)
339-
340305
#endif // BITCOIN_LOGGING_H

src/util/log.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <logging/categories.h> // IWYU pragma: export
99
#include <threadsafety.h>
1010
#include <tinyformat.h>
11+
#include <util/check.h>
1112
#include <util/string.h>
1213
#include <util/threadnames.h>
1314

@@ -171,4 +172,25 @@ static inline bool LogAcceptCategory(uint64_t category, util::log::Level level)
171172
return util::log::g_dispatcher().WillLog(level, category);
172173
}
173174

175+
// Allow __func__ to be used in any context without warnings:
176+
// NOLINTNEXTLINE(bugprone-lambda-function-name)
177+
#define LogPrintLevel_(category, level, should_ratelimit, ...) util::log::g_dispatcher().Log(level, category, SourceLocation{__func__}, should_ratelimit, __VA_ARGS__)
178+
179+
// Arguments are always evaluated, even when logging is disabled.
180+
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
181+
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__)
182+
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
183+
184+
// Use a macro instead of a function to prevent evaluating arguments when the log will be dropped anyway.
185+
#define detail_LogIfCategoryAndLevelEnabled(category, level, ...) \
186+
do { \
187+
Assume(level < util::log::Level::Info); \
188+
if (LogAcceptCategory((category), (level))) { \
189+
LogPrintLevel_(category, level, /*should_ratelimit=*/false, __VA_ARGS__); \
190+
} \
191+
} while (0)
192+
193+
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Debug, __VA_ARGS__)
194+
#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Trace, __VA_ARGS__)
195+
174196
#endif // BITCOIN_UTIL_LOG_H

0 commit comments

Comments
 (0)