std.log: add functionality to check if a specific log level and scope are enabled

This commit is contained in:
Lee Cannon 2022-10-26 21:03:02 +01:00 committed by Veikka Tuominen
parent 4fa027a3cc
commit 93bdd04e88

View file

@ -29,9 +29,9 @@
//! args: anytype, //! args: anytype,
//! ) void { //! ) void {
//! // Ignore all non-error logging from sources other than //! // Ignore all non-error logging from sources other than
//! // .my_project, .nice_library and .default //! // .my_project, .nice_library and the default
//! const scope_prefix = "(" ++ switch (scope) { //! const scope_prefix = "(" ++ switch (scope) {
//! .my_project, .nice_library, .default => @tagName(scope), //! .my_project, .nice_library, std.log.default_log_scope => @tagName(scope),
//! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err)) //! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err))
//! @tagName(scope) //! @tagName(scope)
//! else //! else
@ -125,14 +125,8 @@ fn log(
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) void { ) void {
const effective_log_level = blk: { if (comptime !logEnabled(message_level, scope)) return;
inline for (scope_levels) |scope_level| {
if (scope_level.scope == scope) break :blk scope_level.level;
}
break :blk level;
};
if (@enumToInt(message_level) <= @enumToInt(effective_log_level)) {
if (@hasDecl(root, "log")) { if (@hasDecl(root, "log")) {
if (@typeInfo(@TypeOf(root.log)) != .Fn) if (@typeInfo(@TypeOf(root.log)) != .Fn)
@compileError("Expected root.log to be a function"); @compileError("Expected root.log to be a function");
@ -140,7 +134,19 @@ fn log(
} else { } else {
defaultLog(message_level, scope, format, args); defaultLog(message_level, scope, format, args);
} }
}
/// Determine if a specific log message level and scope combination are enabled for logging.
pub fn logEnabled(comptime message_level: Level, comptime scope: @Type(.EnumLiteral)) bool {
inline for (scope_levels) |scope_level| {
if (scope_level.scope == scope) return @enumToInt(message_level) <= @enumToInt(scope_level.level);
} }
return @enumToInt(message_level) <= @enumToInt(level);
}
/// Determine if a specific log message level using the default log scope is enabled for logging.
pub fn defaultLogEnabled(comptime message_level: Level) bool {
return comptime logEnabled(message_level, default_log_scope);
} }
/// The default implementation for root.log. root.log may forward log messages /// The default implementation for root.log. root.log may forward log messages
@ -210,8 +216,10 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
}; };
} }
pub const default_log_scope = .default;
/// The default scoped logging namespace. /// The default scoped logging namespace.
pub const default = scoped(.default); pub const default = scoped(default_log_scope);
/// Log an error message using the default scope. This log level is intended to /// Log an error message using the default scope. This log level is intended to
/// be used when something has gone wrong. This might be recoverable or might /// be used when something has gone wrong. This might be recoverable or might