authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-08-20 12:24:02+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-08-20 12:24:02+03:00
logb3d3e93636800e830a47871cad8d7728c76c1c71
tree078630f1258a8653fefdd7165a54853d7a5e1236
parente94f3c4e25de123f5d19efdf2e068f7fd222f07e
parentcadf32d74545a31daaddcfccbf0f8e68efea66a7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9224 from marler8997/defaultLog

A couple std.log conveniences

2 files changed, 42 insertions(+), 39 deletions(-)

lib/std/log.zig+40-19
...@@ -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.
...@@ -145,30 +162,34 @@ fn log(...@@ -145,30 +162,34 @@ fn log(
145 if (@typeInfo(@TypeOf(root.log)) != .Fn)162 if (@typeInfo(@TypeOf(root.log)) != .Fn)
146 @compileError("Expected root.log to be a function");163 @compileError("Expected root.log to be a function");
147 root.log(message_level, scope, format, args);164 root.log(message_level, scope, format, args);
148 } else if (std.Target.current.os.tag == .freestanding) {
149 // On freestanding one must provide a log function; we do not have
150 // any I/O configured.
151 return;
152 } else {165 } else {
153 const level_txt = switch (message_level) {166 defaultLog(message_level, scope, format, args);
154 .emerg => "emergency",
155 .alert => "alert",
156 .crit => "critical",
157 .err => "error",
158 .warn => "warning",
159 .notice => "notice",
160 .info => "info",
161 .debug => "debug",
162 };
163 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
164 const stderr = std.io.getStdErr().writer();
165 const held = std.debug.getStderrMutex().acquire();
166 defer held.release();
167 nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
168 }167 }
169 }168 }
170}169}
171170
171/// The default implementation for root.log. root.log may forward log messages
172/// to this function.
173pub fn defaultLog(
174 comptime message_level: Level,
175 comptime scope: @Type(.EnumLiteral),
176 comptime format: []const u8,
177 args: anytype,
178) void {
179 if (std.Target.current.os.tag == .freestanding) {
180 // On freestanding one must provide a log function; we do not have
181 // any I/O configured.
182 return;
183 }
184
185 const level_txt = comptime message_level.asText();
186 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
187 const stderr = std.io.getStdErr().writer();
188 const held = std.debug.getStderrMutex().acquire();
189 defer held.release();
190 nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
191}
192
172/// Returns a scoped logging namespace that logs all messages using the scope193/// Returns a scoped logging namespace that logs all messages using the scope
173/// provided here.194/// provided here.
174pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {195pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
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;