authorgravatar for heidezomp@protonmail.comheidezomp <heidezomp@protonmail.com> 2020-08-13 17:12:16+02:00
committergravatar for heidezomp@protonmail.comheidezomp <heidezomp@protonmail.com> 2020-08-13 17:12:16+02:00
log2439f67061b007197bdd1b0037ffe4ba7b520024
tree1c3f49aa7fcdeb7c2e05ca38b6adf4ff1008045f
parenta8e0f667c64bc6413a2c10ac76cd4e3000ceaf1b

std.log: update documentation and example for scoped logging


2 files changed, 25 insertions(+), 23 deletions(-)

doc/langref.html.in+1-1
......@@ -325,7 +325,7 @@ pub fn main() !void {
325325 represents writing data to a file. When the disk is full, a write to the file will fail.
326326 However, we typically do not expect writing text to the standard output to fail. To avoid having
327327 to handle the failure case of printing to standard output, you can use alternate functions: the
328 <code>std.log</code> function for proper logging or the <code>std.debug.print</code> function.
328 functions in <code>std.log</code> for proper logging or the <code>std.debug.print</code> function.
329329 This documentation will use the latter option to print to standard error (stderr) and silently return
330330 on failure. The next code sample, <code>hello_again.zig</code> demonstrates the use of
331331 <code>std.debug.print</code>.
lib/std/log.zig+24-22
......@@ -6,12 +6,16 @@ const root = @import("root");
66//! of programs and libraries using this interface to be formatted and filtered
77//! by the implementer of the root.log function.
88//!
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.
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.
9//! Each log message has an associated scope enum, which can be used to give
10//! context to the logging. The logging functions in std.log implicitly use a
11//! scope of .default.
12//!
13//! A logging namespace using a custom scope can be created using the
14//! std.log.scoped function, passing the scope as an argument; the logging
15//! functions in the resulting struct use the provided scope parameter.
16//! For example, a library called 'libfoo' might use
17//! `const log = std.log.scoped(.libfoo);` to use .libfoo as the scope of its
18//! log messages.
1519//!
1620//! An example root.log might look something like this:
1721//!
......@@ -29,9 +33,9 @@ const root = @import("root");
2933//! args: anytype,
3034//! ) void {
3135//! // Ignore all non-critical logging from sources other than
32//! // .my_project and .nice_library
36//! // .my_project, .nice_library and .default
3337//! const scope_prefix = "(" ++ switch (scope) {
34//! .my_project, .nice_library => @tagName(scope),
38//! .my_project, .nice_library, .default => @tagName(scope),
3539//! else => if (@enumToInt(level) <= @enumToInt(std.log.Level.crit))
3640//! @tagName(scope)
3741//! else
......@@ -48,26 +52,24 @@ const root = @import("root");
4852//! }
4953//!
5054//! pub fn main() void {
51//! // Using explicit scopes:
52//! // Won't be printed as log_level is .warn
53//! std.log.info(.my_project, "Starting up.", .{});
54//! std.log.err(.nice_library, "Something went very wrong, sorry.", .{});
55//! // Won't be printed as it gets filtered out by our log function
56//! std.log.err(.lib_that_logs_too_much, "Added 1 + 1", .{});
55//! // Using the default scope:
56//! std.log.info("Just a simple informational log message", .{}); // Won't be printed as log_level is .warn
57//! std.log.warn("Flux capacitor is starting to overheat", .{});
5758//!
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", .{});
59//! // Using scoped logging:
60//! const my_project_log = std.log.scoped(.my_project);
61//! const nice_library_log = std.log.scoped(.nice_library);
62//! const verbose_lib_log = std.log.scoped(.verbose_lib);
6163//!
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", .{});
64//! my_project_log.info("Starting up", .{}); // Won't be printed as log_level is .warn
65//! nice_library_log.err("Something went very wrong, sorry", .{});
66//! verbose_lib_log.err("Added 1 + 1: {}", .{1 + 1}); // Won't be printed as it gets filtered out by our log function
6567//! }
6668//! ```
6769//! Which produces the following output:
6870//! ```
69//! [err] (nice_library): Something went very wrong, sorry.
70//! [alert] (my_project): The scope for this message is implicitly .my_project
71//! [warn] (default): Flux capacitor is starting to overheat
72//! [err] (nice_library): Something went very wrong, sorry
7173//! ```
7274
7375pub const Level = enum {