Compare commits

..

No commits in common. "ce7866df0943d0f0bf153b119a832d76abd2715c" and "4cabecea95fe65979c2846975ec7f980117c708f" have entirely different histories.

8 changed files with 113 additions and 31 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"
fern = { version = "0.6.2", features = ["colored"] }
log = "0.4.21"
humantime = "2.1.0"
tracing = { version = "0.1.40", features = ["log"] }
tracing-appender = { version = "0.2.3" }
tracing-subscriber = { version = "0.3.18", features = ["fmt", "tracing-log"] }

View File

@ -20,7 +20,7 @@ pub struct Args {
)]
log_level: Option<String>,
#[arg(long)]
log_file: Option<String>,
log_file: Option<PathBuf>,
}
impl Default for Args {
@ -59,7 +59,7 @@ impl Args {
self.log_level.clone()
}
pub fn get_log_file(&self) -> Option<String> {
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 crate::config::{args::Args, engine::Engine};
@ -20,7 +20,7 @@ pub struct Config {
pub engine: Option<Engine>,
pub log_level: Option<String>,
pub log_file: Option<String>
pub log_file: Option<PathBuf>
}
impl Default for Config {
@ -32,7 +32,7 @@ impl Default for Config {
engine: Some(Engine::default()),
log_level: Some("info".to_string()),
log_file: Some("saltfish.log".to_string()),
log_file: Some(PathBuf::from("saltfish.log")),
}
}
}

38
src/config/error.rs Normal file
View File

@ -0,0 +1,38 @@
use std::io;
use crate::errors::ErrorType;
#[derive(Debug)]
pub enum ConfigError {
Arguments(clap::error::Error),
ConfigFile(io::Error),
Merging(String)
}
impl ConfigError {
fn throw(self) -> ErrorType {
match self {
Self::Arguments(error) => {
ErrorType::new(
"Parsing Arguments".to_string(),
error.to_string(),
false
)
},
Self::ConfigFile(error) => {
ErrorType::new(
"Reading Config File".to_string(),
error.to_string(),
false
)
},
Self::Merging(error) => {
ErrorType::new(
"Reading Config File".to_string(),
error,
false
)
}
}
}
}

View File

@ -1,22 +1,27 @@
mod config;
use std::str::FromStr;
mod args;
mod engine;
use crate::logging::logger_init;
pub mod error;
use std::str::FromStr;
use crate::logger_update;
use self::{args::Args, config::Config};
use clap::Parser;
use tracing::{debug, error, info, Level};
pub fn get_config() -> Option<Config> {
let args = Args::try_parse().or_else(|err|{
if err.use_stderr() {
if ! err.use_stderr() {
println!("{}", err);
return Err(())
}
error!("{}", err);
info!("continuing withouth commandline arguments");
Ok(Args::default())
});
@ -25,7 +30,7 @@ pub fn get_config() -> Option<Config> {
Ok(args) => {
let config_path = args.get_config_path().unwrap_or_default();
let config = Config::read(&config_path).merge_args(&args);
logger_init(config.log_file.clone().unwrap(), log::Level::from_str(&config.log_level.clone().unwrap()).unwrap()).unwrap();
logger_update(&config.log_file.clone().unwrap(), Level::from_str(&config.log_level.clone().unwrap()).unwrap());
Some(config)
}
}

23
src/errors.rs Normal file
View File

@ -0,0 +1,23 @@
use crate::config::error::ConfigError;
#[derive(Debug)]
pub struct ErrorType {
kind: String,
message: String,
fatal: bool
}
impl ErrorType {
pub fn new(kind: String, message: String, fatal: bool) -> Self {
ErrorType { kind, message, fatal }
}
}
#[derive(Debug)]
pub enum SaltfishError {
ConfigError(ConfigError)
}

View File

@ -1,18 +1,27 @@
use std::time::SystemTime;
use std::path::Path;
use tracing::Level;
use tracing_subscriber::fmt;
pub fn logger_init() {
let subscriber = fmt()
.compact()
.with_max_level(Level::DEBUG)
.finish();
// tracing::subscriber::set_global_default(subscriber)
// .expect("Failed to set global default subscriber");
pub fn logger_init(file: String, level: log::Level) -> Result<(), fern::InitError> {
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
humantime::format_rfc3339_seconds(SystemTime::now()),
record.level(),
record.target(),
message
))
})
.level(level.to_level_filter())
.chain(fern::log_file(file)?)
.apply()?;
Ok(())
}
pub fn logger_update(file: &Path, level: Level) {
let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap());
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

@ -5,15 +5,22 @@
use crate::config::get_config;
use log::trace;
mod chess;
mod config;
mod errors;
mod logging;
use crate::logging::*;
fn main() {
let config = get_config().unwrap();
trace!("{:?}", config);
// logger_init();
let config = get_config();
if config.is_none() {
return
}
println!("2");
}