From b03c770b59a6a06862f5911ec7bdf6317a51faf0 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Thu, 29 Aug 2024 14:59:45 +0200 Subject: [PATCH] doc: add tips section in README --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index d9b0a8d..ffc7fa3 100644 --- a/README.md +++ b/README.md @@ -89,3 +89,19 @@ This will result in the following output: For more details about the output customization see the configuration options of the `zlog` module. Following options are available: - _timestamp_ (default: `true`): Prepend the current timestamp before each log message. + +## Tips + +The following list shows some tips on how to use logging more effectively. These tips do not apply just to `zlog` (and not even only to zig code). + +- Use `errdefer` to directly print messages on failures in the same function they occur: + ```zig + // assume log is already defined before (with the corresponding scope) + const port = port: { + errdefer |err| log.err("failed to read the port number: {}", .{err}); + + var buf: [fmt.count("{}\n", .{maxInt(u16)})]u8 = undefined; + const len = try process.stdout.?.readAll(&buf); + break :port try fmt.parseInt(u16, buf[0 .. len -| 1], 10); + }; + ```