From bd01c347af73ff79666d357545b3e821c83fdaf8 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 9 Jul 2023 13:42:26 -0700 Subject: [PATCH] add LoggingWriter --- src/lib.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.zig b/src/lib.zig index f24e5b07325b35d9cfe36c0d8b71e2e16d5839b1..244703eca09b7af37bddbd4199c0ba655542d204 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -588,3 +588,29 @@ pub fn LoggingReader(comptime T: type, comptime scope: @Type(.EnumLiteral)) type } }; } + +pub fn LoggingWriter(comptime T: type, comptime scope: @Type(.EnumLiteral)) type { + return struct { + child_stream: T, + + pub const Error = T.Error; + pub const Writer = std.io.Writer(Self, Error, write); + + const Self = @This(); + + pub fn init(child_stream: T) Self { + return .{ + .child_stream = child_stream, + }; + } + + pub fn writer(self: Self) Writer { + return .{ .context = self }; + } + + fn write(self: Self, bytes: []const u8) Error!usize { + std.log.scoped(scope).debug("{s}", .{bytes}); + return self.child_stream.write(bytes); + } + }; +} -- 2.54.0