| ... | @@ -18,8 +18,8 @@ | ... | @@ -18,8 +18,8 @@ |
| 18 | //! ``` | 18 | //! ``` |
| 19 | //! const std = @import("std"); | 19 | //! const std = @import("std"); |
| 20 | //! | 20 | //! |
| 21 | //! // Set the log level to warning | 21 | //! // Set the log level to info |
| 22 | //! pub const log_level: std.log.Level = .warn; | 22 | //! pub const log_level: std.log.Level = .info; |
| 23 | //! | 23 | //! |
| 24 | //! // Define root.log to override the std implementation | 24 | //! // Define root.log to override the std implementation |
| 25 | //! pub fn log( | 25 | //! pub fn log( |
| ... | @@ -28,17 +28,17 @@ | ... | @@ -28,17 +28,17 @@ |
| 28 | //! comptime format: []const u8, | 28 | //! comptime format: []const u8, |
| 29 | //! args: anytype, | 29 | //! args: anytype, |
| 30 | //! ) void { | 30 | //! ) void { |
| 31 | //! // Ignore all non-critical 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 .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, .default => @tagName(scope), |
| 35 | //! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.crit)) | 35 | //! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.err)) |
| 36 | //! @tagName(scope) | 36 | //! @tagName(scope) |
| 37 | //! else | 37 | //! else |
| 38 | //! return, | 38 | //! return, |
| 39 | //! } ++ "): "; | 39 | //! } ++ "): "; |
| 40 | //! | 40 | //! |
| 41 | //! const prefix = "[" ++ @tagName(level) ++ "] " ++ scope_prefix; | 41 | //! const prefix = "[" ++ level.asText() ++ "] " ++ scope_prefix; |
| 42 | //! | 42 | //! |
| 43 | //! // Print the message to stderr, silently ignoring any errors | 43 | //! // Print the message to stderr, silently ignoring any errors |
| 44 | //! const held = std.debug.getStderrMutex().acquire(); | 44 | //! const held = std.debug.getStderrMutex().acquire(); |
| ... | @@ -49,23 +49,23 @@ | ... | @@ -49,23 +49,23 @@ |
| 49 | //! | 49 | //! |
| 50 | //! pub fn main() void { | 50 | //! pub fn main() void { |
| 51 | //! // Using the default scope: | 51 | //! // Using the default scope: |
| 52 | //! std.log.info("Just a simple informational log message", .{}); // Won't be printed as log_level is .warn | 52 | //! std.log.debug("A borderline useless debug log message", .{}); // Won't be printed as log_level is .info |
| 53 | //! std.log.warn("Flux capacitor is starting to overheat", .{}); | 53 | //! std.log.info("Flux capacitor is starting to overheat", .{}); |
| 54 | //! | 54 | //! |
| 55 | //! // Using scoped logging: | 55 | //! // Using scoped logging: |
| 56 | //! const my_project_log = std.log.scoped(.my_project); | 56 | //! const my_project_log = std.log.scoped(.my_project); |
| 57 | //! const nice_library_log = std.log.scoped(.nice_library); | 57 | //! const nice_library_log = std.log.scoped(.nice_library); |
| 58 | //! const verbose_lib_log = std.log.scoped(.verbose_lib); | 58 | //! const verbose_lib_log = std.log.scoped(.verbose_lib); |
| 59 | //! | 59 | //! |
| 60 | //! my_project_log.info("Starting up", .{}); // Won't be printed as log_level is .warn | 60 | //! my_project_log.debug("Starting up", .{}); // Won't be printed as log_level is .info |
| 61 | //! nice_library_log.err("Something went very wrong, sorry", .{}); | 61 | //! nice_library_log.warn("Something went very wrong, sorry", .{}); |
| 62 | //! verbose_lib_log.err("Added 1 + 1: {}", .{1 + 1}); // Won't be printed as it gets filtered out by our log function | 62 | //! verbose_lib_log.warn("Added 1 + 1: {}", .{1 + 1}); // Won't be printed as it gets filtered out by our log function |
| 63 | //! } | 63 | //! } |
| 64 | //! ``` | 64 | //! ``` |
| 65 | //! Which produces the following output: | 65 | //! Which produces the following output: |
| 66 | //! ``` | 66 | //! ``` |
| 67 | //! [warn] (default): Flux capacitor is starting to overheat | 67 | //! [info] (default): Flux capacitor is starting to overheat |
| 68 | //! [err] (nice_library): Something went very wrong, sorry | 68 | //! [warning] (nice_library): Something went very wrong, sorry |
| 69 | //! ``` | 69 | //! ``` |
| 70 | | 70 | |
| 71 | const std = @import("std.zig"); | 71 | const std = @import("std.zig"); |
| ... | @@ -73,42 +73,29 @@ const builtin = @import("builtin"); | ... | @@ -73,42 +73,29 @@ const builtin = @import("builtin"); |
| 73 | const root = @import("root"); | 73 | const root = @import("root"); |
| 74 | | 74 | |
| 75 | pub const Level = enum { | 75 | pub const Level = enum { |
| 76 | /// Emergency: a condition that cannot be handled, usually followed by a | 76 | /// Error: something has gone wrong. This might be recoverable or might |
| 77 | /// panic. | 77 | /// be followed by the program exiting. |
| 78 | emerg, | | |
| 79 | /// Alert: a condition that should be corrected immediately (e.g. database | | |
| 80 | /// corruption). | | |
| 81 | alert, | | |
| 82 | /// Critical: A bug has been detected or something has gone wrong and it | | |
| 83 | /// will have an effect on the operation of the program. | | |
| 84 | crit, | | |
| 85 | /// Error: A bug has been detected or something has gone wrong but it is | | |
| 86 | /// recoverable. | | |
| 87 | err, | 78 | err, |
| 88 | /// Warning: it is uncertain if something has gone wrong or not, but the | 79 | /// Warning: it is uncertain if something has gone wrong or not, but the |
| 89 | /// circumstances would be worth investigating. | 80 | /// circumstances would be worth investigating. |
| 90 | warn, | 81 | warn, |
| 91 | /// Notice: non-error but significant conditions. | 82 | /// Info: general messages about the state of the program. |
| 92 | notice, | | |
| 93 | /// Informational: general messages about the state of the program. | | |
| 94 | info, | 83 | info, |
| 95 | /// Debug: messages only useful for debugging. | 84 | /// Debug: messages only useful for debugging. |
| 96 | debug, | 85 | debug, |
| 97 | | 86 | |
| 98 | /// Returns a string literal of the given level in full text form. | 87 | /// Returns a string literal of the given level in full text form. |
| 99 | pub fn asText(comptime self: Level) switch (self) { | 88 | pub fn asText(comptime self: Level) switch (self) { |
| 100 | .emerg => @TypeOf("emergency"), | | |
| 101 | .crit => @TypeOf("critical"), | | |
| 102 | .err => @TypeOf("error"), | 89 | .err => @TypeOf("error"), |
| 103 | .warn => @TypeOf("warning"), | 90 | .warn => @TypeOf("warning"), |
| 104 | else => @TypeOf(@tagName(self)), | 91 | .info => @TypeOf("info"), |
| | 92 | .debug => @TypeOf("debug"), |
| 105 | } { | 93 | } { |
| 106 | return switch (self) { | 94 | return switch (self) { |
| 107 | .emerg => "emergency", | | |
| 108 | .crit => "critical", | | |
| 109 | .err => "error", | 95 | .err => "error", |
| 110 | .warn => "warning", | 96 | .warn => "warning", |
| 111 | else => @tagName(self), | 97 | .info => "info", |
| | 98 | .debug => "debug", |
| 112 | }; | 99 | }; |
| 113 | } | 100 | } |
| 114 | }; | 101 | }; |
| ... | @@ -116,9 +103,8 @@ pub const Level = enum { | ... | @@ -116,9 +103,8 @@ pub const Level = enum { |
| 116 | /// The default log level is based on build mode. | 103 | /// The default log level is based on build mode. |
| 117 | pub const default_level: Level = switch (builtin.mode) { | 104 | pub const default_level: Level = switch (builtin.mode) { |
| 118 | .Debug => .debug, | 105 | .Debug => .debug, |
| 119 | .ReleaseSafe => .notice, | 106 | .ReleaseSafe => .info, |
| 120 | .ReleaseFast => .err, | 107 | .ReleaseFast, .ReleaseSmall => .err, |
| 121 | .ReleaseSmall => .err, | | |
| 122 | }; | 108 | }; |
| 123 | | 109 | |
| 124 | /// The current log level. This is set to root.log_level if present, otherwise | 110 | /// The current log level. This is set to root.log_level if present, otherwise |
| ... | @@ -188,39 +174,18 @@ pub fn defaultLog( | ... | @@ -188,39 +174,18 @@ pub fn defaultLog( |
| 188 | /// provided here. | 174 | /// provided here. |
| 189 | pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { | 175 | pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| 190 | return struct { | 176 | return struct { |
| 191 | /// Log an emergency message. This log level is intended to be used | 177 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 192 | /// for conditions that cannot be handled and is usually followed by a panic. | 178 | pub const emerg = @This().err; |
| 193 | pub fn emerg( | | |
| 194 | comptime format: []const u8, | | |
| 195 | args: anytype, | | |
| 196 | ) void { | | |
| 197 | @setCold(true); | | |
| 198 | log(.emerg, scope, format, args); | | |
| 199 | } | | |
| 200 | | 179 | |
| 201 | /// Log an alert message. This log level is intended to be used for | 180 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 202 | /// conditions that should be corrected immediately (e.g. database corruption). | 181 | pub const alert = @This().err; |
| 203 | pub fn alert( | | |
| 204 | comptime format: []const u8, | | |
| 205 | args: anytype, | | |
| 206 | ) void { | | |
| 207 | @setCold(true); | | |
| 208 | log(.alert, scope, format, args); | | |
| 209 | } | | |
| 210 | | 182 | |
| 211 | /// Log a critical message. This log level is intended to be used | 183 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 212 | /// when a bug has been detected or something has gone wrong and it will have | 184 | pub const crit = @This().err; |
| 213 | /// an effect on the operation of the program. | | |
| 214 | pub fn crit( | | |
| 215 | comptime format: []const u8, | | |
| 216 | args: anytype, | | |
| 217 | ) void { | | |
| 218 | @setCold(true); | | |
| 219 | log(.crit, scope, format, args); | | |
| 220 | } | | |
| 221 | | 185 | |
| 222 | /// Log an error message. This log level is intended to be used when | 186 | /// Log an error message. This log level is intended to be used |
| 223 | /// a bug has been detected or something has gone wrong but it is recoverable. | 187 | /// when something has gone wrong. This might be recoverable or might |
| | 188 | /// be followed by the program exiting. |
| 224 | pub fn err( | 189 | pub fn err( |
| 225 | comptime format: []const u8, | 190 | comptime format: []const u8, |
| 226 | args: anytype, | 191 | args: anytype, |
| ... | @@ -239,14 +204,8 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { | ... | @@ -239,14 +204,8 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| 239 | log(.warn, scope, format, args); | 204 | log(.warn, scope, format, args); |
| 240 | } | 205 | } |
| 241 | | 206 | |
| 242 | /// Log a notice message. This log level is intended to be used for | 207 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 243 | /// non-error but significant conditions. | 208 | pub const notice = @This().info; |
| 244 | pub fn notice( | | |
| 245 | comptime format: []const u8, | | |
| 246 | args: anytype, | | |
| 247 | ) void { | | |
| 248 | log(.notice, scope, format, args); | | |
| 249 | } | | |
| 250 | | 209 | |
| 251 | /// Log an info message. This log level is intended to be used for | 210 | /// Log an info message. This log level is intended to be used for |
| 252 | /// general messages about the state of the program. | 211 | /// general messages about the state of the program. |
| ... | @@ -271,24 +230,18 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { | ... | @@ -271,24 +230,18 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| 271 | /// The default scoped logging namespace. | 230 | /// The default scoped logging namespace. |
| 272 | pub const default = scoped(.default); | 231 | pub const default = scoped(.default); |
| 273 | | 232 | |
| 274 | /// Log an emergency message using the default scope. This log level is | 233 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 275 | /// intended to be used for conditions that cannot be handled and is usually | 234 | pub const emerg = default.err; |
| 276 | /// followed by a panic. | | |
| 277 | pub const emerg = default.emerg; | | |
| 278 | | 235 | |
| 279 | /// Log an alert message using the default scope. This log level is intended to | 236 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 280 | /// be used for conditions that should be corrected immediately (e.g. database | 237 | pub const alert = default.err; |
| 281 | /// corruption). | | |
| 282 | pub const alert = default.alert; | | |
| 283 | | 238 | |
| 284 | /// Log a critical message using the default scope. This log level is intended | 239 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 285 | /// to be used when a bug has been detected or something has gone wrong and it | 240 | pub const crit = default.err; |
| 286 | /// will have an effect on the operation of the program. | | |
| 287 | pub const crit = default.crit; | | |
| 288 | | 241 | |
| 289 | /// Log an error message using the default scope. This log level is intended to | 242 | /// Log an error message using the default scope. This log level is intended to |
| 290 | /// be used when a bug has been detected or something has gone wrong but it is | 243 | /// be used when something has gone wrong. This might be recoverable or might |
| 291 | /// recoverable. | 244 | /// be followed by the program exiting. |
| 292 | pub const err = default.err; | 245 | pub const err = default.err; |
| 293 | | 246 | |
| 294 | /// Log a warning message using the default scope. This log level is intended | 247 | /// Log a warning message using the default scope. This log level is intended |
| ... | @@ -296,9 +249,8 @@ pub const err = default.err; | ... | @@ -296,9 +249,8 @@ pub const err = default.err; |
| 296 | /// the circumstances would be worth investigating. | 249 | /// the circumstances would be worth investigating. |
| 297 | pub const warn = default.warn; | 250 | pub const warn = default.warn; |
| 298 | | 251 | |
| 299 | /// Log a notice message using the default scope. This log level is intended to | 252 | /// Deprecated. TODO: replace with @compileError() after 0.9.0 is released |
| 300 | /// be used for non-error but significant conditions. | 253 | pub const notice = default.info; |
| 301 | pub const notice = default.notice; | | |
| 302 | | 254 | |
| 303 | /// Log an info message using the default scope. This log level is intended to | 255 | /// Log an info message using the default scope. This log level is intended to |
| 304 | /// be used for general messages about the state of the program. | 256 | /// be used for general messages about the state of the program. |