diff --git a/src/config/mod.rs b/src/config/mod.rs index b832cb0..cc423fb 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -100,14 +100,23 @@ pub fn init_runtime () -> std::io::Result { } } -pub fn init_configs (cmd : Cmd) -> std::io::Result<()> { +pub fn init_configs(cmd: Cmd) -> std::io::Result<()> { //init logger env_logger::builder() - .format(|buf,rec| { + .format(|buf, rec| { let style = buf.default_level_style(rec.level()); - writeln!(buf, "[{} {style}{}{style:#} librespeed_rs] {}",current_formatted_time(),rec.level(), rec.args()) + writeln!( + buf, + "[{} {style}{}{style:#} {}:{}] {}", + current_formatted_time(), + rec.level(), + rec.file().unwrap_or("unknown"), + rec.line().unwrap_or(0), + rec.args() + ) }) - .filter_level(LevelFilter::Info).init(); + .filter_level(LevelFilter::Info) + .init(); println!("{HEAD_ART}"); //find server configs match cmd.server_config_path.clone() { @@ -115,12 +124,16 @@ pub fn init_configs (cmd : Cmd) -> std::io::Result<()> { let config = open_config_file(&config_path); match config { Ok(config) => { - initialize(config,cmd)?; - info!("Configs initialized file : {}",config_path); + initialize(config, cmd)?; + info!("Configs initialized file : {}", config_path); Ok(()) } Err(e) => { - Err(Error::new(ErrorKind::Other,e)) + trace!("Failed to load config from {}: {}", config_path, e); + Err(Error::new( + ErrorKind::Other, + format!("Could not load config from {}: {}", config_path, e), + )) } } } @@ -129,16 +142,16 @@ pub fn init_configs (cmd : Cmd) -> std::io::Result<()> { match config { // open config from current dir Ok(config) => { - initialize(config,cmd)?; + initialize(config, cmd)?; info!("Configs initialized file : configs.toml"); Ok(()) } // set default config Err(e) => { let config = ServerConfig::default(); - initialize(config,cmd)?; + initialize(config, cmd)?; info!("Configs initialized with defaults"); - trace!("Load config default path error : {}",e); + trace!("Load config default path error : {}", e); Ok(()) } } diff --git a/src/database/sqlite.rs b/src/database/sqlite.rs index f2ed513..df44678 100644 --- a/src/database/sqlite.rs +++ b/src/database/sqlite.rs @@ -1,19 +1,29 @@ -use std::io::{Error, ErrorKind}; -use rusqlite::{Connection, Row}; -use crate::database::{Database, DBRawToStruct}; +use crate::database::{DBRawToStruct, Database}; use crate::results::TelemetryData; +use rusqlite::{Connection, Row}; +use std::io::{Error, ErrorKind}; +use std::path::PathBuf; pub struct SQLite { pub connection: Connection, } -pub fn init (database_file : &Option) -> std::io::Result { +pub fn init(database_file: &Option) -> std::io::Result { match database_file { - None => { - Err(Error::new(ErrorKind::Other,"Error setup sqlite invalid database file.")) - } + None => Err(Error::new( + ErrorKind::Other, + "Error setup sqlite invalid database file.", + )), Some(database_file) => { - let connection = Connection::open(database_file); + let mut database_path = PathBuf::from(database_file); + if !database_path.is_absolute() { + database_path = PathBuf::from(std::env::current_dir().unwrap().join(database_file)); + } + + // attempt to create the parent directory and ignore errors if it already exists + let _ = std::fs::create_dir_all(database_path.parent().unwrap()); + + let connection = Connection::open(database_path); match connection { Ok(connection) => { let create_table = connection.execute(