Skip to content

Commit

Permalink
Improve error reporing when the logger fails to create log dir
Browse files Browse the repository at this point in the history
Previously we would report this in "mentions" tab:

    Can't create logger: No such file or directory (os error 2)

With this patch:

    Could not create log directory "/home/omer/no/such/dir": No such file or directory (os error 2)

Not sure about the wording, but showing the path in the error message is helpful
to realize the typos or when a user reports the error to issue tracker.

(#214)
  • Loading branch information
osa1 committed Jun 22, 2020
1 parent 4af06df commit b7dfd9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 18 additions & 5 deletions libtiny_logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use std::fs;
use std::fs::{File, OpenOptions};
use std::io;
use std::io::Result;
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
Expand All @@ -19,8 +18,16 @@ pub struct Logger {
inner: Rc<RefCell<LoggerInner>>,
}

#[derive(Debug)]
pub enum LoggerInitError {
CouldNotCreateDir { dir_path: PathBuf, err: io::Error },
}

impl Logger {
pub fn new(log_dir: PathBuf, report_err: Box<dyn Fn(String)>) -> Result<Logger> {
pub fn new(
log_dir: PathBuf,
report_err: Box<dyn Fn(String)>,
) -> Result<Logger, LoggerInitError> {
Ok(Logger {
inner: Rc::new(RefCell::new(LoggerInner::new(log_dir, report_err)?)),
})
Expand Down Expand Up @@ -90,7 +97,7 @@ struct ServerLogs {
users: HashMap<String, File>,
}

fn print_header(fd: &mut File) -> Result<()> {
fn print_header(fd: &mut File) -> io::Result<()> {
writeln!(fd)?;
writeln!(
fd,
Expand Down Expand Up @@ -132,10 +139,16 @@ fn try_open_log_file(path: &PathBuf, report_err: &dyn Fn(String)) -> Option<File
}

impl LoggerInner {
fn new(log_dir: PathBuf, report_err: Box<dyn Fn(String)>) -> Result<LoggerInner> {
fn new(
log_dir: PathBuf,
report_err: Box<dyn Fn(String)>,
) -> Result<LoggerInner, LoggerInitError> {
if let Err(err) = fs::create_dir(&log_dir) {
if err.kind() != io::ErrorKind::AlreadyExists {
return Err(err);
return Err(LoggerInitError::CouldNotCreateDir {
dir_path: log_dir,
err,
});
}
}

Expand Down
9 changes: 5 additions & 4 deletions tiny/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod ui;
mod utils;

use libtiny_client::{Client, ServerInfo};
use libtiny_logger::Logger;
use libtiny_logger::{Logger, LoggerInitError};
use libtiny_tui::{MsgTarget, TUI};
use libtiny_ui::UI;

Expand Down Expand Up @@ -121,11 +121,12 @@ fn run(
};
let logger: Option<Logger> =
log_dir.and_then(|log_dir| match Logger::new(log_dir, report_logger_error) {
Err(err) => {
Err(LoggerInitError::CouldNotCreateDir { dir_path, err }) => {
tui.add_client_err_msg(
&format!("Can't create logger: {}", err),
&MsgTarget::CurrentTab,
&format!("Could not create log directory {:?}: {}", dir_path, err),
&MsgTarget::Server { serv: "mentions" },
);
tui.draw();
None
}
Ok(logger) => {
Expand Down

0 comments on commit b7dfd9c

Please sign in to comment.