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 @@
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.
140pub 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.
148pub 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
219pub const default_log_scope = .default;
220
213221/// The default scoped logging namespace.
214pub const default = scoped(.default);
222pub 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