authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-08 00:21:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-08 00:21:57-07:00
log25d2e7fce04d5cbe63331cc56ab7bafe89c249c4
tree2380175bc0463a770649ca4b46b63a51febd6375
parentad7a09d95a3867ac9d8230c4b9694f711d09390e

fixups from previous commit

* rename the functions * make the other function public and give it a better name * interact with stderr_mutex * std lib test coverage

1 files changed, 12 insertions(+), 4 deletions(-)

lib/std/debug.zig+12-4
...@@ -105,11 +105,15 @@ pub fn getSelfDebugInfo() !*DebugInfo {...@@ -105,11 +105,15 @@ pub fn getSelfDebugInfo() !*DebugInfo {
105}105}
106106
107/// Tries to print a hexadecimal view of the bytes, unbuffered, and ignores any error returned.107/// Tries to print a hexadecimal view of the bytes, unbuffered, and ignores any error returned.
108pub fn hexdump(bytes: []const u8) void {108/// Obtains the stderr mutex while dumping.
109 hexdump_internal(bytes) catch {};109pub fn dump_hex(bytes: []const u8) void {
110 stderr_mutex.lock();
111 defer stderr_mutex.unlock();
112 dump_hex_fallible(bytes) catch {};
110}113}
111114
112fn hexdump_internal(bytes: []const u8) !void {115/// Prints a hexadecimal view of the bytes, unbuffered, returning any error that occurs.
116pub fn dump_hex_fallible(bytes: []const u8) !void {
113 const stderr = std.io.getStdErr();117 const stderr = std.io.getStdErr();
114 const ttyconf = std.io.tty.detectConfig(stderr);118 const ttyconf = std.io.tty.detectConfig(stderr);
115 const writer = stderr.writer();119 const writer = stderr.writer();
...@@ -140,7 +144,7 @@ fn hexdump_internal(bytes: []const u8) !void {...@@ -140,7 +144,7 @@ fn hexdump_internal(bytes: []const u8) !void {
140 if (std.ascii.isPrint(byte)) {144 if (std.ascii.isPrint(byte)) {
141 try writer.writeByte(byte);145 try writer.writeByte(byte);
142 } else {146 } else {
143 // TODO: remove this `if` when https://github.com/ziglang/zig/issues/7600 is fixed147 // Related: https://github.com/ziglang/zig/issues/7600
144 if (ttyconf == .windows_api) {148 if (ttyconf == .windows_api) {
145 try writer.writeByte('.');149 try writer.writeByte('.');
146 continue;150 continue;
...@@ -2831,3 +2835,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize...@@ -2831,3 +2835,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
2831 }2835 }
2832 };2836 };
2833}2837}
2838
2839test {
2840 _ = &dump_hex;
2841}