You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
log, refactor: Disallow category args to logging macros
Disallow passing BCLog category constants to LogInfo(), LogWarning(), and
LogError() macros, and disallow omitting BCLog categories when calling
LogDebug() and LogTrace() macros.
Additionally, drop category information from log output at higher levels as
suggested by Hodlinator
bitcoin#29256 (comment).
None of these restrictions are technically necessary, and not everybody
believes they are good. However, they have existed since the
Log{Trace,Debug,Info,Warning,Error} macros were introduced in bitcoin#28318, and
removing these restrictions is orthogonal to the goals of this PR which are:
(1) to allow the Log macros to work without accessing global state and (2) to
support local logging customization with additional metadata.
This change just more clearly documents the current logging restrictions and
provides better error messages to developers when enforcing them.
Co-Authored-By: Hodlinator <172445034+hodlinator@users.noreply.github.com>
//! Trigger compile error if caller tries to pass a category constant as a
438
+
//! first argument to a logging call that specifies take_category == false.
439
+
//! There is no technical reason why all logging calls could not accept
440
+
//! category arguments, but for various reasons, such as (1) not wanting to
441
+
//! allow users filter by category at high priority levels, and (2) wanting
442
+
//! to incentivize developers to use lower log levels to avoid log spam,
443
+
//! passing category constants at higher levels is forbidden.
444
+
static_assert(take_category, "Cannot pass BCLog::LogFlags category argument to Info/Warning/Error logging call. Please switch to Debug/Trace call, or drop the category argument!");
445
+
return Context{LogInstance(), category};
446
+
}
447
+
448
+
template <bool take_category>
449
+
static Context GetContext(std::string_view fmt)
450
+
{
451
+
//! Trigger compile error if caller does not pass a category constant as the
452
+
//! first argument to a logging call that specifies take_category == true.
453
+
//! There is no technical reason why category arguments need to be required,
454
+
//! but categories are useful for finding and filtering relevant messages
455
+
//! when debugging, so we want to encourage them.
456
+
static_assert(!take_category, "Missing required BCLog::LogFlags category argument for Debug/Trace logging call. Category can only be omitted for Info/Warning/Error calls.");
457
+
return Context{LogInstance()};
458
+
}
432
459
433
460
//! Internal helper to format log arguments and call a logging function.
0 commit comments