Compare commits
No commits in common. "beb337cefd817938bc5023be30c3390be49f6845" and "dd5bfff7d2f0859e92ca1ca1b068638e3eb1333c" have entirely different histories.
beb337cefd
...
dd5bfff7d2
@ -7,7 +7,6 @@ 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;
|
||||||
@ -25,49 +24,45 @@ 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) -> Result<(), io::Error> {
|
pub fn log_message(msg: &LogMessage, conf: &LogSettings, file: &str, line: u32, column: u32) {
|
||||||
// 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 Ok(());
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Log to file
|
// May panic if file cannot be opened or written to.
|
||||||
match conf.path().as_ref()
|
conf.path().as_ref().map_or_else(
|
||||||
{
|
|| {},
|
||||||
None => {/* Do not log to file */}
|
|path| {
|
||||||
Some(p) =>
|
let mut file = OpenOptions::new()
|
||||||
{
|
|
||||||
let file = OpenOptions::new()
|
|
||||||
.write(true)
|
.write(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(p);
|
.open(path)
|
||||||
let mut file = match file
|
.unwrap_or_else(|_| panic!("Could not open log file: {path:#?}"));
|
||||||
{
|
writeln!(file, "{log_line}")
|
||||||
Ok(f) => f,
|
.unwrap_or_else(|_| panic!("Could not write log to file: {path:#?}"));
|
||||||
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() {
|
||||||
let mut stdout = io::stdout().lock();
|
// May panic if writing to stderr fails.
|
||||||
return writeln!(stdout, "{log_line}");
|
eprintln!("{log_line}");
|
||||||
} else if msg.1 >= Information && *conf.stdout() {
|
} else if msg.1 >= Information && *conf.stdout() {
|
||||||
let mut stderr = io::stderr().lock();
|
// May panic if writing to stdout fails.
|
||||||
return writeln!(stderr, "{log_line}")
|
println!("{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(
|
||||||
@ -108,12 +103,11 @@ 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!"));
|
||||||
let res = log_message($msg, &*conf, file!(), line!(), column!());
|
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!());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,8 +123,6 @@ impl Display for LogMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Evaluate replacing the following with a trait, to decouple this component from the rest.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every possible Message, which may be logged. Grouped the following:
|
* Every possible Message, which may be logged. Grouped the following:
|
||||||
*
|
*
|
||||||
@ -190,4 +182,4 @@ impl Display for LogMessageType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user