diff --git a/meli.conf.5 b/meli.conf.5 index 9a54d935..efa1c91f 100644 --- a/meli.conf.5 +++ b/meli.conf.5 @@ -49,7 +49,27 @@ can have nested configuration files by using the following include macro: .Dl include(\&"/path/to/file\&") .Sh SECTIONS -The top level sections of the config are accounts, shortcuts, notifications, pager, listing, composing, pgp, terminal. +The top level sections of the config are: +.Bl -bullet -compact +.It +accounts +.It +shortcuts +.It +notifications +.It +pager +.It +listing +.It +composing +.It +pgp +.It +terminal +.It +log +.El .Sh EXAMPLES example configuration .sp @@ -703,6 +723,40 @@ theme = "themeB" \&... .Ed .El +.Sh LOG +.Bl -tag -width 36n +.It Ic log_file Ar String +(optional) path of the log file +.\" default value +.Pq Pa $XDG_DATA_HOME/meli/meli.log +.It Ic maximum_level Ar String +(optional) maximum level of messages to log. All levels less or equal to the +.Ic maximum_level +will be appended to the log file. Available levels are, in partial order: +.Bl -bullet -compact +.It +.Em OFF +.It +.Em FATAL +.It +.Em ERROR +.It +.Em WARN +.It +.Em INFO +.It +.Em DEBUG +.It +.Em TRACE +.El +This means that to turn logging off, set +.Ic maximum_level +to +.Em OFF Ns +\&. +.\" default value +.Pq Em INFO +.El .Sh SEE ALSO .Xr meli 1 , .Xr meli-themes 5 diff --git a/melib/src/logging.rs b/melib/src/logging.rs index cef2fdb9..17aaee34 100644 --- a/melib/src/logging.rs +++ b/melib/src/logging.rs @@ -25,7 +25,7 @@ use std::io::{BufWriter, Write}; use std::path::PathBuf; use std::sync::{Arc, Mutex}; -#[derive(Copy, Clone, PartialEq, PartialOrd, Hash, Debug)] +#[derive(Copy, Clone, PartialEq, PartialOrd, Hash, Debug, Serialize, Deserialize)] pub enum LoggingLevel { OFF, FATAL, @@ -54,6 +54,12 @@ impl std::fmt::Display for LoggingLevel { } } +impl Default for LoggingLevel { + fn default() -> Self { + LoggingLevel::INFO + } +} + use LoggingLevel::*; struct LoggingBackend { @@ -63,15 +69,15 @@ struct LoggingBackend { thread_local!(static LOG: Arc> = Arc::new(Mutex::new({ 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(); -LoggingBackend { - dest: BufWriter::new(log_file), - level: INFO, -} -}))); + 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(); + LoggingBackend { + dest: BufWriter::new(log_file), + level: LoggingLevel::default(), + }})) +); pub fn log(val: String, level: LoggingLevel) { LOG.with(|f| { diff --git a/src/conf.rs b/src/conf.rs index 5e1f4521..4c098986 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -271,6 +271,8 @@ pub struct FileSettings { pub terminal: TerminalSettings, #[serde(default)] pub plugins: HashMap, + #[serde(default)] + pub log: LogSettings, } #[derive(Debug, Clone, Default)] @@ -308,6 +310,7 @@ pub struct Settings { pub pgp: PGPSettings, pub terminal: TerminalSettings, pub plugins: HashMap, + pub log: LogSettings, } impl FileSettings { @@ -465,6 +468,13 @@ impl Settings { s.insert(id, ac); } + if let Some(ref log_path) = fs.log.log_file { + melib::change_log_dest(log_path.into()); + } + if fs.log.maximum_level != melib::LoggingLevel::default() { + melib::change_log_level(fs.log.maximum_level); + } + Ok(Settings { accounts: s, pager: fs.pager, @@ -476,6 +486,7 @@ impl Settings { pgp: fs.pgp, terminal: fs.terminal, plugins: fs.plugins, + log: fs.log, }) } } @@ -798,3 +809,11 @@ mod pp { Ok(ret) } } + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct LogSettings { + #[serde(default)] + log_file: Option, + #[serde(default)] + maximum_level: melib::LoggingLevel, +} diff --git a/src/terminal/embed.rs b/src/terminal/embed.rs index 3b3b938f..6a076ece 100644 --- a/src/terminal/embed.rs +++ b/src/terminal/embed.rs @@ -19,7 +19,6 @@ * along with meli. If not, see . */ -use crate::split_command; use crate::terminal::position::*; use melib::log; use melib::ERROR;