From 67e86e4d74a745904a422f20a52c53312086f5a1 Mon Sep 17 00:00:00 2001 From: Mikhail Gorbachev Date: Sun, 6 Jun 2021 15:52:30 +0300 Subject: [PATCH] Refactor `LogError` --- src/errors.rs | 6 ++++-- src/logs.rs | 12 ++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index eb8cbe1..a479268 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -132,8 +132,10 @@ impl From for PaperoniError { pub enum LogError { #[error(transparent)] FlexiError(#[from] FlexiLoggerError), - #[error("Wrong log directory: {0}")] - LogDirectoryError(String), + #[error("Unable to get user directories for logging purposes")] + UserDirectoriesError, + #[error("Can't create log directory: {0}")] + CreateLogDirectoryError(#[from] std::io::Error), } #[derive(Debug, Error)] diff --git a/src/logs.rs b/src/logs.rs index e8f89de..526921d 100644 --- a/src/logs.rs +++ b/src/logs.rs @@ -166,12 +166,10 @@ pub fn init_logger( let formatted_timestamp = start_time.format("%Y-%m-%d_%H-%M-%S"); let mut logger = flexi_logger::Logger::with(log_spec); - if is_logging_to_file && (!paperoni_dir.is_dir() || !log_dir.is_dir()) { - if let Err(e) = fs::create_dir_all(&log_dir) { - return Err(Error::LogDirectoryError(format!("Unable to create paperoni directories on home directory for logging purposes\n{}",e))); - } - } if is_logging_to_file { + if !paperoni_dir.is_dir() || !log_dir.is_dir() { + fs::create_dir_all(&log_dir)?; + } logger = logger .directory(log_dir) .discriminant(formatted_timestamp.to_string()) @@ -181,9 +179,7 @@ pub fn init_logger( logger.start()?; Ok(()) } - None => Err(Error::LogDirectoryError( - "Unable to get user directories for logging purposes".to_string(), - )), + None => Err(Error::UserDirectoriesError), } }