Refactor LogError

This commit is contained in:
Mikhail Gorbachev 2021-06-06 15:52:30 +03:00
parent aa9258e122
commit 67e86e4d74
2 changed files with 8 additions and 10 deletions

View file

@ -132,8 +132,10 @@ impl From<std::str::Utf8Error> for PaperoniError {
pub enum LogError { pub enum LogError {
#[error(transparent)] #[error(transparent)]
FlexiError(#[from] FlexiLoggerError), FlexiError(#[from] FlexiLoggerError),
#[error("Wrong log directory: {0}")] #[error("Unable to get user directories for logging purposes")]
LogDirectoryError(String), UserDirectoriesError,
#[error("Can't create log directory: {0}")]
CreateLogDirectoryError(#[from] std::io::Error),
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]

View file

@ -166,12 +166,10 @@ pub fn init_logger(
let formatted_timestamp = start_time.format("%Y-%m-%d_%H-%M-%S"); let formatted_timestamp = start_time.format("%Y-%m-%d_%H-%M-%S");
let mut logger = flexi_logger::Logger::with(log_spec); 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 is_logging_to_file {
if !paperoni_dir.is_dir() || !log_dir.is_dir() {
fs::create_dir_all(&log_dir)?;
}
logger = logger logger = logger
.directory(log_dir) .directory(log_dir)
.discriminant(formatted_timestamp.to_string()) .discriminant(formatted_timestamp.to_string())
@ -181,9 +179,7 @@ pub fn init_logger(
logger.start()?; logger.start()?;
Ok(()) Ok(())
} }
None => Err(Error::LogDirectoryError( None => Err(Error::UserDirectoriesError),
"Unable to get user directories for logging purposes".to_string(),
)),
} }
} }