| ... | ... | @@ -2,12 +2,16 @@ const std = @import("std.zig"); |
| 2 | 2 | const builtin = std.builtin; |
| 3 | 3 | const root = @import("root"); |
| 4 | 4 | |
| 5 | | //! std.log is standardized interface for logging which allows for the logging |
| 5 | //! std.log is a standardized interface for logging which allows for the logging |
| 6 | 6 | //! of programs and libraries using this interface to be formatted and filtered |
| 7 | 7 | //! by the implementer of the root.log function. |
| 8 | 8 | //! |
| 9 | 9 | //! The scope parameter should be used to give context to the logging. For |
| 10 | 10 | //! example, a library called 'libfoo' might use .libfoo as its scope. |
| 11 | //! This parameter can either be passed explicitly to the logging functions |
| 12 | //! provided here, or a scoped logging namespace can be created |
| 13 | //! using the `log.scoped` function. If logging scopes are not relevant for |
| 14 | //! your use case, the `log.default` scope namespace can be used. |
| 11 | 15 | //! |
| 12 | 16 | //! An example root.log might look something like this: |
| 13 | 17 | //! |
| ... | ... | @@ -44,16 +48,26 @@ const root = @import("root"); |
| 44 | 48 | //! } |
| 45 | 49 | //! |
| 46 | 50 | //! pub fn main() void { |
| 51 | //! // Using explicit scopes: |
| 47 | 52 | //! // Won't be printed as log_level is .warn |
| 48 | 53 | //! std.log.info(.my_project, "Starting up.", .{}); |
| 49 | 54 | //! std.log.err(.nice_library, "Something went very wrong, sorry.", .{}); |
| 50 | 55 | //! // Won't be printed as it gets filtered out by our log function |
| 51 | 56 | //! std.log.err(.lib_that_logs_too_much, "Added 1 + 1", .{}); |
| 57 | //! |
| 58 | //! // Using a scoped logging namespace: |
| 59 | //! const scoped_log = std.log.scoped(.my_project); |
| 60 | //! scoped_log.alert("The scope for this message is implicitly .my_project", .{}); |
| 61 | //! |
| 62 | //! // Using the default namespace: |
| 63 | //! // Won't be printed as log_level is .warn |
| 64 | //! std.log.default.info("I don't care about my namespace", .{}); |
| 52 | 65 | //! } |
| 53 | 66 | //! ``` |
| 54 | 67 | //! Which produces the following output: |
| 55 | 68 | //! ``` |
| 56 | 69 | //! [err] (nice_library): Something went very wrong, sorry. |
| 70 | //! [alert] (my_project): The scope for this message is implicitly .my_project |
| 57 | 71 | //! ``` |
| 58 | 72 | |
| 59 | 73 | pub const Level = enum { |
| ... | ... | @@ -201,6 +215,8 @@ pub fn debug( |
| 201 | 215 | log(.debug, scope, format, args); |
| 202 | 216 | } |
| 203 | 217 | |
| 218 | /// Returns a scoped logging namespace that logs all messages using the scope |
| 219 | /// provided here. |
| 204 | 220 | pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| 205 | 221 | return struct { |
| 206 | 222 | /// Log an emergency message to stderr. This log level is intended to be used |
| ... | ... | @@ -283,4 +299,5 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| 283 | 299 | }; |
| 284 | 300 | } |
| 285 | 301 | |
| 302 | /// The default scoped logging namespace. |
| 286 | 303 | pub const default = scoped(.default); |