| ... | @@ -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 than | 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 | //! 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 | //! else | 37 | //! 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 | } |
| 134 | | 138 | |
| 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")) { | 140 | pub 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. |
| | 148 | pub fn defaultLogEnabled(comptime message_level: Level) bool { |
| | 149 | return comptime logEnabled(message_level, default_log_scope); |
| 144 | } | 150 | } |
| 145 | | 151 | |
| 146 | /// The default implementation for root.log. root.log may forward log messages | 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,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 | /// The default scoped logging namespace. | 221 | /// The default scoped logging namespace. |
| 214 | pub const default = scoped(.default); | 222 | pub const default = scoped(default_log_scope); |
| 215 | | 223 | |
| 216 | /// Log an error message using the default scope. This log level is intended to | 224 | /// 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 might | 225 | /// be used when something has gone wrong. This might be recoverable or might |