structured logging
This commit is contained in:
parent
3f59f7f43b
commit
7ae9e15c72
|
|
@ -114,7 +114,9 @@ pub const ArgsSerializer = struct {
|
||||||
const T = @TypeOf(value);
|
const T = @TypeOf(value);
|
||||||
switch (comptime kindFor(T)) {
|
switch (comptime kindFor(T)) {
|
||||||
.boolean => try output.writeByte(@intFromBool(value)),
|
.boolean => try output.writeByte(@intFromBool(value)),
|
||||||
.enumeration => try writeValue(@backingInt(value), output),
|
.enumeration => {
|
||||||
|
try writeValue(@backingInt(value), output);
|
||||||
|
},
|
||||||
.integer => {
|
.integer => {
|
||||||
if (T == comptime_int) {
|
if (T == comptime_int) {
|
||||||
return output.writeInt(u32, std.math.cast(u32, value) orelse return error.IntegerOutOfRange, .little);
|
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,
|
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 };
|
||||||
|
|
@ -323,9 +327,10 @@ pub const LoggerSystem = struct {
|
||||||
/// Encodes using argument types only. No registry access or shared writer.
|
/// Encodes using argument types only. No registry access or shared writer.
|
||||||
/// On success, ownership of the payload transfers to the queue.
|
/// On success, ownership of the payload transfers to the queue.
|
||||||
/// Site/logger descriptors must outlive all queued and retained events.
|
/// 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);
|
var writer: std.Io.Writer.Allocating = .init(self.payload_allocator);
|
||||||
defer writer.deinit();
|
defer writer.deinit();
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -626,6 +631,7 @@ 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('(');
|
try writer.writeByte('(');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -721,7 +727,8 @@ pub fn DeclareLogger(comptime LoggerName: []const u8, comptime opts: LoggerOptio
|
||||||
const FmtSite = struct {
|
const FmtSite = struct {
|
||||||
pub const site = LogSite.init(LogRef.fromSrcFmt(s, fmt), @TypeOf(args));
|
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.
|
/// Drop new events when full; callers needing retries can use tryAddLog.
|
||||||
|
|
@ -909,5 +916,5 @@ test "testing" {
|
||||||
|
|
||||||
var y = log.toArrayList();
|
var y = log.toArrayList();
|
||||||
defer y.deinit(std.testing.allocator);
|
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 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue