diff --git a/Cargo.toml b/Cargo.toml index fe234a1..a76725d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,6 @@ clap = { version = "4.5.4", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] } serde_derive = { version = "1.0.197" } num_cpus = "1.16.0" -tracing = { version = "0.1.40" } +tracing = { version = "0.1.40", features = ["log"] } tracing-appender = { version = "0.2.3" } -tracing-subscriber = { version = "0.3.18", features = ["fmt"] } +tracing-subscriber = { version = "0.3.18", features = ["fmt", "tracing-log"] } diff --git a/src/config/args.rs b/src/config/args.rs index b3d818b..d3dba35 100644 --- a/src/config/args.rs +++ b/src/config/args.rs @@ -1,7 +1,6 @@ use std::path::PathBuf; -// TODO: figure out why -h/--help don't work #[derive(clap::Parser, Debug)] pub struct Args { #[arg(short, long="config", default_value = "config.toml")] diff --git a/src/config/config.rs b/src/config/config.rs index 6e80939..fa80380 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -15,12 +15,12 @@ use std::collections::HashMap; pub struct Config { #[cfg(feature = "custom_pieces")] #[serde(rename = "piece")] - pieces: Option>, + pub pieces: Option>, - engine: Option, + pub engine: Option, - log_level: Option, - log_file: Option + pub log_level: Option, + pub log_file: Option } impl Default for Config { @@ -31,7 +31,7 @@ impl Default for Config { engine: Some(Engine::default()), - log_level: Some("INFO".to_string()), + log_level: Some("info".to_string()), log_file: Some(PathBuf::from("saltfish.log")), } } @@ -63,21 +63,21 @@ impl Config { } } -impl Config { - #[cfg(feature = "custom_pieces")] - pub fn get_pieces(&self) -> &Option> { - &self.pieces - } - - pub fn get_engine(&self) -> &Option { - &self.engine - } - - pub fn get_log_level(&self) -> &Option { - &self.log_level - } - - pub fn get_log_file(&self) -> &Option { - &self.log_file - } -} +// impl Config { +// #[cfg(feature = "custom_pieces")] +// pub fn get_pieces(&self) -> &Option> { +// &self.pieces +// } +// +// pub fn get_engine(&self) -> &Engine { +// &self.engine.unwrap() +// } +// +// pub fn get_log_level(&self) -> &Option { +// &self.log_level +// } +// +// pub fn get_log_file(&self) -> &Option { +// &self.log_file +// } +// } diff --git a/src/config/mod.rs b/src/config/mod.rs index 8f60e90..c050e4f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,19 +4,36 @@ mod engine; pub mod error; + +use std::str::FromStr; + +use crate::logger_update; + use self::{args::Args, config::Config}; use clap::Parser; -use tracing::{error, info}; +use tracing::{debug, error, info, Level}; -pub fn get_config() -> Config { - // TODO: implement error handling and logging and handle this case appropriate - let args = Args::try_parse().unwrap_or_else(|err|{ - // error!(err.kind()); - error!("{}", format!("{}", err.kind().as_str().unwrap_or("parsing arguments"))); +pub fn get_config() -> Option { + let args = Args::try_parse().or_else(|err|{ + if ! err.use_stderr() { + println!("{}", err); + return Err(()) + } + error!("{}", err); info!("continuing withouth commandline arguments"); - Args::default() + Ok(Args::default()) }); - let config_path = args.get_config_path().unwrap_or_default(); - Config::read(&config_path).merge_args(&args) + + match args { + Err(_) => None, + Ok(args) => { + let config_path = args.get_config_path().unwrap_or_default(); + let config = Config::read(&config_path).merge_args(&args); + logger_update(&config.log_file.clone().unwrap(), Level::from_str(&config.log_level.clone().unwrap()).unwrap()); + Some(config) + } + } } + + diff --git a/src/logging.rs b/src/logging.rs index 05581a2..0a97abd 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -4,15 +4,24 @@ use tracing::Level; use tracing_subscriber::fmt; pub fn logger_init() { - fmt() + let subscriber = fmt() + .compact() .with_max_level(Level::DEBUG) - .init(); + .finish(); + // tracing::subscriber::set_global_default(subscriber) + // .expect("Failed to set global default subscriber"); + } pub fn logger_update(file: &Path, level: Level) { let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap()); - let _ = tracing_appender::non_blocking(file_appender); - fmt() + let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); + println!("dir: {:?} | file: {:?}", file.parent(), file.file_name()); + let subscriber = fmt() + .compact() .with_max_level(level) + .with_writer(non_blocking) .finish(); + tracing::subscriber::set_global_default(subscriber) + .expect("Failed to set global default subscriber"); } diff --git a/src/main.rs b/src/main.rs index ab637b5..902b6e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,6 @@ )] -use tracing::{info, error, Level}; -use std::str::FromStr; - use crate::config::get_config; mod chess; @@ -20,12 +17,10 @@ use crate::logging::*; fn main() { - logger_init(); + // logger_init(); let config = get_config(); - logger_update( - config.get_log_file().as_ref().unwrap(), - Level::from_str(config.get_log_level().as_ref().unwrap().as_str()).unwrap() - ); - error!("test"); - println!("{config:#?}"); + if config.is_none() { + return + } + println!("2"); }