From f79e97f5c3cae593aefaf7b02664e961572092c8 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 26 May 2020 21:48:36 -0400 Subject: removed panics from logging initialization --- src/logging.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/logging.rs b/src/logging.rs index 346a7ab..5d3f378 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -2,19 +2,25 @@ use std::fs::OpenOptions; use simplelog::*; +use crate::conf; use crate::error; +use crate::user; -pub fn init(path: &str) -> error::Result<()> { - let logfile = match OpenOptions::new().append(true).create(true).open(path) { - Err(e) => { - panic!("Could not open log file: {}", e); - } - Ok(f) => f, - }; +pub fn checked_init() { + let logfile = format!("/tmp/clinte_{}.log", *user::NAME); - if let Err(e) = WriteLogger::init(LevelFilter::Info, Config::default(), logfile) { - panic!("Could not initiate logging: {}", e); + if let Err(e) = init(&logfile) { + log::error!("Couldn't initialize logging. Exiting."); + if *conf::DEBUG { + log::error!("--> {}", e); + } + std::process::exit(1); } +} + +fn init(path: &str) -> error::Result<()> { + let logfile = OpenOptions::new().append(true).create(true).open(path)?; + WriteLogger::init(LevelFilter::Info, Config::default(), logfile)?; Ok(()) } -- cgit 1.4.1-2-gfad0