Using CLIARGS.Verbosity everywhere where needed is cumbersome, so centralized logging solution is needed.
Solution
Either start using crates like log and env_logger or cook up your own simple function/macro.
Starting point could be something like what ChatGPT suggested:
#[macro_export]
macro_rules! log_msg {
($fmt:expr $(, $($arg:tt)*)?) => {
println!("[{}:{}] {}", file!(), line!(), $fmt $(, $($arg)*)?);
};
}
fn main() {
...
log_msg!("This is a log message");
log_msg!("Log message with an argument: {}", 42);
...
}
fn log_msg<T: std::fmt::Display>(level: &str, fmt: &str, args: T) {
let file = file!();
let line = line!();
let message = format!("{}", args);
println!("[{}:{}] [{}] {}", file, line, level, fmt, message);
}
fn main() {
...
log_msg("INFO", "This is an info message", "");
log_msg("WARN", "This is a warning message with an argument: {}", 42);
...
}
I bet that neither of above will work right out of the box, but afterwards implementing log levels should be easy.
Problem
Using
CLIARGS.Verbosity
everywhere where needed is cumbersome, so centralized logging solution is needed.Solution
Either start using crates like
log
andenv_logger
or cook up your own simple function/macro.Starting point could be something like what ChatGPT suggested:
I bet that neither of above will work right out of the box, but afterwards implementing log levels should be easy.