commits from last night: enlarge get_config() function
This commit is contained in:
parent
694616f2cf
commit
85e5522950
@ -12,6 +12,6 @@ clap = { version = "4.5.4", features = ["derive"] }
|
|||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_derive = { version = "1.0.197" }
|
serde_derive = { version = "1.0.197" }
|
||||||
num_cpus = "1.16.0"
|
num_cpus = "1.16.0"
|
||||||
tracing = { version = "0.1.40" }
|
tracing = { version = "0.1.40", features = ["log"] }
|
||||||
tracing-appender = { version = "0.2.3" }
|
tracing-appender = { version = "0.2.3" }
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
tracing-subscriber = { version = "0.3.18", features = ["fmt", "tracing-log"] }
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
||||||
// TODO: figure out why -h/--help don't work
|
|
||||||
#[derive(clap::Parser, Debug)]
|
#[derive(clap::Parser, Debug)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
#[arg(short, long="config", default_value = "config.toml")]
|
#[arg(short, long="config", default_value = "config.toml")]
|
||||||
|
|||||||
@ -15,12 +15,12 @@ use std::collections::HashMap;
|
|||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
#[serde(rename = "piece")]
|
#[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>,
|
pub log_level: Option<String>,
|
||||||
log_file: Option<PathBuf>
|
pub log_file: Option<PathBuf>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@ -31,7 +31,7 @@ impl Default for Config {
|
|||||||
|
|
||||||
engine: Some(Engine::default()),
|
engine: Some(Engine::default()),
|
||||||
|
|
||||||
log_level: Some("INFO".to_string()),
|
log_level: Some("info".to_string()),
|
||||||
log_file: Some(PathBuf::from("saltfish.log")),
|
log_file: Some(PathBuf::from("saltfish.log")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,21 +63,21 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
// impl Config {
|
||||||
#[cfg(feature = "custom_pieces")]
|
// #[cfg(feature = "custom_pieces")]
|
||||||
pub fn get_pieces(&self) -> &Option<HashMap<String, CustomPiece>> {
|
// pub fn get_pieces(&self) -> &Option<HashMap<String, CustomPiece>> {
|
||||||
&self.pieces
|
// &self.pieces
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
pub fn get_engine(&self) -> &Option<Engine> {
|
// pub fn get_engine(&self) -> &Engine {
|
||||||
&self.engine
|
// &self.engine.unwrap()
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
pub fn get_log_level(&self) -> &Option<String> {
|
// pub fn get_log_level(&self) -> &Option<String> {
|
||||||
&self.log_level
|
// &self.log_level
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
pub fn get_log_file(&self) -> &Option<PathBuf> {
|
// pub fn get_log_file(&self) -> &Option<PathBuf> {
|
||||||
&self.log_file
|
// &self.log_file
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@ -4,19 +4,36 @@ mod engine;
|
|||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::logger_update;
|
||||||
|
|
||||||
use self::{args::Args, config::Config};
|
use self::{args::Args, config::Config};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use tracing::{error, info};
|
use tracing::{debug, error, info, Level};
|
||||||
|
|
||||||
|
|
||||||
pub fn get_config() -> Config {
|
pub fn get_config() -> Option<Config> {
|
||||||
// TODO: implement error handling and logging and handle this case appropriate
|
let args = Args::try_parse().or_else(|err|{
|
||||||
let args = Args::try_parse().unwrap_or_else(|err|{
|
if ! err.use_stderr() {
|
||||||
// error!(err.kind());
|
println!("{}", err);
|
||||||
error!("{}", format!("{}", err.kind().as_str().unwrap_or("parsing arguments")));
|
return Err(())
|
||||||
|
}
|
||||||
|
error!("{}", err);
|
||||||
info!("continuing withouth commandline arguments");
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -4,15 +4,24 @@ use tracing::Level;
|
|||||||
use tracing_subscriber::fmt;
|
use tracing_subscriber::fmt;
|
||||||
|
|
||||||
pub fn logger_init() {
|
pub fn logger_init() {
|
||||||
fmt()
|
let subscriber = fmt()
|
||||||
|
.compact()
|
||||||
.with_max_level(Level::DEBUG)
|
.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) {
|
pub fn logger_update(file: &Path, level: Level) {
|
||||||
let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap());
|
let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap());
|
||||||
let _ = tracing_appender::non_blocking(file_appender);
|
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
|
||||||
fmt()
|
println!("dir: {:?} | file: {:?}", file.parent(), file.file_name());
|
||||||
|
let subscriber = fmt()
|
||||||
|
.compact()
|
||||||
.with_max_level(level)
|
.with_max_level(level)
|
||||||
|
.with_writer(non_blocking)
|
||||||
.finish();
|
.finish();
|
||||||
|
tracing::subscriber::set_global_default(subscriber)
|
||||||
|
.expect("Failed to set global default subscriber");
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/main.rs
15
src/main.rs
@ -4,9 +4,6 @@
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
|
|
||||||
use tracing::{info, error, Level};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::config::get_config;
|
use crate::config::get_config;
|
||||||
|
|
||||||
mod chess;
|
mod chess;
|
||||||
@ -20,12 +17,10 @@ use crate::logging::*;
|
|||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
logger_init();
|
// logger_init();
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
logger_update(
|
if config.is_none() {
|
||||||
config.get_log_file().as_ref().unwrap(),
|
return
|
||||||
Level::from_str(config.get_log_level().as_ref().unwrap().as_str()).unwrap()
|
}
|
||||||
);
|
println!("2");
|
||||||
error!("test");
|
|
||||||
println!("{config:#?}");
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user