conf: add options for logging

Add options for log file location and maximum log level. Also add
manpage entries for these options in `meli.conf.5`
memfd
Manos Pitsidianakis 2020-05-28 16:02:57 +03:00
parent 608ef9a946
commit bfff0e4feb
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
4 changed files with 90 additions and 12 deletions

View File

@ -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

View File

@ -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<Mutex<LoggingBackend>> = 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| {

View File

@ -271,6 +271,8 @@ pub struct FileSettings {
pub terminal: TerminalSettings,
#[serde(default)]
pub plugins: HashMap<String, Plugin>,
#[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<String, Plugin>,
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<PathBuf>,
#[serde(default)]
maximum_level: melib::LoggingLevel,
}

View File

@ -19,7 +19,6 @@
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::split_command;
use crate::terminal::position::*;
use melib::log;
use melib::ERROR;