|
29 | 29 | //! args: anytype, |
30 | 30 | //! ) void { |
31 | 31 | //! // Ignore all non-error logging from sources other than |
32 | | -//! // .my_project, .nice_library and .default |
| 32 | +//! // .my_project, .nice_library and the default |
33 | 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 | 35 | //! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err)) |
36 | 36 | //! @tagName(scope) |
37 | 37 | //! else |
@@ -125,22 +125,28 @@ fn log( |
125 | 125 | comptime format: []const u8, |
126 | 126 | args: anytype, |
127 | 127 | ) 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 | +} |
134 | 138 |
|
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); |
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. |
| 148 | +pub fn defaultLogEnabled(comptime message_level: Level) bool { |
| 149 | + return comptime logEnabled(message_level, default_log_scope); |
144 | 150 | } |
145 | 151 |
|
146 | 152 | /// 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 | 216 | }; |
211 | 217 | } |
212 | 218 |
|
| 219 | +pub const default_log_scope = .default; |
| 220 | + |
213 | 221 | /// The default scoped logging namespace. |
214 | | -pub const default = scoped(.default); |
| 222 | +pub const default = scoped(default_log_scope); |
215 | 223 |
|
216 | 224 | /// Log an error message using the default scope. This log level is intended to |
217 | 225 | /// be used when something has gone wrong. This might be recoverable or might |
|
0 commit comments