Skip to content

Commit

Permalink
Reuse the same fixed buffer stream for all logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Jul 12, 2024
1 parent faf79c9 commit 2a8357b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const PRINTKRB_RECORD_MAX = 1024;
var mutex = std.Thread.Mutex{};
var kmsg: ?std.fs.File = null;

// The Zig string formatter can make many individual writes to our
// writer depending on the format string, so we do all the formatting
// ahead of time here so we can perform the write all at once when the
// log line goes to the kernel.
var log_buf: [PRINTKRB_RECORD_MAX]u8 = undefined;
var stream = std.io.fixedBufferStream(&log_buf);

pub fn init() !void {
kmsg = try std.fs.cwd().openFile(KMSG, .{ .mode = .write_only });
}
Expand Down Expand Up @@ -61,15 +68,12 @@ pub fn logFn(
mutex.lock();
defer mutex.unlock();

// The Zig string formatter can make many individual writes to our
// writer depending on the format string, so we do all the formatting
// ahead of time here so we can perform the write all at once when the
// log line goes to the kernel.
var buf: [PRINTKRB_RECORD_MAX]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
stream.reset();

stream.writer().print(
"<" ++ syslog_prefix ++ ">" ++ LOG_PREFIX ++ ": " ++ format,
args,
) catch {};
file.writeAll(buf[0..stream.pos]) catch {};

file.writeAll(log_buf[0..stream.pos]) catch {};
}

0 comments on commit 2a8357b

Please sign in to comment.