create logs in subfolder

This commit is contained in:
Robin Müller 2024-04-16 15:08:34 +02:00
parent 45930a104b
commit 7da9e2364b
3 changed files with 13 additions and 2 deletions

4
.gitignore vendored
View File

@ -4,4 +4,6 @@
/.vscode /.vscode
# Ignore this, may include user specific paths. # Ignore this, may include user specific paths.
/.cargo/config.toml /.cargo/config.toml
output.log
# Ignore logs folder generared by application.
/logs

View File

@ -9,6 +9,7 @@ use std::path::{Path, PathBuf};
pub const STOP_FILE_NAME: &str = "stop-experiment"; pub const STOP_FILE_NAME: &str = "stop-experiment";
pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278"; pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278";
pub const LOG_FOLDER: &str = "logs";
pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED; pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED;
pub const SERVER_PORT: u16 = 7301; pub const SERVER_PORT: u16 = 7301;

View File

@ -1,4 +1,11 @@
use std::path::Path;
use ops_sat_rs::config::LOG_FOLDER;
pub fn setup_logger() -> Result<(), fern::InitError> { 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() fern::Dispatch::new()
.format(move |out, message, record| { .format(move |out, message, record| {
out.finish(format_args!( out.finish(format_args!(
@ -12,7 +19,8 @@ pub fn setup_logger() -> Result<(), fern::InitError> {
.level(log::LevelFilter::Debug) .level(log::LevelFilter::Debug)
.chain(std::io::stdout()) .chain(std::io::stdout())
.chain(fern::log_file(format!( .chain(fern::log_file(format!(
"output_{}.log", "{}/output_{}.log",
LOG_FOLDER,
humantime::format_rfc3339_seconds(std::time::SystemTime::now()) humantime::format_rfc3339_seconds(std::time::SystemTime::now())
))?) ))?)
.apply()?; .apply()?;