authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-19 18:53:37 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-19 18:53:37 -08:00
log39a43e1bacabb514e6479795899b097022789623
tree747c3286703f62608e206085bf91029c45c7106b
parenta715c0c693801013e99801c92f7500d2fcb97229

allow passing a fmt+args to trace()


5 files changed, 12 insertions(+), 10 deletions(-)

src/log.zig+2-2
...@@ -10,8 +10,8 @@ pub fn init_thread() !void {}...@@ -10,8 +10,8 @@ pub fn init_thread() !void {}
1010
11pub fn deinit_thread() void {}11pub fn deinit_thread() void {}
1212
13pub inline fn trace_begin(ctx: tracer.Ctx) void {13pub inline fn trace_begin(ctx: tracer.Ctx, comptime ifmt: []const u8, iargs: anytype) void {
14 log.debug("{s}:{d}:{d} ({s})", .{ ctx.src.file, ctx.src.line, ctx.src.column, ctx.src.fn_name });14 log.debug("{s}:{d}:{d} ({s})" ++ ifmt, .{ ctx.src.file, ctx.src.line, ctx.src.column, ctx.src.fn_name } ++ iargs);
15}15}
1616
17pub inline fn trace_end(ctx: tracer.Ctx) void {17pub inline fn trace_end(ctx: tracer.Ctx) void {
src/main.zig+1-1
...@@ -24,6 +24,6 @@ pub fn main() !void {...@@ -24,6 +24,6 @@ pub fn main() !void {
24}24}
2525
26fn handler() void {26fn handler() void {
27 const t = tracer.trace(@src());27 const t = tracer.trace(@src(), "", .{});
28 defer t.end();28 defer t.end();
29}29}
src/mod.zig+2-2
...@@ -26,11 +26,11 @@ pub fn deinit_thread() void {...@@ -26,11 +26,11 @@ pub fn deinit_thread() void {
26 impl.deinit_thread();26 impl.deinit_thread();
27}27}
2828
29pub inline fn trace(src: std.builtin.SourceLocation) Ctx {29pub inline fn trace(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype) Ctx {
30 const ctx = Ctx{30 const ctx = Ctx{
31 .src = src,31 .src = src,
32 };32 };
33 if (started) impl.trace_begin(ctx);33 if (started) impl.trace_begin(ctx, fmt, args);
34 return ctx;34 return ctx;
35}35}
3636
src/none.zig+3-1
...@@ -10,8 +10,10 @@ pub fn init_thread() !void {}...@@ -10,8 +10,10 @@ pub fn init_thread() !void {}
1010
11pub fn deinit_thread() void {}11pub fn deinit_thread() void {}
1212
13pub inline fn trace_begin(ctx: tracer.Ctx) void {13pub inline fn trace_begin(ctx: tracer.Ctx, comptime ifmt: []const u8, iargs: anytype) void {
14 _ = ctx;14 _ = ctx;
15 _ = ifmt;
16 _ = iargs;
15}17}
1618
17pub inline fn trace_end(ctx: tracer.Ctx) void {19pub inline fn trace_end(ctx: tracer.Ctx) void {
src/spall.zig+4-4
...@@ -37,8 +37,8 @@ pub fn deinit_thread() void {...@@ -37,8 +37,8 @@ pub fn deinit_thread() void {
37 log.debug("{s}", .{path});37 log.debug("{s}", .{path});
38}38}
3939
40pub inline fn trace_begin(ctx: tracer.Ctx) void {40pub inline fn trace_begin(ctx: tracer.Ctx, comptime ifmt: []const u8, iargs: anytype) void {
41 const fmt = "{s}:{d}:{d} ({s})";41 const fmt = "{s}:{d}:{d} ({s})" ++ ifmt;
42 const args = .{42 const args = .{
43 if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file,43 if (ctx.src.file[0] == '/') ctx.src.file[trim_count..] else ctx.src.file,
44 ctx.src.line,44 ctx.src.line,
...@@ -49,10 +49,10 @@ pub inline fn trace_begin(ctx: tracer.Ctx) void {...@@ -49,10 +49,10 @@ pub inline fn trace_begin(ctx: tracer.Ctx) void {
49 .pid = @intCast(pid),49 .pid = @intCast(pid),
50 .tid = @intCast(tid),50 .tid = @intCast(tid),
51 .time = @floatFromInt(std.time.microTimestamp()),51 .time = @floatFromInt(std.time.microTimestamp()),
52 .name_len = @intCast(std.fmt.count(fmt, args)),52 .name_len = @intCast(std.fmt.count(fmt, args ++ iargs)),
53 .args_len = 0,53 .args_len = 0,
54 }) catch return;54 }) catch return;
55 buffered_writer.writer().print(fmt, args) catch return;55 buffered_writer.writer().print(fmt, args ++ iargs) catch return;
56}56}
5757
58pub inline fn trace_end(ctx: tracer.Ctx) void {58pub inline fn trace_end(ctx: tracer.Ctx) void {