struct EnergyBenchLogger;

impl log::Log for EnergyBenchLogger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        metadata.level() <= log::Level::Info
    }

    fn log(&self, record: &log::Record) {
        if self.enabled(record.metadata()) {
            println!("{} - {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

/// Wrapper around log::LevelFilter, we do this because:
/// - for use with `clap`
/// - users of `energy-bench` do not have to include the log library just to set the logging level.
#[repr(usize)]
#[derive(Clone, Copy)]
pub enum LogLevel {
    Off,
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

impl Into<log::LevelFilter> for LogLevel {
    fn into(self) -> log::LevelFilter {
        use LogLevel::*;
        match self {
            Off => log::LevelFilter::Off,
            Trace => log::LevelFilter::Trace,
            Debug => log::LevelFilter::Debug,
            Info => log::LevelFilter::Info,
            Warn => log::LevelFilter::Warn,
            Error => log::LevelFilter::Error,
        }
    }
}

pub fn enable_logging(log_level: LogLevel) {
    static LOGGER: EnergyBenchLogger = EnergyBenchLogger;
    log::set_logger(&LOGGER)
        .map(|()| log::set_max_level(log_level.into()))
        .unwrap();
}
