Skip to content

Commit 93bdd04

Browse files
leecannonVexu
authored andcommitted
std.log: add functionality to check if a specific log level and scope are enabled
1 parent 4fa027a commit 93bdd04

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

lib/std/log.zig

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
//! args: anytype,
3030
//! ) void {
3131
//! // Ignore all non-error logging from sources other than
32-
//! // .my_project, .nice_library and .default
32+
//! // .my_project, .nice_library and the default
3333
//! const scope_prefix = "(" ++ switch (scope) {
34-
//! .my_project, .nice_library, .default => @tagName(scope),
34+
//! .my_project, .nice_library, std.log.default_log_scope => @tagName(scope),
3535
//! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err))
3636
//! @tagName(scope)
3737
//! else
@@ -125,22 +125,28 @@ fn log(
125125
comptime format: []const u8,
126126
args: anytype,
127127
) void {
128-
const effective_log_level = blk: {
129-
inline for (scope_levels) |scope_level| {
130-
if (scope_level.scope == scope) break :blk scope_level.level;
131-
}
132-
break :blk level;
133-
};
128+
if (comptime !logEnabled(message_level, scope)) return;
129+
130+
if (@hasDecl(root, "log")) {
131+
if (@typeInfo(@TypeOf(root.log)) != .Fn)
132+
@compileError("Expected root.log to be a function");
133+
root.log(message_level, scope, format, args);
134+
} else {
135+
defaultLog(message_level, scope, format, args);
136+
}
137+
}
134138

135-
if (@enumToInt(message_level) <= @enumToInt(effective_log_level)) {
136-
if (@hasDecl(root, "log")) {
137-
if (@typeInfo(@TypeOf(root.log)) != .Fn)
138-
@compileError("Expected root.log to be a function");
139-
root.log(message_level, scope, format, args);
140-
} else {
141-
defaultLog(message_level, scope, format, args);
142-
}
139+
/// Determine if a specific log message level and scope combination are enabled for logging.
140+
pub fn logEnabled(comptime message_level: Level, comptime scope: @Type(.EnumLiteral)) bool {
141+
inline for (scope_levels) |scope_level| {
142+
if (scope_level.scope == scope) return @enumToInt(message_level) <= @enumToInt(scope_level.level);
143143
}
144+
return @enumToInt(message_level) <= @enumToInt(level);
145+
}
146+
147+
/// Determine if a specific log message level using the default log scope is enabled for logging.
148+
pub fn defaultLogEnabled(comptime message_level: Level) bool {
149+
return comptime logEnabled(message_level, default_log_scope);
144150
}
145151

146152
/// The default implementation for root.log. root.log may forward log messages
@@ -210,8 +216,10 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
210216
};
211217
}
212218

219+
pub const default_log_scope = .default;
220+
213221
/// The default scoped logging namespace.
214-
pub const default = scoped(.default);
222+
pub const default = scoped(default_log_scope);
215223

216224
/// Log an error message using the default scope. This log level is intended to
217225
/// be used when something has gone wrong. This might be recoverable or might

0 commit comments

Comments
 (0)