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

Conversation

lun-4
Copy link
Contributor

@lun-4 lun-4 commented Sep 5, 2022

From #2586 (comment)

Making it configurable at runtime would not be something the default thing would do but you could override the log function to achieve that behavior.

And from #2586 (comment)

You could also make default_level a function, which just returns a comptime value by default, or root can make it a function that returns a comptime or runtime value.

Overriding the log function isn't enough because level checking happens before that, so it'd make sense to expose that bit of logic for API users to change.

Overriding the current way level is set also isn't enough because that's set as a const. That leaves an overridable function to be done, as Marler suggested.

Here's an example of how this patch looks in practice:

var current_log_level: std.log.Level = .info; // default

pub fn log_level() std.log.Level {
    return current_log_level;
}

pub fn main() !void {
   // ...
   if (args.options.verbose) current_log_level = .debug;
   // ...
}

@lun-4 lun-4 force-pushed the std-log-runtime-level branch 2 times, most recently from a56aa86 to 31a7acc Compare September 5, 2022 04:17
this lets users of std.log set the function to be run at runtime
@lun-4 lun-4 force-pushed the std-log-runtime-level branch from 31a7acc to 6f1f757 Compare September 5, 2022 04:23
@lun-4 lun-4 closed this Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant