Logging #26

Open
leon wants to merge 33 commits from Logging into main
Showing only changes of commit e4baaa5f45 - Show all commits

View File

@ -7,6 +7,7 @@ mod test;
use std::fmt::Display; use std::fmt::Display;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io;
use std::io::Write; use std::io::Write;
use config::log::LogSettings; use config::log::LogSettings;
@ -24,45 +25,49 @@ use LogMessageType::GenericWarn;
/** /**
* Logs the given message. * Logs the given message.
*
* # Panics
* Panics if readlock on [`config::CONFIG`] could not be acquired
* or if another error occurs, such as a full disk.
*/ */
pub fn log_message(msg: &LogMessage, conf: &LogSettings, file: &str, line: u32, column: u32) { pub fn log_message(msg: &LogMessage, conf: &LogSettings, file: &str, line: u32, column: u32) -> Result<(), io::Error> {
// Check if message may be logged according to config.
let Some(log_line) = log_to_str(msg, conf, file, line, column) else { let Some(log_line) = log_to_str(msg, conf, file, line, column) else {
return; return Ok(());
}; };
// May panic if file cannot be opened or written to. // Log to file
conf.path().as_ref().map_or_else( match conf.path().as_ref()
|| {}, {
|path| { None => {/* Do not log to file */}
let mut file = OpenOptions::new() Some(p) =>
{
let file = OpenOptions::new()
.write(true) .write(true)
.append(true) .append(true)
.create(true) .create(true)
.open(path) .open(p);
.unwrap_or_else(|_| panic!("Could not open log file: {path:#?}")); let mut file = match file
writeln!(file, "{log_line}") {
.unwrap_or_else(|_| panic!("Could not write log to file: {path:#?}")); Ok(f) => f,
}, Err(e) => return Err(e),
); };
match writeln!(file, "{log_line}")
{
Ok(_) => {},
Err(e) => return Err(e),
}
}
};
if msg.1 <= Warning && *conf.stderr() { if msg.1 <= Warning && *conf.stderr() {
// May panic if writing to stderr fails. let mut stdout = io::stdout().lock();
eprintln!("{log_line}"); return writeln!(stdout, "{log_line}");
} else if msg.1 >= Information && *conf.stdout() { } else if msg.1 >= Information && *conf.stdout() {
// May panic if writing to stdout fails. let mut stderr = io::stderr().lock();
println!("{log_line}"); return writeln!(stderr, "{log_line}")
} }
return Ok(());
} }
/** /**
* Return log line, if message may be logged according to [`config::log::LogSettings`]. * Return log line, if message may be logged according to [`config::log::LogSettings`].
* # Panics
* Panics if readlock on [`config::CONFIG`] could not be acquired
* or if another error occurs, such as a full disk.
*/ */
#[must_use] #[must_use]
pub fn log_to_str( pub fn log_to_str(
@ -103,11 +108,12 @@ macro_rules! log {
let conf = config::LOG_SETTINGS let conf = config::LOG_SETTINGS
.read() .read()
.unwrap_or_else(|_| panic!("Failed aqcuire read lock on config!")); .unwrap_or_else(|_| panic!("Failed aqcuire read lock on config!"));
log_message($msg, &*conf, file!(), line!(), column!()); let res = log_message($msg, &*conf, file!(), line!(), column!());
drop(conf); drop(conf);
res
}; };
($msg:expr, $config:expr) => { ($msg:expr, $config:expr) => {
log_message($msg, $config, file!(), line!(), column!()); log_message($msg, $config, file!(), line!(), column!())
}; };
} }