doc: add tips section in README

This commit is contained in:
2024-08-29 14:59:45 +02:00
parent eb7e78697c
commit b03c770b59

View File

@@ -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);
};
```