use std::{fs, path::PathBuf, time::SystemTime};
pub enum LogLocation {
CurrentDirectory,
StdOut,
PlatformDefault(String),
Custom(String),
}
impl LogLocation {
pub fn resolve(self) -> String {
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
match self {
LogLocation::CurrentDirectory => format!("./log_{}.txt", time),
LogLocation::StdOut => String::from("/dev/stdout"),
LogLocation::PlatformDefault(path) => {
let platform_path = match std::env::consts::OS {
"windows" => PathBuf::from("C:\\ProgramData\\").join(path),
"linux" => PathBuf::from("/var/log/").join(path),
"macos" => {
let home = std::env::var("HOME").unwrap();
PathBuf::from(format!("{}/Library/Logs/", home)).join(path)
}
_ => PathBuf::from("/var/log/").join(path),
};
if let Err(e) = fs::create_dir_all(platform_path.as_os_str()) {
println!("[CRITICAL] Failed to create log directory: {}", e);
} else {
println!("[INFO] Intialized dir {}", platform_path.to_str().unwrap());
}
return platform_path
.join(format!("log_{}.txt", time))
.to_str()
.unwrap()
.to_string();
}
LogLocation::Custom(path) => path,
}
}
}