const std = @import("std"); pub const Level = enum(u8) { debug, info, warn, err, pub fn label(self: Level) []const u8 { return switch (self) { .debug => "debug", .info => "info", .warn => "warn", .err => "error", }; } }; pub const Source = enum { host, guest, pub fn label(self: Source) []const u8 { return switch (self) { .host => "host", .guest => "guest", }; } }; pub const Context = struct { component: ?[]const u8 = null, source: ?Source = null, event: ?[]const u8 = null, phase: ?[]const u8 = null, vu_id: ?u32 = null, iteration_no: ?u64 = null, }; pub const Sink = struct { context: ?*anyopaque, write_fn: *const fn (?*anyopaque, []const u8) anyerror!void, flush_fn: ?*const fn (?*anyopaque) anyerror!void = null, pub fn fromWriterPtr(writer_ptr: anytype) Sink { const WriterPtr = @TypeOf(writer_ptr); if (@typeInfo(WriterPtr) != .pointer) { @compileError("Sink.fromWriterPtr expects a writer pointer"); } const WriterType = std.meta.Child(WriterPtr); const Adapter = struct { fn write(context: ?*anyopaque, bytes: []const u8) anyerror!void { const writer: WriterPtr = @ptrCast(@alignCast(context.?)); try writer.writeAll(bytes); } fn flush(context: ?*anyopaque) anyerror!void { const writer: WriterPtr = @ptrCast(@alignCast(context.?)); if (@hasDecl(WriterType, "flush")) { try writer.flush(); } } }; return .{ .context = @ptrCast(writer_ptr), .write_fn = Adapter.write, .flush_fn = Adapter.flush, }; } pub fn write(self: Sink, bytes: []const u8) !void { try self.write_fn(self.context, bytes); } pub fn flush(self: Sink) !void { if (self.flush_fn) |flush_fn| { try flush_fn(self.context); } } }; pub fn WriterHolder(comptime WriterType: type) type { return struct { writer: WriterType, pub fn init(writer: WriterType) @This() { return .{ .writer = writer }; } pub fn sink(self: *@This()) Sink { return if (@typeInfo(WriterType) == .pointer) Sink.fromWriterPtr(self.writer) else Sink.fromWriterPtr(&self.writer); } }; } pub const Logger = struct { allocator: std.mem.Allocator, sink: Sink, minimum_level: Level = .debug, mutex: std.Thread.Mutex = .{}, const LogEntry = struct { ts_unix_nanos: u64, level: []const u8, message: []const u8, component: ?[]const u8 = null, source: ?[]const u8 = null, event: ?[]const u8 = null, phase: ?[]const u8 = null, vu_id: ?u32 = null, iteration_no: ?u64 = null, }; pub fn init(allocator: std.mem.Allocator, sink: Sink) Logger { return .{ .allocator = allocator, .sink = sink, }; } pub fn initWithLevel(allocator: std.mem.Allocator, sink: Sink, minimum_level: Level) Logger { var logger = init(allocator, sink); logger.minimum_level = minimum_level; return logger; } pub fn enabled(self: *const Logger, level: Level) bool { return @intFromEnum(level) >= @intFromEnum(self.minimum_level); } pub fn log(self: *Logger, level: Level, message: []const u8, context: Context) !void { if (!self.enabled(level)) return; self.mutex.lock(); defer self.mutex.unlock(); var out: std.Io.Writer.Allocating = .init(self.allocator); defer out.deinit(); const entry = LogEntry{ .ts_unix_nanos = @intCast(@max(std.time.nanoTimestamp(), 0)), .level = level.label(), .message = message, .component = context.component, .source = if (context.source) |source| source.label() else null, .event = context.event, .phase = context.phase, .vu_id = context.vu_id, .iteration_no = context.iteration_no, }; try std.json.Stringify.value(entry, .{}, &out.writer); try out.writer.writeAll("\n"); try self.sink.write(out.writer.buffered()); try self.sink.flush(); } pub fn logf(self: *Logger, level: Level, context: Context, comptime fmt: []const u8, args: anytype) !void { const message = try std.fmt.allocPrint(self.allocator, fmt, args); defer self.allocator.free(message); try self.log(level, message, context); } };