authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-06-24 04:30:05-06:00
committergravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-06-24 04:30:05-06:00
logcadf32d74545a31daaddcfccbf0f8e68efea66a7
tree775ceb86a8e6cde8b6a9122aad6901e4cbeaab95
parent642f5df0abbcfda47df94ba657b24a913d6903d4

Expose mechanism to convert log level to text


2 files changed, 20 insertions(+), 30 deletions(-)

lib/std/log.zig+18-10
...@@ -100,6 +100,23 @@ pub const Level = enum {...@@ -100,6 +100,23 @@ pub const Level = enum {
100 info,100 info,
101 /// Debug: messages only useful for debugging.101 /// Debug: messages only useful for debugging.
102 debug,102 debug,
103
104 /// Returns a string literal of the given level in full text form.
105 pub fn asText(comptime self: Level) switch (self) {
106 .emerg => @TypeOf("emergency"),
107 .crit => @TypeOf("critical"),
108 .err => @TypeOf("error"),
109 .warn => @TypeOf("warning"),
110 else => @TypeOf(@tagName(self)),
111 } {
112 return switch (self) {
113 .emerg => "emergency",
114 .crit => "critical",
115 .err => "error",
116 .warn => "warning",
117 else => @tagName(self),
118 };
119 }
103};120};
104121
105/// The default log level is based on build mode.122/// The default log level is based on build mode.
...@@ -165,16 +182,7 @@ pub fn defaultLog(...@@ -165,16 +182,7 @@ pub fn defaultLog(
165 return;182 return;
166 }183 }
167184
168 const level_txt = switch (message_level) {185 const level_txt = comptime message_level.asText();
169 .emerg => "emergency",
170 .alert => "alert",
171 .crit => "critical",
172 .err => "error",
173 .warn => "warning",
174 .notice => "notice",
175 .info => "info",
176 .debug => "debug",
177 };
178 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";186 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
179 const stderr = std.io.getStdErr().writer();187 const stderr = std.io.getStdErr().writer();
180 const held = std.debug.getStderrMutex().acquire();188 const held = std.debug.getStderrMutex().acquire();
test/compare_output.zig+2-20
...@@ -585,16 +585,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -585,16 +585,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
585 \\ comptime format: []const u8,585 \\ comptime format: []const u8,
586 \\ args: anytype,586 \\ args: anytype,
587 \\) void {587 \\) void {
588 \\ const level_txt = switch (level) {588 \\ const level_txt = comptime level.asText();
589 \\ .emerg => "emergency",
590 \\ .alert => "alert",
591 \\ .crit => "critical",
592 \\ .err => "error",
593 \\ .warn => "warning",
594 \\ .notice => "notice",
595 \\ .info => "info",
596 \\ .debug => "debug",
597 \\ };
598 \\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";589 \\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
599 \\ const stdout = std.io.getStdOut().writer();590 \\ const stdout = std.io.getStdOut().writer();
600 \\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;591 \\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
...@@ -638,16 +629,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -638,16 +629,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
638 \\ comptime format: []const u8,629 \\ comptime format: []const u8,
639 \\ args: anytype,630 \\ args: anytype,
640 \\) void {631 \\) void {
641 \\ const level_txt = switch (level) {632 \\ const level_txt = comptime level.asText();
642 \\ .emerg => "emergency",
643 \\ .alert => "alert",
644 \\ .crit => "critical",
645 \\ .err => "error",
646 \\ .warn => "warning",
647 \\ .notice => "notice",
648 \\ .info => "info",
649 \\ .debug => "debug",
650 \\ };
651 \\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";633 \\ const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
652 \\ const stdout = std.io.getStdOut().writer();634 \\ const stdout = std.io.getStdOut().writer();
653 \\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;635 \\ nosuspend stdout.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;