Skip to content

Commit bd5745d

Browse files
committed
std.log.defaultLog: provide a small buffer
1 parent 5360968 commit bd5745d

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

lib/std/log.zig

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ pub fn defaultLogEnabled(comptime message_level: Level) bool {
137137
return comptime logEnabled(message_level, default_log_scope);
138138
}
139139

140-
/// The default implementation for the log function, custom log functions may
140+
/// The default implementation for the log function. Custom log functions may
141141
/// forward log messages to this function.
142+
///
143+
/// Uses a 64-byte buffer for formatted printing which is flushed before this
144+
/// function returns.
142145
pub fn defaultLog(
143146
comptime message_level: Level,
144147
comptime scope: @Type(.enum_literal),
@@ -147,16 +150,10 @@ pub fn defaultLog(
147150
) void {
148151
const level_txt = comptime message_level.asText();
149152
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
150-
const stderr = std.fs.File.stderr().deprecatedWriter();
151-
var bw = std.io.bufferedWriter(stderr);
152-
const writer = bw.writer();
153-
154-
std.debug.lockStdErr();
155-
defer std.debug.unlockStdErr();
156-
nosuspend {
157-
writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
158-
bw.flush() catch return;
159-
}
153+
var buffer: [64]u8 = undefined;
154+
const stderr = std.debug.lockStderrWriter(&buffer);
155+
defer std.debug.unlockStderrWriter();
156+
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
160157
}
161158

162159
/// Returns a scoped logging namespace that logs all messages using the scope

0 commit comments

Comments
 (0)