use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use nadir_core::log::{self, FileConfig, info, warn};
use nadir_platform::user_dirs;
const FILE_NAME: &str = "nadir.log";
const ROTATION: FileConfig = FileConfig {
max_bytes: 4 * 1024 * 1024,
keep: 3,
rotate_on_open: true,
};
pub(crate) fn start(studio: &str, game: &str) {
let dirs = match user_dirs(studio, game) {
Ok(dirs) => dirs,
Err(error) => {
warn!("no log file: {error}");
return;
}
};
match open_in(&dirs.logs) {
Ok(path) => info!("logging to {}", path.display()),
Err(error) => warn!(
"no log file: could not open one in {}: {error}",
dirs.logs.display()
),
}
}
fn open_in(dir: &Path) -> io::Result<PathBuf> {
fs::create_dir_all(dir)?;
let path = dir.join(FILE_NAME);
log::open_file(&path, ROTATION)?;
Ok(path)
}
#[cfg(test)]
mod tests {
use std::env;
use std::process;
use super::*;
fn read(path: &Path) -> String {
fs::read_to_string(path).unwrap_or_else(|error| panic!("read {}: {error}", path.display()))
}
#[test]
fn the_log_file_is_created_with_its_folder_and_keeps_the_last_three_runs() {
let root = env::temp_dir().join(format!("nadir-engine-log-{}", process::id()));
let dir = root.join("Studio").join("Game");
log::set_stderr(false);
for run in 1..=5 {
let path = open_in(&dir).expect("open the log file");
assert_eq!(path, dir.join("nadir.log"));
info!("written by run {run}");
}
log::close_file();
log::set_stderr(true);
let mut names: Vec<String> = fs::read_dir(&dir)
.expect("list the log folder")
.map(|entry| {
let entry = entry.expect("read a folder entry");
entry.file_name().to_string_lossy().into_owned()
})
.collect();
names.sort();
assert_eq!(
names,
["nadir.log", "nadir.log.1", "nadir.log.2", "nadir.log.3"]
);
for (name, run) in [
("nadir.log", 5),
("nadir.log.1", 4),
("nadir.log.2", 3),
("nadir.log.3", 2),
] {
let text = read(&dir.join(name));
assert!(
text.contains(&format!("written by run {run}")),
"{name}: {text}"
);
assert_eq!(text.matches("written by run").count(), 1, "{name}: {text}");
}
fs::remove_dir_all(&root).expect("remove the test folder");
}
}