add logging and remove logging optional feature
Some checks failed
CI / Saltfish (push) Failing after -2m45s
CI / Saltfish [custom_pieces] (push) Successful in -2m47s

This commit is contained in:
fabolous005 2024-04-05 04:35:28 +02:00
parent b7faca1e4b
commit 791749b8d7
8 changed files with 96 additions and 12 deletions

View File

@ -5,7 +5,6 @@ edition = "2021"
[features] [features]
custom_pieces = [] custom_pieces = []
logging = ["dep:tracing", "dep:tracing-subscriber"]
[dependencies] [dependencies]
toml = { version = "0.8.12" } toml = { version = "0.8.12" }
@ -13,5 +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", optional = true } tracing = { version = "0.1.40" }
tracing-subscriber = { version = "0.3.18", optional = true } tracing-appender = { version = "0.2.3" }
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }

0
saltdish Normal file
View File

0
saltfish.log Normal file
View File

View File

@ -1,7 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
#[derive(clap::Parser)] // TODO: figure out why -h/--help don't work
#[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")]
config_path: Option<PathBuf>, config_path: Option<PathBuf>,
@ -14,6 +15,13 @@ pub struct Args {
#[arg(short, long)] #[arg(short, long)]
jobs: Option<u8>, jobs: Option<u8>,
#[arg(long, value_parser =
clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"])
)]
log_level: Option<String>,
#[arg(long)]
log_file: Option<PathBuf>,
} }
impl Default for Args { impl Default for Args {
@ -23,6 +31,9 @@ impl Default for Args {
depth: None, depth: None,
strenght: None, strenght: None,
jobs: None, jobs: None,
log_level: None,
log_file: None,
} }
} }
} }
@ -44,4 +55,12 @@ impl Args {
pub fn get_jobs(&self) -> Option<u8> { pub fn get_jobs(&self) -> Option<u8> {
self.jobs self.jobs
} }
pub fn get_log_level(&self) -> Option<String> {
self.log_level.clone()
}
pub fn get_log_file(&self) -> Option<PathBuf> {
self.log_file.clone()
}
} }

View File

@ -1,4 +1,4 @@
use std::{fs::File, io::Read, path::Path, u8}; use std::{fs::File, io::Read, path::{Path, PathBuf}, u8};
use serde::Deserialize; use serde::Deserialize;
use crate::config::{args::Args, engine::Engine}; use crate::config::{args::Args, engine::Engine};
@ -17,7 +17,10 @@ pub struct Config {
#[serde(rename = "piece")] #[serde(rename = "piece")]
pieces: Option<HashMap<String, CustomPiece>>, pieces: Option<HashMap<String, CustomPiece>>,
engine: Option<Engine> engine: Option<Engine>,
log_level: Option<String>,
log_file: Option<PathBuf>
} }
impl Default for Config { impl Default for Config {
@ -28,6 +31,8 @@ impl Default for Config {
engine: Some(Engine::default()), engine: Some(Engine::default()),
log_level: Some("INFO".to_string()),
log_file: Some(PathBuf::from("saltfish.log")),
} }
} }
} }
@ -50,6 +55,28 @@ impl Config {
args.get_depth().or(self.engine.clone().unwrap_or_default().depth()), args.get_depth().or(self.engine.clone().unwrap_or_default().depth()),
args.get_jobs().or(self.engine.clone().unwrap_or_default().jobs()), args.get_jobs().or(self.engine.clone().unwrap_or_default().jobs()),
)), )),
log_level: args.get_log_level().or(self.log_level),
log_file: args.get_log_file().or(self.log_file)
} }
} }
} }
impl Config {
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
}
}

View File

@ -1,18 +1,22 @@
mod config; mod config;
mod args; mod args;
mod engine; mod engine;
pub mod error; pub mod error;
use self::{args::Args, config::Config, error::ConfigError}; use self::{args::Args, config::Config};
use clap::Parser; use clap::Parser;
use tracing::{error, info};
pub fn get_config() -> Result<Config, ConfigError> {
pub fn get_config() -> Config {
// TODO: implement error handling and logging and handle this case appropriate // TODO: implement error handling and logging and handle this case appropriate
let args = Args::try_parse().unwrap_or_else(|err|{ let args = Args::try_parse().unwrap_or_else(|err|{
println!("error parsing cmd args\n{:#?}", err); // error!(err.kind());
error!("{}", format!("{}", err.kind().as_str().unwrap_or("parsing arguments")));
info!("continuing withouth commandline arguments");
Args::default() Args::default()
}); });
let config_path = args.get_config_path().unwrap_or_default(); let config_path = args.get_config_path().unwrap_or_default();
let config = Config::read(&config_path).merge_args(&args); Config::read(&config_path).merge_args(&args)
Ok(config)
} }

18
src/logging.rs Normal file
View File

@ -0,0 +1,18 @@
use std::path::Path;
use tracing::Level;
use tracing_subscriber::fmt;
pub fn logger_init() {
fmt()
.with_max_level(Level::DEBUG)
.init();
}
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()
.with_max_level(level)
.finish();
}

View File

@ -3,6 +3,10 @@
clippy::module_inception clippy::module_inception
)] )]
use tracing::{info, error, Level};
use std::str::FromStr;
use crate::config::get_config; use crate::config::get_config;
mod chess; mod chess;
@ -10,6 +14,18 @@ mod config;
mod errors; mod errors;
mod logging;
use crate::logging::*;
fn main() { fn main() {
let _config = get_config(); 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:#?}");
} }