Expose mechanism to convert log level to text

This commit is contained in:
Jonathan Marler 2021-06-24 04:30:05 -06:00
parent 642f5df0ab
commit cadf32d745
2 changed files with 20 additions and 30 deletions

View file

@ -100,6 +100,23 @@ pub const Level = enum {
info, info,
/// Debug: messages only useful for debugging. /// Debug: messages only useful for debugging.
debug, 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. /// The default log level is based on build mode.
@ -165,16 +182,7 @@ pub fn defaultLog(
return; return;
} }
const level_txt = switch (message_level) { const level_txt = comptime message_level.asText();
.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 prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();
const held = std.debug.getStderrMutex().acquire(); const held = std.debug.getStderrMutex().acquire();

View file

@ -585,16 +585,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ comptime format: []const u8, \\ comptime format: []const u8,
\\ args: anytype, \\ args: anytype,
\\) void { \\) void {
\\ const level_txt = switch (level) { \\ const level_txt = comptime level.asText();
\\ .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 prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
\\ const stdout = std.io.getStdOut().writer(); \\ const stdout = std.io.getStdOut().writer();
\\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; \\ 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, \\ comptime format: []const u8,
\\ args: anytype, \\ args: anytype,
\\) void { \\) void {
\\ const level_txt = switch (level) { \\ const level_txt = comptime level.asText();
\\ .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 prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
\\ const stdout = std.io.getStdOut().writer(); \\ const stdout = std.io.getStdOut().writer();
\\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; \\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;