structured logging now properly contains enum values

This commit is contained in:
Peterino2 2026-10-19 20:50:17 -07:00
parent 7ae9e15c72
commit f270c76cc9
1 changed files with 52 additions and 2 deletions

View File

@ -17,6 +17,7 @@ pub const ArgsDecoderOp = struct {
signed: bool = false, signed: bool = false,
fields: []const Field = &.{}, fields: []const Field = &.{},
is_tuple: bool = false, is_tuple: bool = false,
enum_ref: ?*EnumTable = null,
pub const Field = struct { name: []const u8, op: ArgsDecoderOp }; pub const Field = struct { name: []const u8, op: ArgsDecoderOp };
}; };
@ -46,6 +47,7 @@ pub const ArgsDecoder = struct {
const full_name = @typeName(T); const full_name = @typeName(T);
const name_start = if (std.mem.lastIndexOfScalar(u8, full_name, '.')) |index| index + 1 else 0; const name_start = if (std.mem.lastIndexOfScalar(u8, full_name, '.')) |index| index + 1 else 0;
op.type_name = full_name[name_start..]; op.type_name = full_name[name_start..];
op.enum_ref = &EnumInfo(T).Table;
}, },
.comptime_int => op.bits = 32, .comptime_int => op.bits = 32,
.comptime_float => op.bits = 64, .comptime_float => op.bits = 64,
@ -288,6 +290,36 @@ const PendingLog = struct {
serialized_args: []const u8, serialized_args: []const u8,
}; };
pub const EnumEntry = struct {
value: i64,
name: []const u8,
};
pub const EnumTable = []const EnumEntry;
pub fn MakeEnumTable(comptime T: type) EnumTable {
const table = comptime blk: {
const info = @typeInfo(T).@"enum";
var entries: [info.field_names.len]EnumEntry = undefined;
for (info.field_names, info.field_values, 0..) |name, value, i| {
entries[i] = .{ .value = @intCast(value), .name = name };
}
break :blk entries;
};
return &table;
}
pub fn EnumInfo(comptime T: type) type {
return struct {
pub const Name: []const u8 = @typeName(T);
pub var Table: EnumTable = MakeEnumTable(T);
pub fn touch() void {
// do nothing
}
};
}
/// Keep at a stable address. Initialize before starting producers; close and /// Keep at a stable address. Initialize before starting producers; close and
/// join all producers before deinit/reinitialization. Only one thread may drain, /// join all producers before deinit/reinitialization. Only one thread may drain,
/// inspect the registries/entries, render to stdout, or deinitialize the context. /// inspect the registries/entries, render to stdout, or deinitialize the context.
@ -310,8 +342,6 @@ pub const LoggerSystem = struct {
logSites: std.ArrayList(LogSite) = .empty, logSites: std.ArrayList(LogSite) = .empty,
entries: std.ArrayList(StructuredLog) = .empty, entries: std.ArrayList(StructuredLog) = .empty,
// enumEntries: std.ArrayList(*const EnumLookup) = .empty,
outputWriters: std.ArrayList(*std.Io.Writer) = .empty, outputWriters: std.ArrayList(*std.Io.Writer) = .empty,
pub const EnqueueError = ArgsSerializer.Error || std.mem.Allocator.Error || error{ QueueFull, Closed }; pub const EnqueueError = ArgsSerializer.Error || std.mem.Allocator.Error || error{ QueueFull, Closed };
@ -331,6 +361,16 @@ pub const LoggerSystem = struct {
var writer: std.Io.Writer.Allocating = .init(self.payload_allocator); var writer: std.Io.Writer.Allocating = .init(self.payload_allocator);
defer writer.deinit(); defer writer.deinit();
inline for (args) |a| {
switch (@typeInfo(@TypeOf(a))) {
.@"enum" => {
const T = @TypeOf(a);
EnumInfo(T).touch();
},
else => {},
}
}
try ArgsSerializer.serialize(args, &writer.writer); try ArgsSerializer.serialize(args, &writer.writer);
const bytes = try writer.toOwnedSlice(); const bytes = try writer.toOwnedSlice();
errdefer self.payload_allocator.free(bytes); errdefer self.payload_allocator.free(bytes);
@ -632,6 +672,16 @@ pub const StructuredLog = struct {
if (format == .any) { if (format == .any) {
try writer.writeAll(op.type_name); try writer.writeAll(op.type_name);
try writer.writeByte('.'); try writer.writeByte('.');
if (op.enum_ref) |eref| {
const count = (@as(usize, op.bits) + 7) / 8;
const integer = try cursor.integer(count);
for (eref.*) |e| {
if (e.value == @as(i64, @intCast(integer))) {
try writer.writeAll(e.name);
break;
}
}
}
try writer.writeByte('('); try writer.writeByte('(');
} }