authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-12 18:26:35-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-08-12 18:26:35-04:00
log30db5b1fb23502de02650473ff8282b2875650a9
treeed21b8fb7fafaecf3cbd7db4ca91cda20dc0cb5d
parent67d684d89aeb0bb3cfa86c57e8d77359d45743fa
parentbf2ed0f571736241151a358b2533d45cb769db68
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6039 from heidezomp/std-log-scoped

std.log: add scoped logging struct

1 files changed, 110 insertions(+), 9 deletions(-)

lib/std/log.zig+110-9
...@@ -2,12 +2,16 @@ const std = @import("std.zig");...@@ -2,12 +2,16 @@ const std = @import("std.zig");
2const builtin = std.builtin;2const builtin = std.builtin;
3const root = @import("root");3const root = @import("root");
44
5//! std.log is standardized interface for logging which allows for the logging5//! 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 filtered6//! 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. For9//! 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 .warn52//! // 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 function55//! // 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//! ```
5872
59pub const Level = enum {73pub const Level = enum {
...@@ -115,7 +129,7 @@ fn log(...@@ -115,7 +129,7 @@ fn log(
115 }129 }
116}130}
117131
118/// Log an emergency message to stderr. This log level is intended to be used132/// 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.
120pub fn emerg(134pub 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}
128142
129/// Log an alert message to stderr. This log level is intended to be used for143/// 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).
131pub fn alert(145pub 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}
139153
140/// Log a critical message to stderr. This log level is intended to be used154/// 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 have155/// 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.
143pub fn crit(157pub 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}
151165
152/// Log an error message to stderr. This log level is intended to be used when166/// 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.
154pub fn err(168pub 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}
162176
163/// Log a warning message to stderr. This log level is intended to be used if177/// 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 the178/// it is uncertain whether something has gone wrong or not, but the
165/// circumstances would be worth investigating.179/// circumstances would be worth investigating.
166pub fn warn(180pub 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}
173187
174/// Log a notice message to stderr. This log level is intended to be used for188/// 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.
176pub fn notice(190pub 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}
183197
184/// Log an info message to stderr. This log level is intended to be used for198/// 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.
186pub fn info(200pub 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}
193207
194/// Log a debug message to stderr. This log level is intended to be used for208/// 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.
196pub fn debug(210pub 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.
220pub 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.
303pub const default = scoped(.default);