diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-05-28 19:27:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-28 19:27:14 -0400 |
| commit | 963ffe9d572e6da4ef22672af9b7c54150f66b27 (patch) | |
| tree | 950c39722d71cdd6f2af75c255ee92a318cda516 /lib/std/debug.zig | |
| parent | 759c2211c2eba44cccf0608267bf1a05934ad8a1 (diff) | |
| parent | 3a3d2187f986066859cfb793fb7ee1cae4dfea08 (diff) | |
| download | zig-963ffe9d572e6da4ef22672af9b7c54150f66b27.tar.gz zig-963ffe9d572e6da4ef22672af9b7c54150f66b27.zip | |
Merge pull request #20059 from ziglang/progress
rework std.Progress
Diffstat (limited to 'lib/std/debug.zig')
| -rw-r--r-- | lib/std/debug.zig | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 980b027f0a..1073d6d3ab 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -77,19 +77,28 @@ const PdbOrDwarf = union(enum) { } }; -var stderr_mutex = std.Thread.Mutex{}; +/// Allows the caller to freely write to stderr until `unlockStdErr` is called. +/// +/// During the lock, any `std.Progress` information is cleared from the terminal. +pub fn lockStdErr() void { + std.Progress.lockStdErr(); +} + +pub fn unlockStdErr() void { + std.Progress.unlockStdErr(); +} /// Print to stderr, unbuffered, and silently returning on failure. Intended /// for use in "printf debugging." Use `std.log` functions for proper logging. pub fn print(comptime fmt: []const u8, args: anytype) void { - stderr_mutex.lock(); - defer stderr_mutex.unlock(); + lockStdErr(); + defer unlockStdErr(); const stderr = io.getStdErr().writer(); nosuspend stderr.print(fmt, args) catch return; } pub fn getStderrMutex() *std.Thread.Mutex { - return &stderr_mutex; + @compileError("deprecated. call std.debug.lockStdErr() and std.debug.unlockStdErr() instead which will integrate properly with std.Progress"); } /// TODO multithreaded awareness @@ -107,8 +116,8 @@ pub fn getSelfDebugInfo() !*DebugInfo { /// Tries to print a hexadecimal view of the bytes, unbuffered, and ignores any error returned. /// Obtains the stderr mutex while dumping. pub fn dump_hex(bytes: []const u8) void { - stderr_mutex.lock(); - defer stderr_mutex.unlock(); + lockStdErr(); + defer unlockStdErr(); dump_hex_fallible(bytes) catch {}; } @@ -2750,13 +2759,19 @@ pub const Trace = ConfigurableTrace(2, 4, builtin.mode == .Debug); pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime is_enabled: bool) type { return struct { - addrs: [actual_size][stack_frame_count]usize = undefined, - notes: [actual_size][]const u8 = undefined, - index: Index = 0, + addrs: [actual_size][stack_frame_count]usize, + notes: [actual_size][]const u8, + index: Index, const actual_size = if (enabled) size else 0; const Index = if (enabled) usize else u0; + pub const init: @This() = .{ + .addrs = undefined, + .notes = undefined, + .index = 0, + }; + pub const enabled = is_enabled; pub const add = if (enabled) addNoInline else addNoOp; |
