diff --git a/src/lib.zig b/src/lib.zig index 4aa8a5c66eaaae4ae6cf796105556b358ee113aa..f24e5b07325b35d9cfe36c0d8b71e2e16d5839b1 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -561,3 +561,30 @@ pub fn FieldUnion(comptime T: type) type { .decls = &.{}, } }); } + +pub fn LoggingReader(comptime T: type, comptime scope: @Type(.EnumLiteral)) type { + return struct { + child_stream: T, + + pub const Error = T.Error; + pub const Reader = std.io.Reader(Self, Error, read); + + const Self = @This(); + + pub fn init(child_stream: T) Self { + return .{ + .child_stream = child_stream, + }; + } + + pub fn reader(self: Self) Reader { + return .{ .context = self }; + } + + fn read(self: Self, dest: []u8) Error!usize { + const n = try self.child_stream.read(dest); + std.log.scoped(scope).debug("{s}", .{dest[0..n]}); + return n; + } + }; +}