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
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m27s
This commit is contained in:
30
README.md
30
README.md
@@ -66,24 +66,24 @@ pub fn main() void {
|
|||||||
This will result in the following output:
|
This will result in the following output:
|
||||||
|
|
||||||
```
|
```
|
||||||
[2024-08-28 22:22] debug(main): Debug message 42
|
[[1m2025-02-24 13:00:08[0m] [[38;5;12mdebug[0m]([2mmain[0m): Debug message 42
|
||||||
[2024-08-28 22:22] info(main): Info message { 1, 2, 3, 4 }
|
[[1m2025-02-24 13:00:08[0m] [[38;5;10minfo[0m]([2mmain[0m): Info message { 1, 2, 3, 4 }
|
||||||
[2024-08-28 22:22] info(main): Info message "This is a test"
|
[[1m2025-02-24 13:00:08[0m] [[38;5;10minfo[0m]([2mmain[0m): Info message "This is a test"
|
||||||
[2024-08-28 22:22] warning(main): Warning message main.Options = enum {
|
[[1m2025-02-24 13:00:08[0m] [[38;5;11mwarning[0m]([2mmain[0m): Warning message main.Options = enum {
|
||||||
|
a,
|
||||||
|
b,
|
||||||
|
c,
|
||||||
|
} = a
|
||||||
|
[[1m2025-02-24 13:00:08[0m] [[38;5;9merror[0m]([2mmain[0m): Error message main.Struct = struct {
|
||||||
|
.a = 42,
|
||||||
|
.b = true,
|
||||||
|
.c = []u16: { 1, 2, 3, 4, 5 },
|
||||||
|
.d = { 115, 116, 114, 105, 110, 103 },
|
||||||
|
.e = main.Options = enum {
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
c,
|
c,
|
||||||
} = a
|
} = b,
|
||||||
[2024-08-28 22:22] error(main): Error message main.Struct = struct {
|
|
||||||
.a = 42,
|
|
||||||
.b = true,
|
|
||||||
.c = []u16: { 1, 2, 3, 4, 5 },
|
|
||||||
.d = { 115, 116, 114, 105, 110, 103 },
|
|
||||||
.e = main.Options = enum {
|
|
||||||
a,
|
|
||||||
b,
|
|
||||||
c,
|
|
||||||
} = b,
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
13
src/zlog.zig
13
src/zlog.zig
@@ -16,8 +16,15 @@ fn logFn(
|
|||||||
// TODO: provide build time configuration to allow tweaking corresponding output
|
// TODO: provide build time configuration to allow tweaking corresponding output
|
||||||
// - change output file for writing messages to (default `stderr`)
|
// - change output file for writing messages to (default `stderr`)
|
||||||
// write into own file for each level?
|
// 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";
|
const fmt = level_txt ++ prefix ++ format ++ "\n";
|
||||||
if (comptime build_options.file.len != 0) {
|
if (comptime build_options.file.len != 0) {
|
||||||
// TODO: handle errors accordingly (i.e. panic?)
|
// 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 {
|
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 {
|
pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
|
||||||
|
|||||||
Reference in New Issue
Block a user