mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
Expose default std.log implementation
This allows root.log implementations to capture log messages, process them, and forward them to the default implementation if desired.
This commit is contained in:
parent
31c49ad64d
commit
642f5df0ab
@ -145,11 +145,26 @@ fn log(
|
|||||||
if (@typeInfo(@TypeOf(root.log)) != .Fn)
|
if (@typeInfo(@TypeOf(root.log)) != .Fn)
|
||||||
@compileError("Expected root.log to be a function");
|
@compileError("Expected root.log to be a function");
|
||||||
root.log(message_level, scope, format, args);
|
root.log(message_level, scope, format, args);
|
||||||
} else if (std.Target.current.os.tag == .freestanding) {
|
} else {
|
||||||
|
defaultLog(message_level, scope, format, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The default implementation for root.log. root.log may forward log messages
|
||||||
|
/// to this function.
|
||||||
|
pub fn defaultLog(
|
||||||
|
comptime message_level: Level,
|
||||||
|
comptime scope: @Type(.EnumLiteral),
|
||||||
|
comptime format: []const u8,
|
||||||
|
args: anytype,
|
||||||
|
) void {
|
||||||
|
if (std.Target.current.os.tag == .freestanding) {
|
||||||
// On freestanding one must provide a log function; we do not have
|
// On freestanding one must provide a log function; we do not have
|
||||||
// any I/O configured.
|
// any I/O configured.
|
||||||
return;
|
return;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
const level_txt = switch (message_level) {
|
const level_txt = switch (message_level) {
|
||||||
.emerg => "emergency",
|
.emerg => "emergency",
|
||||||
.alert => "alert",
|
.alert => "alert",
|
||||||
@ -165,8 +180,6 @@ fn log(
|
|||||||
const held = std.debug.getStderrMutex().acquire();
|
const held = std.debug.getStderrMutex().acquire();
|
||||||
defer held.release();
|
defer held.release();
|
||||||
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a scoped logging namespace that logs all messages using the scope
|
/// Returns a scoped logging namespace that logs all messages using the scope
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user