| ... | @@ -2,12 +2,16 @@ const std = @import("std.zig"); | ... | @@ -2,12 +2,16 @@ const std = @import("std.zig"); |
| 2 | const builtin = std.builtin; | 2 | const builtin = std.builtin; |
| 3 | const root = @import("root"); | 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 | //! of programs and libraries using this interface to be formatted and filtered | 6 | //! of programs and libraries using this interface to be formatted and filtered |
| 7 | //! by the implementer of the root.log function. | 7 | //! by the implementer of the root.log function. |
| 8 | //! | 8 | //! |
| 9 | //! The scope parameter should be used to give context to the logging. For | 9 | //! The scope parameter should be used to give context to the logging. For |
| 10 | //! example, a library called 'libfoo' might use .libfoo as its scope. | 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 | //! An example root.log might look something like this: | 16 | //! An example root.log might look something like this: |
| 13 | //! | 17 | //! |
| ... | @@ -44,16 +48,26 @@ const root = @import("root"); | ... | @@ -44,16 +48,26 @@ const root = @import("root"); |
| 44 | //! } | 48 | //! } |
| 45 | //! | 49 | //! |
| 46 | //! pub fn main() void { | 50 | //! pub fn main() void { |
| | 51 | //! // Using explicit scopes: |
| 47 | //! // Won't be printed as log_level is .warn | 52 | //! // Won't be printed as log_level is .warn |
| 48 | //! std.log.info(.my_project, "Starting up.", .{}); | 53 | //! std.log.info(.my_project, "Starting up.", .{}); |
| 49 | //! std.log.err(.nice_library, "Something went very wrong, sorry.", .{}); | 54 | //! std.log.err(.nice_library, "Something went very wrong, sorry.", .{}); |
| 50 | //! // Won't be printed as it gets filtered out by our log function | 55 | //! // Won't be printed as it gets filtered out by our log function |
| 51 | //! std.log.err(.lib_that_logs_too_much, "Added 1 + 1", .{}); | 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 | //! Which produces the following output: | 67 | //! Which produces the following output: |
| 55 | //! ``` | 68 | //! ``` |
| 56 | //! [err] (nice_library): Something went very wrong, sorry. | 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 | pub const Level = enum { | 73 | pub const Level = enum { |
| ... | @@ -115,7 +129,7 @@ fn log( | ... | @@ -115,7 +129,7 @@ fn log( |
| 115 | } | 129 | } |
| 116 | } | 130 | } |
| 117 | | 131 | |
| 118 | /// Log an emergency message to stderr. This log level is intended to be used | 132 | /// Log an emergency message. This log level is intended to be used |
| 119 | /// for conditions that cannot be handled and is usually followed by a panic. | 133 | /// for conditions that cannot be handled and is usually followed by a panic. |
| 120 | pub fn emerg( | 134 | pub fn emerg( |
| 121 | comptime scope: @Type(.EnumLiteral), | 135 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -126,7 +140,7 @@ pub fn emerg( | ... | @@ -126,7 +140,7 @@ pub fn emerg( |
| 126 | log(.emerg, scope, format, args); | 140 | log(.emerg, scope, format, args); |
| 127 | } | 141 | } |
| 128 | | 142 | |
| 129 | /// Log an alert message to stderr. This log level is intended to be used for | 143 | /// Log an alert message. This log level is intended to be used for |
| 130 | /// conditions that should be corrected immediately (e.g. database corruption). | 144 | /// conditions that should be corrected immediately (e.g. database corruption). |
| 131 | pub fn alert( | 145 | pub fn alert( |
| 132 | comptime scope: @Type(.EnumLiteral), | 146 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -137,7 +151,7 @@ pub fn alert( | ... | @@ -137,7 +151,7 @@ pub fn alert( |
| 137 | log(.alert, scope, format, args); | 151 | log(.alert, scope, format, args); |
| 138 | } | 152 | } |
| 139 | | 153 | |
| 140 | /// Log a critical message to stderr. This log level is intended to be used | 154 | /// Log a critical message. This log level is intended to be used |
| 141 | /// when a bug has been detected or something has gone wrong and it will have | 155 | /// when a bug has been detected or something has gone wrong and it will have |
| 142 | /// an effect on the operation of the program. | 156 | /// an effect on the operation of the program. |
| 143 | pub fn crit( | 157 | pub fn crit( |
| ... | @@ -149,7 +163,7 @@ pub fn crit( | ... | @@ -149,7 +163,7 @@ pub fn crit( |
| 149 | log(.crit, scope, format, args); | 163 | log(.crit, scope, format, args); |
| 150 | } | 164 | } |
| 151 | | 165 | |
| 152 | /// Log an error message to stderr. This log level is intended to be used when | 166 | /// Log an error message. This log level is intended to be used when |
| 153 | /// a bug has been detected or something has gone wrong but it is recoverable. | 167 | /// a bug has been detected or something has gone wrong but it is recoverable. |
| 154 | pub fn err( | 168 | pub fn err( |
| 155 | comptime scope: @Type(.EnumLiteral), | 169 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -160,7 +174,7 @@ pub fn err( | ... | @@ -160,7 +174,7 @@ pub fn err( |
| 160 | log(.err, scope, format, args); | 174 | log(.err, scope, format, args); |
| 161 | } | 175 | } |
| 162 | | 176 | |
| 163 | /// Log a warning message to stderr. This log level is intended to be used if | 177 | /// Log a warning message. This log level is intended to be used if |
| 164 | /// it is uncertain whether something has gone wrong or not, but the | 178 | /// it is uncertain whether something has gone wrong or not, but the |
| 165 | /// circumstances would be worth investigating. | 179 | /// circumstances would be worth investigating. |
| 166 | pub fn warn( | 180 | pub fn warn( |
| ... | @@ -171,7 +185,7 @@ pub fn warn( | ... | @@ -171,7 +185,7 @@ pub fn warn( |
| 171 | log(.warn, scope, format, args); | 185 | log(.warn, scope, format, args); |
| 172 | } | 186 | } |
| 173 | | 187 | |
| 174 | /// Log a notice message to stderr. This log level is intended to be used for | 188 | /// Log a notice message. This log level is intended to be used for |
| 175 | /// non-error but significant conditions. | 189 | /// non-error but significant conditions. |
| 176 | pub fn notice( | 190 | pub fn notice( |
| 177 | comptime scope: @Type(.EnumLiteral), | 191 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -181,7 +195,7 @@ pub fn notice( | ... | @@ -181,7 +195,7 @@ pub fn notice( |
| 181 | log(.notice, scope, format, args); | 195 | log(.notice, scope, format, args); |
| 182 | } | 196 | } |
| 183 | | 197 | |
| 184 | /// Log an info message to stderr. This log level is intended to be used for | 198 | /// Log an info message. This log level is intended to be used for |
| 185 | /// general messages about the state of the program. | 199 | /// general messages about the state of the program. |
| 186 | pub fn info( | 200 | pub fn info( |
| 187 | comptime scope: @Type(.EnumLiteral), | 201 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -191,7 +205,7 @@ pub fn info( | ... | @@ -191,7 +205,7 @@ pub fn info( |
| 191 | log(.info, scope, format, args); | 205 | log(.info, scope, format, args); |
| 192 | } | 206 | } |
| 193 | | 207 | |
| 194 | /// Log a debug message to stderr. This log level is intended to be used for | 208 | /// Log a debug message. This log level is intended to be used for |
| 195 | /// messages which are only useful for debugging. | 209 | /// messages which are only useful for debugging. |
| 196 | pub fn debug( | 210 | pub fn debug( |
| 197 | comptime scope: @Type(.EnumLiteral), | 211 | comptime scope: @Type(.EnumLiteral), |
| ... | @@ -200,3 +214,90 @@ pub fn debug( | ... | @@ -200,3 +214,90 @@ pub fn debug( |
| 200 | ) void { | 214 | ) void { |
| 201 | log(.debug, scope, format, args); | 215 | log(.debug, scope, format, args); |
| 202 | } | 216 | } |
| | 217 | |
| | 218 | /// Returns a scoped logging namespace that logs all messages using the scope |
| | 219 | /// provided here. |
| | 220 | pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { |
| | 221 | return struct { |
| | 222 | /// Log an emergency message. This log level is intended to be used |
| | 223 | /// for conditions that cannot be handled and is usually followed by a panic. |
| | 224 | pub fn emerg( |
| | 225 | comptime format: []const u8, |
| | 226 | args: anytype, |
| | 227 | ) void { |
| | 228 | @setCold(true); |
| | 229 | log(.emerg, scope, format, args); |
| | 230 | } |
| | 231 | |
| | 232 | /// Log an alert message. This log level is intended to be used for |
| | 233 | /// conditions that should be corrected immediately (e.g. database corruption). |
| | 234 | pub fn alert( |
| | 235 | comptime format: []const u8, |
| | 236 | args: anytype, |
| | 237 | ) void { |
| | 238 | @setCold(true); |
| | 239 | log(.alert, scope, format, args); |
| | 240 | } |
| | 241 | |
| | 242 | /// Log a critical message. This log level is intended to be used |
| | 243 | /// when a bug has been detected or something has gone wrong and it will have |
| | 244 | /// an effect on the operation of the program. |
| | 245 | pub fn crit( |
| | 246 | comptime format: []const u8, |
| | 247 | args: anytype, |
| | 248 | ) void { |
| | 249 | @setCold(true); |
| | 250 | log(.crit, scope, format, args); |
| | 251 | } |
| | 252 | |
| | 253 | /// Log an error message. This log level is intended to be used when |
| | 254 | /// a bug has been detected or something has gone wrong but it is recoverable. |
| | 255 | pub fn err( |
| | 256 | comptime format: []const u8, |
| | 257 | args: anytype, |
| | 258 | ) void { |
| | 259 | @setCold(true); |
| | 260 | log(.err, scope, format, args); |
| | 261 | } |
| | 262 | |
| | 263 | /// Log a warning message. This log level is intended to be used if |
| | 264 | /// it is uncertain whether something has gone wrong or not, but the |
| | 265 | /// circumstances would be worth investigating. |
| | 266 | pub fn warn( |
| | 267 | comptime format: []const u8, |
| | 268 | args: anytype, |
| | 269 | ) void { |
| | 270 | log(.warn, scope, format, args); |
| | 271 | } |
| | 272 | |
| | 273 | /// Log a notice message. This log level is intended to be used for |
| | 274 | /// non-error but significant conditions. |
| | 275 | pub fn notice( |
| | 276 | comptime format: []const u8, |
| | 277 | args: anytype, |
| | 278 | ) void { |
| | 279 | log(.notice, scope, format, args); |
| | 280 | } |
| | 281 | |
| | 282 | /// Log an info message. This log level is intended to be used for |
| | 283 | /// general messages about the state of the program. |
| | 284 | pub fn info( |
| | 285 | comptime format: []const u8, |
| | 286 | args: anytype, |
| | 287 | ) void { |
| | 288 | log(.info, scope, format, args); |
| | 289 | } |
| | 290 | |
| | 291 | /// Log a debug message. This log level is intended to be used for |
| | 292 | /// messages which are only useful for debugging. |
| | 293 | pub fn debug( |
| | 294 | comptime format: []const u8, |
| | 295 | args: anytype, |
| | 296 | ) void { |
| | 297 | log(.debug, scope, format, args); |
| | 298 | } |
| | 299 | }; |
| | 300 | } |
| | 301 | |
| | 302 | /// The default scoped logging namespace. |
| | 303 | pub const default = scoped(.default); |