mirror of
https://github.com/ziglang/zig.git
synced 2026-02-16 06:18:32 +00:00
Merge pull request #9224 from marler8997/defaultLog
A couple std.log conveniences
This commit is contained in:
commit
b3d3e93636
@ -100,6 +100,23 @@ pub const Level = enum {
|
||||
info,
|
||||
/// Debug: messages only useful for debugging.
|
||||
debug,
|
||||
|
||||
/// Returns a string literal of the given level in full text form.
|
||||
pub fn asText(comptime self: Level) switch (self) {
|
||||
.emerg => @TypeOf("emergency"),
|
||||
.crit => @TypeOf("critical"),
|
||||
.err => @TypeOf("error"),
|
||||
.warn => @TypeOf("warning"),
|
||||
else => @TypeOf(@tagName(self)),
|
||||
} {
|
||||
return switch (self) {
|
||||
.emerg => "emergency",
|
||||
.crit => "critical",
|
||||
.err => "error",
|
||||
.warn => "warning",
|
||||
else => @tagName(self),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// The default log level is based on build mode.
|
||||
@ -145,30 +162,34 @@ fn log(
|
||||
if (@typeInfo(@TypeOf(root.log)) != .Fn)
|
||||
@compileError("Expected root.log to be a function");
|
||||
root.log(message_level, scope, format, args);
|
||||
} else if (std.Target.current.os.tag == .freestanding) {
|
||||
// On freestanding one must provide a log function; we do not have
|
||||
// any I/O configured.
|
||||
return;
|
||||
} else {
|
||||
const level_txt = switch (message_level) {
|
||||
.emerg => "emergency",
|
||||
.alert => "alert",
|
||||
.crit => "critical",
|
||||
.err => "error",
|
||||
.warn => "warning",
|
||||
.notice => "notice",
|
||||
.info => "info",
|
||||
.debug => "debug",
|
||||
};
|
||||
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||
const stderr = std.io.getStdErr().writer();
|
||||
const held = std.debug.getStderrMutex().acquire();
|
||||
defer held.release();
|
||||
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
||||
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
|
||||
// any I/O configured.
|
||||
return;
|
||||
}
|
||||
|
||||
const level_txt = comptime message_level.asText();
|
||||
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||
const stderr = std.io.getStdErr().writer();
|
||||
const held = std.debug.getStderrMutex().acquire();
|
||||
defer held.release();
|
||||
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
||||
}
|
||||
|
||||
/// Returns a scoped logging namespace that logs all messages using the scope
|
||||
/// provided here.
|
||||
pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
|
||||
|
||||
@ -585,16 +585,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\ comptime format: []const u8,
|
||||
\\ args: anytype,
|
||||
\\) void {
|
||||
\\ const level_txt = switch (level) {
|
||||
\\ .emerg => "emergency",
|
||||
\\ .alert => "alert",
|
||||
\\ .crit => "critical",
|
||||
\\ .err => "error",
|
||||
\\ .warn => "warning",
|
||||
\\ .notice => "notice",
|
||||
\\ .info => "info",
|
||||
\\ .debug => "debug",
|
||||
\\ };
|
||||
\\ const level_txt = comptime level.asText();
|
||||
\\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||
\\ const stdout = std.io.getStdOut().writer();
|
||||
\\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
||||
@ -638,16 +629,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\ comptime format: []const u8,
|
||||
\\ args: anytype,
|
||||
\\) void {
|
||||
\\ const level_txt = switch (level) {
|
||||
\\ .emerg => "emergency",
|
||||
\\ .alert => "alert",
|
||||
\\ .crit => "critical",
|
||||
\\ .err => "error",
|
||||
\\ .warn => "warning",
|
||||
\\ .notice => "notice",
|
||||
\\ .info => "info",
|
||||
\\ .debug => "debug",
|
||||
\\ };
|
||||
\\ const level_txt = comptime level.asText();
|
||||
\\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||
\\ const stdout = std.io.getStdOut().writer();
|
||||
\\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user