| 1 | const std = @import("std"); |
| 2 | const root = @import("root"); |
| 3 | const extras = @import("extras"); |
| 4 | const nfs = @import("nfs"); |
| 5 | const backend: Backend = extras.globalOption("tracer_backend", Backend) orelse .none; |
| 6 | const impl = switch (backend) { |
| 7 | .none => none, |
| 8 | .log => log, |
| 9 | .chrome => chrome, |
| 10 | .spall => spall, |
| 11 | .otel => otel, |
| 12 | }; |
| 13 | |
| 14 | pub const none = @import("./none.zig"); |
| 15 | pub const log = @import("./log.zig"); |
| 16 | pub const chrome = @import("./chrome.zig"); |
| 17 | pub const spall = @import("./spall.zig"); |
| 18 | pub const otel = @import("./otel.zig"); |
| 19 | |
| 20 | pub const Backend = enum { |
| 21 | none, |
| 22 | log, |
| 23 | chrome, |
| 24 | spall, |
| 25 | otel, |
| 26 | }; |
| 27 | |
| 28 | threadlocal var started = false; |
| 29 | |
| 30 | pub fn init(args: @typeInfo(@TypeOf(impl.init)).@"fn".params[0].type.?) !void { |
| 31 | try impl.init(args); |
| 32 | } |
| 33 | |
| 34 | pub fn deinit() void { |
| 35 | impl.deinit(); |
| 36 | } |
| 37 | |
| 38 | pub fn init_thread(args: @typeInfo(@TypeOf(impl.init_thread)).@"fn".params[0].type.?) !void { |
| 39 | try impl.init_thread(args); |
| 40 | started = true; |
| 41 | } |
| 42 | |
| 43 | pub fn deinit_thread() void { |
| 44 | impl.deinit_thread(); |
| 45 | started = false; |
| 46 | } |
| 47 | |
| 48 | pub fn trace(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype) Ctx { |
| 49 | return .{ |
| 50 | .src = src, |
| 51 | .data = if (!started) undefined else impl.trace_begin(src, fmt, args), |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | pub const Ctx = struct { |
| 56 | src: std.builtin.SourceLocation, |
| 57 | data: impl.Data, |
| 58 | |
| 59 | pub fn end(self: Ctx) void { |
| 60 | if (!started) return; |
| 61 | impl.trace_end(self); |
| 62 | } |
| 63 | }; |