authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-16 18:27:44-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-16 18:27:44-04:00
log9781342042798f7a75f3f7233872bae0fbde3ecc
treebe2ff5a5010332550258811f36af2c1543b46fa0
parentf0b8791da75d2cb9c73424b8270da81e7d0ec333
parentaf592f0ddd4448e746cf288b674c0199325598d5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5607 from daurnimator/cleanup-debug-stderr

std: clean up debug stderr variables

2 files changed, 11 insertions(+), 25 deletions(-)

lib/std/debug.zig+10-24
......@@ -50,33 +50,17 @@ pub const LineInfo = struct {
5050 }
5151};
5252
53/// Tries to write to stderr, unbuffered, and ignores any error returned.
54/// Does not append a newline.
55var stderr_file: File = undefined;
56var stderr_file_writer: File.Writer = undefined;
57
58var stderr_stream: ?*File.OutStream = null;
5953var stderr_mutex = std.Mutex.init();
6054
55/// Tries to write to stderr, unbuffered, and ignores any error returned.
56/// Does not append a newline.
6157pub fn warn(comptime fmt: []const u8, args: var) void {
6258 const held = stderr_mutex.acquire();
6359 defer held.release();
64 const stderr = getStderrStream();
60 const stderr = io.getStdErr().writer();
6561 nosuspend stderr.print(fmt, args) catch return;
6662}
6763
68pub fn getStderrStream() *File.OutStream {
69 if (stderr_stream) |st| {
70 return st;
71 } else {
72 stderr_file = io.getStdErr();
73 stderr_file_writer = stderr_file.outStream();
74 const st = &stderr_file_writer;
75 stderr_stream = st;
76 return st;
77 }
78}
79
8064pub fn getStderrMutex() *std.Mutex {
8165 return &stderr_mutex;
8266}
......@@ -99,6 +83,7 @@ pub fn detectTTYConfig() TTY.Config {
9983 if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| {
10084 return .escape_codes;
10185 } else |_| {
86 const stderr_file = io.getStdErr();
10287 if (stderr_file.supportsAnsiEscapeCodes()) {
10388 return .escape_codes;
10489 } else if (builtin.os.tag == .windows and stderr_file.isTty()) {
......@@ -113,7 +98,7 @@ pub fn detectTTYConfig() TTY.Config {
11398/// TODO multithreaded awareness
11499pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
115100 nosuspend {
116 const stderr = getStderrStream();
101 const stderr = io.getStdErr().writer();
117102 if (builtin.strip_debug_info) {
118103 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
119104 return;
......@@ -134,7 +119,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
134119/// TODO multithreaded awareness
135120pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
136121 nosuspend {
137 const stderr = getStderrStream();
122 const stderr = io.getStdErr().writer();
138123 if (builtin.strip_debug_info) {
139124 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
140125 return;
......@@ -204,7 +189,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace
204189/// TODO multithreaded awareness
205190pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void {
206191 nosuspend {
207 const stderr = getStderrStream();
192 const stderr = io.getStdErr().writer();
208193 if (builtin.strip_debug_info) {
209194 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
210195 return;
......@@ -272,7 +257,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
272257 const held = panic_mutex.acquire();
273258 defer held.release();
274259
275 const stderr = getStderrStream();
260 const stderr = io.getStdErr().writer();
276261 stderr.print(format ++ "\n", args) catch os.abort();
277262 if (trace) |t| {
278263 dumpStackTrace(t.*);
......@@ -297,7 +282,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
297282 // A panic happened while trying to print a previous panic message,
298283 // we're still holding the mutex but that's fine as we're going to
299284 // call abort()
300 const stderr = getStderrStream();
285 const stderr = io.getStdErr().writer();
301286 stderr.print("Panicked during a panic. Aborting.\n", .{}) catch os.abort();
302287 },
303288 else => {
......@@ -458,6 +443,7 @@ pub const TTY = struct {
458443 .Reset => out_stream.writeAll(RESET) catch return,
459444 },
460445 .windows_api => if (builtin.os.tag == .windows) {
446 const stderr_file = io.getStdErr();
461447 const S = struct {
462448 var attrs: windows.WORD = undefined;
463449 var init_attrs = false;
lib/std/json.zig+1-1
......@@ -1288,7 +1288,7 @@ pub const Value = union(enum) {
12881288 var held = std.debug.getStderrMutex().acquire();
12891289 defer held.release();
12901290
1291 const stderr = std.debug.getStderrStream();
1291 const stderr = io.getStdErr().writer();
12921292 std.json.stringify(self, std.json.StringifyOptions{ .whitespace = null }, stderr) catch return;
12931293 }
12941294};