diff --git a/lib/std/log.zig b/lib/std/log.zig index e0e002d6001d..33ac0d24eabc 100644 --- a/lib/std/log.zig +++ b/lib/std/log.zig @@ -96,18 +96,20 @@ pub const Level = enum { }; /// The default log level is based on build mode. -pub const default_level: Level = switch (builtin.mode) { - .Debug => .debug, - .ReleaseSafe => .info, - .ReleaseFast, .ReleaseSmall => .err, -}; +pub fn defaultLevel() Level { + return switch (builtin.mode) { + .Debug => .debug, + .ReleaseSafe => .info, + .ReleaseFast, .ReleaseSmall => .err, + }; +} /// The current log level. This is set to root.log_level if present, otherwise /// log.default_level. -pub const level: Level = if (@hasDecl(root, "log_level")) +pub const level: fn () Level = if (@hasDecl(root, "log_level")) root.log_level else - default_level; + defaultLevel; pub const ScopeLevel = struct { scope: @Type(.EnumLiteral), @@ -129,7 +131,7 @@ fn log( inline for (scope_levels) |scope_level| { if (scope_level.scope == scope) break :blk scope_level.level; } - break :blk level; + break :blk level(); }; if (@enumToInt(message_level) <= @enumToInt(effective_log_level)) { diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 67f6b331221d..b5f232d5cea3 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -20,7 +20,10 @@ pub var failing_allocator_instance = FailingAllocator.init(base_allocator_instan pub var base_allocator_instance = std.heap.FixedBufferAllocator.init(""); /// TODO https://github.com/ziglang/zig/issues/5738 -pub var log_level = std.log.Level.warn; +pub var current_log_level: std.log.Level = .warn; +pub fn log_level() std.log.Level { + return current_log_level; +} /// This is available to any test that wants to execute Zig in a child process. /// It will be the same executable that is running `zig test`. diff --git a/lib/test_runner.zig b/lib/test_runner.zig index 8a3c1f0ca792..cf0ba7daf668 100644 --- a/lib/test_runner.zig +++ b/lib/test_runner.zig @@ -53,7 +53,7 @@ pub fn main() void { leaks += 1; } } - std.testing.log_level = .warn; + std.testing.current_log_level = .warn; var test_node = root_node.start(test_fn.name, 0); test_node.activate(); @@ -123,7 +123,7 @@ pub fn log( if (@enumToInt(message_level) <= @enumToInt(std.log.Level.err)) { log_err_count += 1; } - if (@enumToInt(message_level) <= @enumToInt(std.testing.log_level)) { + if (@enumToInt(message_level) <= @enumToInt(std.testing.current_log_level)) { std.debug.print( "[" ++ @tagName(scope) ++ "] (" ++ @tagName(message_level) ++ "): " ++ format ++ "\n", args, diff --git a/src/main.zig b/src/main.zig index 6263a6a40265..6c8036bb179e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -92,11 +92,13 @@ const debug_usage = normal_usage ++ const usage = if (debug_extensions_enabled) debug_usage else normal_usage; -pub const log_level: std.log.Level = switch (builtin.mode) { - .Debug => .debug, - .ReleaseSafe, .ReleaseFast => .info, - .ReleaseSmall => .err, -}; +pub fn log_level() std.log.Level { + return switch (builtin.mode) { + .Debug => .debug, + .ReleaseSafe, .ReleaseFast => .info, + .ReleaseSmall => .err, + }; +} var log_scopes: std.ArrayListUnmanaged([]const u8) = .{}; @@ -109,7 +111,7 @@ pub fn log( // Hide debug messages unless: // * logging enabled with `-Dlog`. // * the --debug-log arg for the scope has been provided - if (@enumToInt(level) > @enumToInt(std.log.level) or + if (@enumToInt(level) > @enumToInt(std.log.level()) or @enumToInt(level) > @enumToInt(std.log.Level.info)) { if (!build_options.enable_logging) return; diff --git a/test/compare_output.zig b/test/compare_output.zig index e007f198bb1e..3f4693eae64d 100644 --- a/test/compare_output.zig +++ b/test/compare_output.zig @@ -440,7 +440,9 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { cases.add("std.log per scope log level override", \\const std = @import("std"); \\ - \\pub const log_level: std.log.Level = .debug; + \\pub fn log_level() std.log.Level { + \\ return .debug; + \\} \\ \\pub const scope_levels = [_]std.log.ScopeLevel{ \\ .{ .scope = .a, .level = .warn }, @@ -494,7 +496,9 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { cases.add("std.heap.LoggingAllocator logs to std.log", \\const std = @import("std"); \\ - \\pub const log_level: std.log.Level = .debug; + \\pub fn log_level() std.log.Level { + \\ return .debug; + \\} \\ \\pub fn main() !void { \\ var allocator_buf: [10]u8 = undefined;