use std::{env, path::PathBuf};
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
pub fn setup_logger(p: Option<PathBuf>) -> Result<(), fern::InitError> {
match env::var("RUST_LOG") {
Ok(_) => {
std::env::set_var("RUST_LOG", "trace");
warn!("$RUST_LOG was not set in $ENV");
info!("$RUST_LOG set to 'trace'")
}
Err(e) => {
error!("Unable to get, or set $RUST_LOG\n{e}");
}
};
let termite_path = match p {
Some(p) => p,
None => std::path::PathBuf::from("nvtop.log"),
};
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.level(), record.target(), record.line().unwrap_or(0), message
))
})
.chain(std::io::stdout())
.chain(fern::log_file(termite_path)?)
.apply()?;
trace!("Logger setup complete.");
Ok(())
}