Skip to content

Commit 08033b8

Browse files
author
Drew Wells
committed
surface file i/o errors with config and sqlite.db
add filename and line number to log messages create parent directory, ignoring errors when doing so
1 parent b74d5f1 commit 08033b8

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/config/mod.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,40 @@ pub fn init_runtime () -> std::io::Result<Runtime> {
100100
}
101101
}
102102

103-
pub fn init_configs (cmd : Cmd) -> std::io::Result<()> {
103+
pub fn init_configs(cmd: Cmd) -> std::io::Result<()> {
104104
//init logger
105105
env_logger::builder()
106-
.format(|buf,rec| {
106+
.format(|buf, rec| {
107107
let style = buf.default_level_style(rec.level());
108-
writeln!(buf, "[{} {style}{}{style:#} librespeed_rs] {}",current_formatted_time(),rec.level(), rec.args())
108+
writeln!(
109+
buf,
110+
"[{} {style}{}{style:#} {}:{}] {}",
111+
current_formatted_time(),
112+
rec.level(),
113+
rec.file().unwrap_or("unknown"),
114+
rec.line().unwrap_or(0),
115+
rec.args()
116+
)
109117
})
110-
.filter_level(LevelFilter::Info).init();
118+
.filter_level(LevelFilter::Info)
119+
.init();
111120
println!("{HEAD_ART}");
112121
//find server configs
113122
match cmd.server_config_path.clone() {
114123
Some(config_path) => {
115124
let config = open_config_file(&config_path);
116125
match config {
117126
Ok(config) => {
118-
initialize(config,cmd)?;
119-
info!("Configs initialized file : {}",config_path);
127+
initialize(config, cmd)?;
128+
info!("Configs initialized file : {}", config_path);
120129
Ok(())
121130
}
122131
Err(e) => {
123-
Err(Error::new(ErrorKind::Other,e))
132+
trace!("Failed to load config from {}: {}", config_path, e);
133+
Err(Error::new(
134+
ErrorKind::Other,
135+
format!("Could not load config from {}: {}", config_path, e),
136+
))
124137
}
125138
}
126139
}
@@ -129,16 +142,16 @@ pub fn init_configs (cmd : Cmd) -> std::io::Result<()> {
129142
match config {
130143
// open config from current dir
131144
Ok(config) => {
132-
initialize(config,cmd)?;
145+
initialize(config, cmd)?;
133146
info!("Configs initialized file : configs.toml");
134147
Ok(())
135148
}
136149
// set default config
137150
Err(e) => {
138151
let config = ServerConfig::default();
139-
initialize(config,cmd)?;
152+
initialize(config, cmd)?;
140153
info!("Configs initialized with defaults");
141-
trace!("Load config default path error : {}",e);
154+
trace!("Load config default path error : {}", e);
142155
Ok(())
143156
}
144157
}

src/database/sqlite.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
use std::io::{Error, ErrorKind};
2-
use rusqlite::{Connection, Row};
3-
use crate::database::{Database, DBRawToStruct};
1+
use crate::database::{DBRawToStruct, Database};
42
use crate::results::TelemetryData;
3+
use rusqlite::{Connection, Row};
4+
use std::io::{Error, ErrorKind};
5+
use std::path::PathBuf;
56

67
pub struct SQLite {
78
pub connection: Connection,
89
}
910

10-
pub fn init (database_file : &Option<String>) -> std::io::Result<Connection> {
11+
pub fn init(database_file: &Option<String>) -> std::io::Result<Connection> {
1112
match database_file {
12-
None => {
13-
Err(Error::new(ErrorKind::Other,"Error setup sqlite invalid database file."))
14-
}
13+
None => Err(Error::new(
14+
ErrorKind::Other,
15+
"Error setup sqlite invalid database file.",
16+
)),
1517
Some(database_file) => {
16-
let connection = Connection::open(database_file);
18+
let mut database_path = PathBuf::from(database_file);
19+
if !database_path.is_absolute() {
20+
database_path = PathBuf::from(std::env::current_dir().unwrap().join(database_file));
21+
}
22+
23+
// attempt to create the parent directory and ignore errors if it already exists
24+
let _ = std::fs::create_dir_all(database_path.parent().unwrap());
25+
26+
let connection = Connection::open(database_path);
1727
match connection {
1828
Ok(connection) => {
1929
let create_table = connection.execute(

0 commit comments

Comments
 (0)