feat(zlog): colored level text and styled timestamps and scopes
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m27s

This commit is contained in:
2025-02-24 14:01:49 +01:00
parent dd98e63bcb
commit a21d84cfcf
2 changed files with 25 additions and 18 deletions

View File

@@ -66,15 +66,15 @@ pub fn main() void {
This will result in the following output:
```
[2024-08-28 22:22] debug(main): Debug message 42
[2024-08-28 22:22] info(main): Info message { 1, 2, 3, 4 }
[2024-08-28 22:22] info(main): Info message "This is a test"
[2024-08-28 22:22] warning(main): Warning message main.Options = enum {
[2025-02-24 13:00:08] [debug](main): Debug message 42
[2025-02-24 13:00:08] [info](main): Info message { 1, 2, 3, 4 }
[2025-02-24 13:00:08] [info](main): Info message "This is a test"
[2025-02-24 13:00:08] [warning](main): Warning message main.Options = enum {
a,
b,
c,
} = a
[2024-08-28 22:22] error(main): Error message main.Struct = struct {
[2025-02-24 13:00:08] [error](main): Error message main.Struct = struct {
.a = 42,
.b = true,
.c = []u16: { 1, 2, 3, 4, 5 },

View File

@@ -16,8 +16,15 @@ fn logFn(
// TODO: provide build time configuration to allow tweaking corresponding output
// - change output file for writing messages to (default `stderr`)
// write into own file for each level?
const level_txt = comptime message_level.asText();
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
// TODO: make the level text colored according to the level!
const prefix = if (scope == .default) ": " else "(\x1b[2m" ++ @tagName(scope) ++ "\x1b[0m): ";
const level_txt = switch (comptime message_level) {
.err => "[\x1b[38;5;9merror\x1b[0m]",
.warn => "[\x1b[38;5;11mwarning\x1b[0m]",
.info => "[\x1b[38;5;10minfo\x1b[0m]",
.debug => "[\x1b[38;5;12mdebug\x1b[0m]",
};
const fmt = level_txt ++ prefix ++ format ++ "\n";
if (comptime build_options.file.len != 0) {
// TODO: handle errors accordingly (i.e. panic?)
@@ -56,7 +63,7 @@ inline fn log_writing(writer: anytype, comptime fmt: []const u8, args: anytype)
}
inline fn log_timestamp(writer: anytype) void {
writer.print("[{any}] ", .{ztime.DateTime.now()}) catch return;
writer.print("[\x1b[1m{any}\x1b[0m] ", .{ztime.DateTime.now()}) catch return;
}
pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {