From 2888f43c8acf2379a54f79f92dd67b017718efb0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sun, 16 Oct 2022 17:03:06 -0700 Subject: Make debug logs configurable --- src/output.zig | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/output.zig b/src/output.zig index 6ae8ae4ff..3d9241500 100644 --- a/src/output.zig +++ b/src/output.zig @@ -340,11 +340,15 @@ pub fn print(comptime fmt: string, args: anytype) void { } /// Debug-only logs which should not appear in release mode +/// To enable a specific log at runtime, set the environment variable +/// BUN_DEBUG_${TAG} to 1 +/// For example, to enable the "foo" log, set the environment variable +/// BUN_DEBUG_foo=1 +/// To enable all logs, set the environment variable +/// BUN_DEBUG_ALL=1 const _log_fn = fn (comptime fmt: string, args: anytype) callconv(.Inline) void; pub fn scoped(comptime tag: @Type(.EnumLiteral), comptime disabled: bool) _log_fn { - const disable = comptime !Environment.isDebug or disabled; - - if (comptime disable) { + if (comptime !Environment.isDebug) { return struct { pub inline fn log(comptime _: string, _: anytype) void {} }.log; @@ -355,8 +359,29 @@ pub fn scoped(comptime tag: @Type(.EnumLiteral), comptime disabled: bool) _log_f var buffered_writer: BufferedWriter = undefined; var out: BufferedWriter.Writer = undefined; var out_set = false; + var really_disable = disabled; + var evaluated_disable = false; + /// Debug-only logs which should not appear in release mode + /// To enable a specific log at runtime, set the environment variable + /// BUN_DEBUG_${TAG} to 1 + /// For example, to enable the "foo" log, set the environment variable + /// BUN_DEBUG_foo=1 + /// To enable all logs, set the environment variable + /// BUN_DEBUG_ALL=1 pub inline fn log(comptime fmt: string, args: anytype) void { + if (!evaluated_disable) { + evaluated_disable = true; + if (std.os.getenv("BUN_DEBUG_ALL") != null or + std.os.getenv("BUN_DEBUG_" ++ @tagName(tag)) != null) + { + really_disable = false; + } + } + + if (really_disable) + return; + if (!out_set) { buffered_writer = .{ .unbuffered_writer = writer(), -- cgit v1.2.3