Skip to content

std.log: make current level be a function #12748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/std/log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion lib/std/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions lib/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) = .{};

Expand All @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions test/compare_output.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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;
Expand Down