Skip to content

Commit

Permalink
surface file i/o errors with config and sqlite.db
Browse files Browse the repository at this point in the history
    add filename and line number to log messages
    create parent directory, ignoring errors when doing so
  • Loading branch information
drewwells committed Nov 29, 2024
1 parent b74d5f1 commit 08033b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
33 changes: 23 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,40 @@ pub fn init_runtime () -> std::io::Result<Runtime> {
}
}

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() {
Some(config_path) => {
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),
))
}
}
}
Expand All @@ -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(())
}
}
Expand Down
26 changes: 18 additions & 8 deletions src/database/sqlite.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> std::io::Result<Connection> {
pub fn init(database_file: &Option<String>) -> std::io::Result<Connection> {
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(
Expand Down

0 comments on commit 08033b8

Please sign in to comment.