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");
22const builtin = std.builtin;
33const root = @import("root");
44
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
66//! of programs and libraries using this interface to be formatted and filtered
77//! by the implementer of the root.log function.
88//!
99//! The scope parameter should be used to give context to the logging. For
1010//! 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.
1115//!
1216//! An example root.log might look something like this:
1317//!
......@@ -44,16 +48,26 @@ const root = @import("root");
4448//! }
4549//!
4650//! pub fn main() void {
51//! // Using explicit scopes:
4752//! // Won't be printed as log_level is .warn
4853//! std.log.info(.my_project, "Starting up.", .{});
4954//! std.log.err(.nice_library, "Something went very wrong, sorry.", .{});
5055//! // Won't be printed as it gets filtered out by our log function
5156//! 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", .{});
5265//! }
5366//! ```
5467//! Which produces the following output:
5568//! ```
5669//! [err] (nice_library): Something went very wrong, sorry.
70//! [alert] (my_project): The scope for this message is implicitly .my_project
5771//! ```
5872
5973pub const Level = enum {
......@@ -115,7 +129,7 @@ fn log(
115129 }
116130}
117131
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
119133/// for conditions that cannot be handled and is usually followed by a panic.
120134pub fn emerg(
121135 comptime scope: @Type(.EnumLiteral),
......@@ -126,7 +140,7 @@ pub fn emerg(
126140 log(.emerg, scope, format, args);
127141}
128142
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
130144/// conditions that should be corrected immediately (e.g. database corruption).
131145pub fn alert(
132146 comptime scope: @Type(.EnumLiteral),
......@@ -137,7 +151,7 @@ pub fn alert(
137151 log(.alert, scope, format, args);
138152}
139153
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
141155/// when a bug has been detected or something has gone wrong and it will have
142156/// an effect on the operation of the program.
143157pub fn crit(
......@@ -149,7 +163,7 @@ pub fn crit(
149163 log(.crit, scope, format, args);
150164}
151165
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
153167/// a bug has been detected or something has gone wrong but it is recoverable.
154168pub fn err(
155169 comptime scope: @Type(.EnumLiteral),
......@@ -160,7 +174,7 @@ pub fn err(
160174 log(.err, scope, format, args);
161175}
162176
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
164178/// it is uncertain whether something has gone wrong or not, but the
165179/// circumstances would be worth investigating.
166180pub fn warn(
......@@ -171,7 +185,7 @@ pub fn warn(
171185 log(.warn, scope, format, args);
172186}
173187
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
175189/// non-error but significant conditions.
176190pub fn notice(
177191 comptime scope: @Type(.EnumLiteral),
......@@ -181,7 +195,7 @@ pub fn notice(
181195 log(.notice, scope, format, args);
182196}
183197
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
185199/// general messages about the state of the program.
186200pub fn info(
187201 comptime scope: @Type(.EnumLiteral),
......@@ -191,7 +205,7 @@ pub fn info(
191205 log(.info, scope, format, args);
192206}
193207
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
195209/// messages which are only useful for debugging.
196210pub fn debug(
197211 comptime scope: @Type(.EnumLiteral),
......@@ -200,3 +214,90 @@ pub fn debug(
200214) void {
201215 log(.debug, scope, format, args);
202216}
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);