commit cadf32d74545a31daaddcfccbf0f8e68efea66a7 (tree)
parent 642f5df0abbcfda47df94ba657b24a913d6903d4
Author: Jonathan Marler <johnnymarler@gmail.com>
Date: Thu, 24 Jun 2021 04:30:05 -0600
Expose mechanism to convert log level to text
Diffstat:
2 files changed, 20 insertions(+), 30 deletions(-)
diff --git a/lib/std/log.zig b/lib/std/log.zig
@@ -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.
@@ -165,16 +182,7 @@ pub fn defaultLog(
return;
}
- const level_txt = switch (message_level) {
- .emerg => "emergency",
- .alert => "alert",
- .crit => "critical",
- .err => "error",
- .warn => "warning",
- .notice => "notice",
- .info => "info",
- .debug => "debug",
- };
+ 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();
diff --git a/test/compare_output.zig b/test/compare_output.zig
@@ -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;