summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-03-28 00:06:58 +0800
committerKeuin <[email protected]>2022-03-28 00:11:45 +0800
commitb611e395a2d002c0696c2c10e2f43b3f9013c6d9 (patch)
tree0d1e1ea4ce417022ac556b15330112aa2e7fed04 /src/config.rs
parentab4b8903ee9b009c3e1a188f1cfe09e39272d2ee (diff)
Decent logging. Configurable log level.v0.2.0
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs
index c5d07a7..99e5f3d 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -2,6 +2,7 @@ use std::fs::File;
use std::io::Read;
use serde_derive::Deserialize;
+use tracing::error;
#[derive(Deserialize)]
pub struct Config {
@@ -9,17 +10,26 @@ pub struct Config {
pub log_file: String,
pub db_file: String,
pub listen: String,
+ pub log_level: String,
}
impl Config {
// Read config file. Panic if any error occurs.
pub fn from_file(file_path: &str) -> Config {
let mut file = File::open(file_path)
- .unwrap_or_else(|_| panic!("cannot open file {}", file_path));
+ .unwrap_or_else(|err| {
+ error!("Cannot open config file {}: {:?}", file_path, err);
+ panic!();
+ });
let mut config = String::new();
file.read_to_string(&mut config)
- .unwrap_or_else(|_| panic!("cannot read config file {}", file_path));
- return serde_json::from_str(config.as_str())
- .expect("cannot decode config file in JSON");
+ .unwrap_or_else(|err| {
+ error!("Cannot read config file {}: {:?}.", file_path, err);
+ panic!();
+ });
+ return serde_json::from_str(config.as_str()).unwrap_or_else(|err| {
+ error!("Cannot decode config file: {:?}.", err);
+ panic!();
+ });
}
} \ No newline at end of file