commits from last night: enlarge get_config() function
All checks were successful
CI / Saltfish (push) Successful in -2m38s
CI / Saltfish [custom_pieces] (push) Successful in -2m40s

This commit is contained in:
fabolous005 2024-04-06 04:35:22 +02:00
parent 694616f2cf
commit 85e5522950
6 changed files with 69 additions and 49 deletions

View File

@ -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"] }

View File

@ -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")]

View File

@ -15,12 +15,12 @@ use std::collections::HashMap;
pub struct Config {
#[cfg(feature = "custom_pieces")]
#[serde(rename = "piece")]
pieces: Option<HashMap<String, CustomPiece>>,
pub pieces: Option<HashMap<String, CustomPiece>>,
engine: Option<Engine>,
pub engine: Option<Engine>,
log_level: Option<String>,
log_file: Option<PathBuf>
pub log_level: Option<String>,
pub log_file: Option<PathBuf>
}
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<HashMap<String, CustomPiece>> {
&self.pieces
}
pub fn get_engine(&self) -> &Option<Engine> {
&self.engine
}
pub fn get_log_level(&self) -> &Option<String> {
&self.log_level
}
pub fn get_log_file(&self) -> &Option<PathBuf> {
&self.log_file
}
}
// impl Config {
// #[cfg(feature = "custom_pieces")]
// pub fn get_pieces(&self) -> &Option<HashMap<String, CustomPiece>> {
// &self.pieces
// }
//
// pub fn get_engine(&self) -> &Engine {
// &self.engine.unwrap()
// }
//
// pub fn get_log_level(&self) -> &Option<String> {
// &self.log_level
// }
//
// pub fn get_log_file(&self) -> &Option<PathBuf> {
// &self.log_file
// }
// }

View File

@ -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")));
info!("continuing withouth commandline arguments");
Args::default()
});
let config_path = args.get_config_path().unwrap_or_default();
Config::read(&config_path).merge_args(&args)
pub fn get_config() -> Option<Config> {
let args = Args::try_parse().or_else(|err|{
if ! err.use_stderr() {
println!("{}", err);
return Err(())
}
error!("{}", err);
info!("continuing withouth commandline arguments");
Ok(Args::default())
});
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)
}
}
}

View File

@ -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");
}

View File

@ -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");
}