authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2022-10-26 21:03:02+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-03 13:02:32+02:00
log93bdd04e8880f0ae2ad73b5922d499ec8500a82c
treef576c785334263fa865aed2fea9f33b5fc7767d9
parent4fa027a3cceb9068db8ff432abc8ab0d464f8b33

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


1 files changed, 25 insertions(+), 17 deletions(-)

lib/std/log.zig+25-17
...@@ -29,9 +29,9 @@...@@ -29,9 +29,9 @@
29//! args: anytype,29//! args: anytype,
30//! ) void {30//! ) void {
31//! // Ignore all non-error logging from sources other than31//! // Ignore all non-error logging from sources other than
32//! // .my_project, .nice_library and .default32//! // .my_project, .nice_library and the default
33//! const scope_prefix = "(" ++ switch (scope) {33//! 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),
35//! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err))35//! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err))
36//! @tagName(scope)36//! @tagName(scope)
37//! else37//! else
...@@ -125,22 +125,28 @@ fn log(...@@ -125,22 +125,28 @@ fn log(
125 comptime format: []const u8,125 comptime format: []const u8,
126 args: anytype,126 args: anytype,
127) void {127) void {
128 const effective_log_level = blk: {128 if (comptime !logEnabled(message_level, scope)) return;
129 inline for (scope_levels) |scope_level| {129
130 if (scope_level.scope == scope) break :blk scope_level.level;130 if (@hasDecl(root, "log")) {
131 }131 if (@typeInfo(@TypeOf(root.log)) != .Fn)
132 break :blk level;132 @compileError("Expected root.log to be a function");
133 };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)) {139/// Determine if a specific log message level and scope combination are enabled for logging.
136 if (@hasDecl(root, "log")) {140pub fn logEnabled(comptime message_level: Level, comptime scope: @Type(.EnumLiteral)) bool {
137 if (@typeInfo(@TypeOf(root.log)) != .Fn)141 inline for (scope_levels) |scope_level| {
138 @compileError("Expected root.log to be a function");142 if (scope_level.scope == scope) return @enumToInt(message_level) <= @enumToInt(scope_level.level);
139 root.log(message_level, scope, format, args);
140 } else {
141 defaultLog(message_level, scope, format, args);
142 }
143 }143 }
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.
148pub fn defaultLogEnabled(comptime message_level: Level) bool {
149 return comptime logEnabled(message_level, default_log_scope);
144}150}
145151
146/// The default implementation for root.log. root.log may forward log messages152/// The default implementation for root.log. root.log may forward log messages
...@@ -210,8 +216,10 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {...@@ -210,8 +216,10 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
210 };216 };
211}217}
212218
219pub const default_log_scope = .default;
220
213/// The default scoped logging namespace.221/// The default scoped logging namespace.
214pub const default = scoped(.default);222pub const default = scoped(default_log_scope);
215223
216/// Log an error message using the default scope. This log level is intended to224/// Log an error message using the default scope. This log level is intended to
217/// be used when something has gone wrong. This might be recoverable or might225/// be used when something has gone wrong. This might be recoverable or might