diff --git a/meli/docs/meli.1 b/meli/docs/meli.1 index f1898941..0937bbf3 100644 --- a/meli/docs/meli.1 +++ b/meli/docs/meli.1 @@ -95,6 +95,8 @@ Print location of configuration file that will be loaded on normal app startup. Print default theme keys and values in TOML syntax, to be used as a blueprint. .It Cm print-loaded-themes Print all loaded themes in TOML syntax. +.It Cm print-log-path +Print log file location. .It Cm view View mail from input file. .El diff --git a/meli/src/args.rs b/meli/src/args.rs index cc11a774..23ca0efa 100644 --- a/meli/src/args.rs +++ b/meli/src/args.rs @@ -76,6 +76,8 @@ pub enum SubCommand { #[structopt(display_order = 5)] /// print compile time feature flags of this binary CompiledWith, + /// Print log file location. + PrintLogPath, /// View mail from input file. View { #[structopt(value_name = "INPUT", parse(from_os_str))] diff --git a/meli/src/conf.rs b/meli/src/conf.rs index 6222260f..b9fd770a 100644 --- a/meli/src/conf.rs +++ b/meli/src/conf.rs @@ -585,7 +585,7 @@ pub struct Settings { pub terminal: TerminalSettings, pub log: LogSettings, #[serde(skip)] - _logger: StderrLogger, + pub _logger: StderrLogger, } impl Settings { diff --git a/meli/src/main.rs b/meli/src/main.rs index 3ae5f147..f5962ec4 100644 --- a/meli/src/main.rs +++ b/meli/src/main.rs @@ -157,6 +157,11 @@ fn run_app(opt: Opt) -> Result<()> { } return Ok(()); } + Some(SubCommand::PrintLogPath) => { + let settings = crate::conf::Settings::new()?; + println!("{}", settings._logger.log_dest().display()); + return Ok(()); + } None => {} } diff --git a/melib/src/utils/logging.rs b/melib/src/utils/logging.rs index 8302fa07..419c5466 100644 --- a/melib/src/utils/logging.rs +++ b/melib/src/utils/logging.rs @@ -20,9 +20,10 @@ */ #[cfg(not(test))] -use std::{fs::OpenOptions, path::PathBuf}; +use std::fs::{File, OpenOptions}; use std::{ io::{BufWriter, Write}, + path::PathBuf, sync::{ atomic::{AtomicU8, Ordering}, Arc, Mutex, @@ -133,12 +134,17 @@ pub enum Destination { None, } -#[derive(Clone)] -pub struct StderrLogger { +struct FileOutput { #[cfg(test)] - dest: Arc>>, + writer: BufWriter, #[cfg(not(test))] - dest: Arc>>, + writer: BufWriter, + path: PathBuf, +} + +#[derive(Clone)] +pub struct StderrLogger { + dest: Arc>, level: Arc, print_level: bool, print_module_names: bool, @@ -170,13 +176,23 @@ impl StderrLogger { #[cfg(not(test))] let logger = { - let data_dir = xdg::BaseDirectories::with_prefix("meli").unwrap(); - let log_file = OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */ - .create(true) /* a new file will be created if the file does not yet already exist.*/ - .read(true) - .open(data_dir.place_data_file("meli.log").unwrap()).unwrap(); + #[inline(always)] + fn __inline_err_wrap() -> Result<(PathBuf, File), Box> { + let data_dir = xdg::BaseDirectories::with_prefix("meli")?; + let path = data_dir.place_data_file("meli.log")?; + let log_file = OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */ + .create(true) /* a new file will be created if the file does not yet already exist.*/ + .read(true) + .open(&path)?; + Ok((path, log_file)) + } + let (path, log_file) = + __inline_err_wrap().expect("Could not create log file in XDG_DATA_DIR"); Self { - dest: Arc::new(Mutex::new(BufWriter::new(log_file))), + dest: Arc::new(Mutex::new(FileOutput { + writer: BufWriter::new(log_file), + path, + })), level: Arc::new(AtomicU8::new(level as u8)), print_level: true, print_module_names: true, @@ -190,7 +206,10 @@ impl StderrLogger { #[cfg(test)] let logger = { Self { - dest: Arc::new(Mutex::new(BufWriter::new(std::io::stderr()))), + dest: Arc::new(Mutex::new(FileOutput { + writer: BufWriter::new(std::io::stderr()), + path: PathBuf::new(), + })), level: Arc::new(AtomicU8::new(level as u8)), print_level: true, print_module_names: true, @@ -225,10 +244,17 @@ impl StderrLogger { let path = path.expand(); // expand shell stuff let mut dest = self.dest.lock().unwrap(); - *dest = BufWriter::new(OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */ + *dest = FileOutput { + writer: BufWriter::new(OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */ .create(true) /* a new file will be created if the file does not yet already exist.*/ .read(true) - .open(path).unwrap()); + .open(&path).unwrap()), + path + }; + } + + pub fn log_dest(&self) -> PathBuf { + self.dest.lock().unwrap().path.clone() } } @@ -284,7 +310,7 @@ impl Log for StderrLogger { (Destination::None | Destination::File, _) => { _ = self.dest.lock().ok().and_then(|mut d| { write( - &mut (*d), + &mut d.writer, record, (self.print_level, self.print_module_names), ) @@ -293,7 +319,7 @@ impl Log for StderrLogger { (Destination::Stderr, true) => { _ = self.dest.lock().ok().and_then(|mut d| { write( - &mut (*d), + &mut d.writer, record, (self.print_level, self.print_module_names), ) @@ -315,6 +341,9 @@ impl Log for StderrLogger { } fn flush(&self) { - self.dest.lock().ok().and_then(|mut w| w.flush().ok()); + self.dest + .lock() + .ok() + .and_then(|mut w| w.writer.flush().ok()); } }