aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorLee Cannon <leecannon@leecannon.xyz>2022-10-26 21:03:02 +0100
committerVeikka Tuominen <git@vexu.eu>2023-01-03 13:02:32 +0200
commit93bdd04e8880f0ae2ad73b5922d499ec8500a82c (patch)
treef576c785334263fa865aed2fea9f33b5fc7767d9 /lib/std
parent4fa027a3cceb9068db8ff432abc8ab0d464f8b33 (diff)
downloadzig-93bdd04e8880f0ae2ad73b5922d499ec8500a82c.tar.gz
zig-93bdd04e8880f0ae2ad73b5922d499ec8500a82c.zip
std.log: add functionality to check if a specific log level and scope are enabled
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/log.zig42
1 files changed, 25 insertions, 17 deletions
diff --git a/lib/std/log.zig b/lib/std/log.zig
index 9ebe85c004..1d1b514c03 100644
--- a/lib/std/log.zig
+++ b/lib/std/log.zig
@@ -29,9 +29,9 @@
//! args: anytype,
//! ) void {
//! // 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) {
-//! .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))
//! @tagName(scope)
//! else
@@ -125,22 +125,28 @@ fn log(
comptime format: []const u8,
args: anytype,
) void {
- const effective_log_level = blk: {
- inline for (scope_levels) |scope_level| {
- if (scope_level.scope == scope) break :blk scope_level.level;
- }
- break :blk level;
- };
+ if (comptime !logEnabled(message_level, scope)) return;
+
+ if (@hasDecl(root, "log")) {
+ if (@typeInfo(@TypeOf(root.log)) != .Fn)
+ @compileError("Expected root.log to be a function");
+ root.log(message_level, scope, format, args);
+ } else {
+ defaultLog(message_level, scope, format, args);
+ }
+}
- if (@enumToInt(message_level) <= @enumToInt(effective_log_level)) {
- if (@hasDecl(root, "log")) {
- if (@typeInfo(@TypeOf(root.log)) != .Fn)
- @compileError("Expected root.log to be a function");
- root.log(message_level, scope, format, args);
- } else {
- 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
@@ -210,8 +216,10 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
};
}
+pub const default_log_scope = .default;
+
/// 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
/// be used when something has gone wrong. This might be recoverable or might