mclang / kiho-wt-puncher

Rust CLI app to make Kiho worktime login/logout punches using Kiho API
GNU General Public License v3.0
1 stars 0 forks source link

Centralized logging with Levels/Verbosity #1

Open mclang opened 12 months ago

mclang commented 12 months ago

Problem

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.