structured logging

This commit is contained in:
Peter Li 2026-10-19 20:11:27 -07:00 committed by Peterino2
parent 3f59f7f43b
commit 7ae9e15c72
1 changed files with 11 additions and 4 deletions

View File

@ -114,7 +114,9 @@ pub const ArgsSerializer = struct {
const T = @TypeOf(value);
switch (comptime kindFor(T)) {
.boolean => try output.writeByte(@intFromBool(value)),
.enumeration => try writeValue(@backingInt(value), output),
.enumeration => {
try writeValue(@backingInt(value), output);
},
.integer => {
if (T == comptime_int) {
return output.writeInt(u32, std.math.cast(u32, value) orelse return error.IntegerOutOfRange, .little);
@ -308,6 +310,8 @@ pub const LoggerSystem = struct {
logSites: std.ArrayList(LogSite) = .empty,
entries: std.ArrayList(StructuredLog) = .empty,
// enumEntries: std.ArrayList(*const EnumLookup) = .empty,
outputWriters: std.ArrayList(*std.Io.Writer) = .empty,
pub const EnqueueError = ArgsSerializer.Error || std.mem.Allocator.Error || error{ QueueFull, Closed };
@ -323,9 +327,10 @@ pub const LoggerSystem = struct {
/// Encodes using argument types only. No registry access or shared writer.
/// On success, ownership of the payload transfers to the queue.
/// Site/logger descriptors must outlive all queued and retained events.
pub fn enqueue(self: *@This(), site: *const LogSite, logger: *const LoggerState, verbosity: Verbosity, args: anytype) EnqueueError!void {
pub fn enqueueArgs(self: *@This(), site: *const LogSite, logger: *const LoggerState, verbosity: Verbosity, args: anytype) EnqueueError!void {
var writer: std.Io.Writer.Allocating = .init(self.payload_allocator);
defer writer.deinit();
try ArgsSerializer.serialize(args, &writer.writer);
const bytes = try writer.toOwnedSlice();
errdefer self.payload_allocator.free(bytes);
@ -626,6 +631,7 @@ pub const StructuredLog = struct {
if (format == .any) {
try writer.writeAll(op.type_name);
try writer.writeByte('.');
try writer.writeByte('(');
}
@ -721,7 +727,8 @@ pub fn DeclareLogger(comptime LoggerName: []const u8, comptime opts: LoggerOptio
const FmtSite = struct {
pub const site = LogSite.init(LogRef.fromSrcFmt(s, fmt), @TypeOf(args));
};
try gLoggerSystem.enqueue(&FmtSite.site, &loggerState, v, args);
try gLoggerSystem.enqueueArgs(&FmtSite.site, &loggerState, v, args);
}
/// Drop new events when full; callers needing retries can use tryAddLog.
@ -909,5 +916,5 @@ test "testing" {
var y = log.toArrayList();
defer y.deinit(std.testing.allocator);
std.debug.print("log>\n{s}\n", .{y.items});
std.debug.print("log>\n{s}\nlength={d}\n", .{ y.items, y.items.len });
}