Created 3 tracing backends

- Logger Tracing backend: Uses the jetzig logger to put tracing
  messages
- Chrome Tracing Backend: Uses the chrome old json format to be used
  with about:tracing (this will work in chromium based navigators)
- None Tracing Backend: Does nothing =)
This commit is contained in:
rimuspp 2024-05-12 16:03:31 -05:00
parent f42717ee72
commit 32817d1540
No known key found for this signature in database
GPG Key ID: 6DB0BA3ACC67DBC7
3 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,66 @@
pub const std = @import("std");
pub const tracing = @import("../tracing.zig");
pub const ChromeJsonBackend = @This();
pid: std.os.linux.pid_t = undefined,
mutex: std.Thread.Mutex = .{},
buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer) = undefined,
backend_file: std.fs.File = undefined,
timer: std.time.Timer = undefined,
threadlocal var tid: std.os.linux.pid_t = undefined;
inline fn init(backend: *ChromeJsonBackend) void {
backend.pid = std.os.linux.getpid();
var name_buffer: [std.fs.MAX_NAME_BYTES]u8 = undefined;
const file_name = std.fmt.bufPrint(&name_buffer, "trace-{d}.chrome.json", .{backend.pid});
backend.backend_file = std.fs.cwd().createFile(file_name) catch @panic(std.fmt.comptimePrint("Failed to create the tracing file at {s}:{d}:{d}", blk: {
const src = @src();
break :blk .{ src.file, src.line, src.column };
}));
backend.buffered_writer = .{ .unbuffered_writer = backend.backend_file.writer() };
backend.timer = std.time.Timer.start() catch @panic(std.fmt.comptimePrint("Failed to start the timer at {s}:{d}:{d: Verify you computer has support for monotonic timers OR your maching has correct settings (seccomp for example)", blk: {
const src = @src();
break :blk .{ src.file, src.line, src.column };
}));
backend.buffered_writer.writer().writeAll("[\n") catch @panic(std.fmt.comptimePrint("Failed to write to the tracing file at {s}:{d}:{d}", blk: {
const src = @src();
break :blk .{ src.file, src.line, src.column };
}));
}
inline fn initThread(_: *ChromeJsonBackend) void {
tid = std.os.linux.gettid();
}
inline fn denitThread(_: *ChromeJsonBackend) void {}
inline fn deinit(_: *ChromeJsonBackend) void {}
inline fn finish(ctx: tracing.TracingContext) void {
var backend = ctx.ImplInterface.chrome;
backend.mutex.lock();
defer backend.mutex.unlock();
backend.buffered_writer.writer().print(
\\{{"cat":"function", "ph": "E", "pid": {d}, "tid": {d}, "ts": {d}}},
\\
,
.{ backend.pid, tid, backend.timer.elapsed() },
) catch {};
}
inline fn trace(backend: ChromeJsonBackend, context: tracing.TracingContext, comptime formatted_message: []const u8, args: anytype) void {
backend.mutex.lock();
defer backend.mutex.unlock();
var writer = backend.buffered_writer.writer();
writer.print(
\\{{"cat":"function", "name":"{s}:{d}:{d} ({s})
++ formatted_message ++
\\", "ph": "B", "pid": {d}, "tid": {d}, "ts": {d}}},
\\
,
blk: {
const src = context.source;
break :blk .{ src.file, src.line, src.column, src.function };
} ++ args ++
.{
backend.pid,
tid,
backend.timer.elapsed(),
},
) catch {};
}

View File

@ -0,0 +1,21 @@
pub const std = @import("std");
pub const tracing = @import("../tracing.zig");
pub const logger = @import("../loggers.zig");
pub const LoggerBackend = @This();
logger: logger.Logger,
timer: std.time.Timer,
inline fn init(_: *LoggerBackend) void {}
inline fn initThread(_: *LoggerBackend) void {}
inline fn denitThread(_: *LoggerBackend) void {}
inline fn deinit(_: *LoggerBackend) void {}
inline fn finish(_: tracing.TracingContext) void {}
inline fn trace(backend: LoggerBackend, context: tracing.TracingContext, comptime formatted_message: []const u8, args: anytype) void {
backend.logger.logRequest(.TRACE, "{d}ns since last event - {s}:{d}:{d} ({s})" ++ formatted_message, .{
backend.timer.lap(), context.source.file,
context.source.line, context.source.column,
context.source.fn_name,
} ++ args) catch return;
}

View File

@ -0,0 +1,10 @@
pub const tracing = @import("../tracing.zig");
pub const NoneBackend = @This();
inline fn init(_ : *NoneBackend) void {}
inline fn initThread(_ : *NoneBackend) void {}
inline fn denitThread(_ : *NoneBackend) void {}
inline fn deinit(_ : *NoneBackend) void {}
inline fn finish(_ : tracing.TracingContext) void {}
inline fn trace(_: NoneBackend, _: tracing.TracingContext, comptime _: []const u8, _: anytype) void {}