diff --git a/.gitignore b/.gitignore index afa321b..f9e18df 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ /.vscode # Ignore this, may include user specific paths. /.cargo/config.toml -output.log \ No newline at end of file + +# Ignore logs folder generared by application. +/logs diff --git a/src/config.rs b/src/config.rs index 4f0ad9e..4ee0a0e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,7 @@ use std::path::{Path, PathBuf}; pub const STOP_FILE_NAME: &str = "stop-experiment"; pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278"; +pub const LOG_FOLDER: &str = "logs"; pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED; pub const SERVER_PORT: u16 = 7301; diff --git a/src/logger.rs b/src/logger.rs index 8c50ea1..4df2e97 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,4 +1,11 @@ +use std::path::Path; + +use ops_sat_rs::config::LOG_FOLDER; + pub fn setup_logger() -> Result<(), fern::InitError> { + if !Path::new(LOG_FOLDER).exists() && std::fs::create_dir_all(LOG_FOLDER).is_err() { + eprintln!("Failed to create log folder '{}'", LOG_FOLDER); + } fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( @@ -12,7 +19,8 @@ pub fn setup_logger() -> Result<(), fern::InitError> { .level(log::LevelFilter::Debug) .chain(std::io::stdout()) .chain(fern::log_file(format!( - "output_{}.log", + "{}/output_{}.log", + LOG_FOLDER, humantime::format_rfc3339_seconds(std::time::SystemTime::now()) ))?) .apply()?;