Logging #26
@ -7,6 +7,7 @@ mod test;
|
||||
|
||||
use std::fmt::Display;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
use config::log::LogSettings;
|
||||
@ -24,45 +25,49 @@ use LogMessageType::GenericWarn;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
return;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// May panic if file cannot be opened or written to.
|
||||
conf.path().as_ref().map_or_else(
|
||||
|| {},
|
||||
|path| {
|
||||
let mut file = OpenOptions::new()
|
||||
// Log to file
|
||||
match conf.path().as_ref()
|
||||
{
|
||||
None => {/* Do not log to file */}
|
||||
Some(p) =>
|
||||
{
|
||||
let file = OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(path)
|
||||
.unwrap_or_else(|_| panic!("Could not open log file: {path:#?}"));
|
||||
writeln!(file, "{log_line}")
|
||||
.unwrap_or_else(|_| panic!("Could not write log to file: {path:#?}"));
|
||||
},
|
||||
);
|
||||
.open(p);
|
||||
let mut file = match file
|
||||
{
|
||||
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() {
|
||||
// May panic if writing to stderr fails.
|
||||
eprintln!("{log_line}");
|
||||
let mut stdout = io::stdout().lock();
|
||||
return writeln!(stdout, "{log_line}");
|
||||
} else if msg.1 >= Information && *conf.stdout() {
|
||||
// May panic if writing to stdout fails.
|
||||
println!("{log_line}");
|
||||
let mut stderr = io::stderr().lock();
|
||||
return writeln!(stderr, "{log_line}")
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]
|
||||
pub fn log_to_str(
|
||||
@ -103,11 +108,12 @@ macro_rules! log {
|
||||
let conf = config::LOG_SETTINGS
|
||||
.read()
|
||||
.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);
|
||||
res
|
||||
};
|
||||
($msg:expr, $config:expr) => {
|
||||
log_message($msg, $config, file!(), line!(), column!());
|
||||
log_message($msg, $config, file!(), line!(), column!())
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user