Logging #26

Open
leon wants to merge 33 commits from Logging into main
2 changed files with 30 additions and 30 deletions
Showing only changes of commit ba0a1fc641 - Show all commits

View File

@ -9,8 +9,8 @@ use std::fmt::Display;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use config::Config; use config::log::LogSettings;
use config::LogVerbosity; use config::log::LogVerbosity;
use chrono::Utc; use chrono::Utc;
@ -29,13 +29,13 @@ use LogMessageType::GenericWarn;
* Panics if readlock on [`config::CONFIG`] could not be acquired * Panics if readlock on [`config::CONFIG`] could not be acquired
* or if another error occurs, such as a full disk. * or if another error occurs, such as a full disk.
*/ */
pub fn log_message(msg: &LogMessage, conf: &Config, file: &str, line: u32, column: u32) { pub fn log_message(msg: &LogMessage, conf: &LogSettings, file: &str, line: u32, column: u32) {
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;
}; };
// May panic if file cannot be opened or written to. // May panic if file cannot be opened or written to.
conf.log_path().as_ref().map_or_else( conf.path().as_ref().map_or_else(
|| {}, || {},
|path| { |path| {
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
@ -49,17 +49,17 @@ pub fn log_message(msg: &LogMessage, conf: &Config, file: &str, line: u32, colum
}, },
); );
if msg.1 <= Warning && *conf.log_stderr() { if msg.1 <= Warning && *conf.stderr() {
// May panic if writing to stderr fails. // May panic if writing to stderr fails.
eprintln!("{log_line}"); eprintln!("{log_line}");
} else if msg.1 >= Information && *conf.log_stdout() { } else if msg.1 >= Information && *conf.stdout() {
// May panic if writing to stdout fails. // May panic if writing to stdout fails.
println!("{log_line}"); println!("{log_line}");
} }
} }
/** /**
* Return log line, if message may be logged according to [`config::Config`]. * Return log line, if message may be logged according to [`config::log::LogSettings`].
* # Panics * # Panics
* Panics if readlock on [`config::CONFIG`] could not be acquired * Panics if readlock on [`config::CONFIG`] could not be acquired
* or if another error occurs, such as a full disk. * or if another error occurs, such as a full disk.
@ -67,24 +67,24 @@ pub fn log_message(msg: &LogMessage, conf: &Config, file: &str, line: u32, colum
#[must_use] #[must_use]
pub fn log_to_str( pub fn log_to_str(
msg: &LogMessage, msg: &LogMessage,
conf: &Config, conf: &LogSettings,
file: &str, file: &str,
line: u32, line: u32,
column: u32, column: u32,
) -> Option<String> { ) -> Option<String> {
if conf.log_verbosity() < &msg.1 { if conf.verbosity() < &msg.1 {
return None; return None;
} }
let mut log_line = String::new(); let mut log_line = String::new();
// add time substring // add time substring
if *conf.log_time() { if *conf.time() {
log_line += &format!("{} ", Utc::now().format(conf.log_time_format())); log_line += &format!("{} ", Utc::now().format(conf.time_format()));
} }
// add code location substring // add code location substring
if *conf.log_location() { if *conf.location() {
log_line += &format!("{file}:{line},{column} "); log_line += &format!("{file}:{line},{column} ");
} }
@ -97,7 +97,7 @@ pub fn log_to_str(
#[macro_export] #[macro_export]
macro_rules! log { macro_rules! log {
($msg:expr) => { ($msg:expr) => {
let conf = config::CONFIG 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!()); log_message($msg, &*conf, file!(), line!(), column!());

View File

@ -42,8 +42,8 @@ pub fn log_macro_given_config()
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message(); let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message();
let mut config = config::DEFAULTS; let mut config = config::log::LogSettings::default();
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
.unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR)); .unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR));
@ -62,8 +62,8 @@ pub fn log_msg_file() {
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message(); let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message();
let mut config = config::DEFAULTS; let mut config = config::log::LogSettings::default();
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
.unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR)); .unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR));
@ -82,8 +82,8 @@ pub fn log_str() {
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message(); let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message();
let mut config = config::DEFAULTS; let mut config = config::log::LogSettings::default();
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
.unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR)); .unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR));
@ -111,9 +111,9 @@ pub fn verbosity_no_filter() {
LogMessageType::GenericInfo(String::from("Test Info")).log_message(), LogMessageType::GenericInfo(String::from("Test Info")).log_message(),
LogMessageType::GenericDebug(String::from("Test Debug")).log_message(), LogMessageType::GenericDebug(String::from("Test Debug")).log_message(),
]; ];
let mut config = config::DEFAULTS; let mut config = config::log::LogSettings::default();
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
config.set_log_verbosity(LogVerbosity::Error); config.set_verbosity(LogVerbosity::Error);
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
.unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR)); .unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR));
@ -143,8 +143,8 @@ pub fn verbosity_filter() {
LogMessageType::GenericInfo(String::from("Test Info")).log_message(), LogMessageType::GenericInfo(String::from("Test Info")).log_message(),
LogMessageType::GenericDebug(String::from("Test Debug")).log_message(), LogMessageType::GenericDebug(String::from("Test Debug")).log_message(),
]; ];
let mut config = config::DEFAULTS; let mut config = config::log::LogSettings::default();
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
.unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR)); .unwrap_or_else(|_| panic!("Could not create directory: {}", *LOG_DIR));
@ -187,11 +187,11 @@ fn log_macro_shared_config() {
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message(); let message = LogMessageType::GenericWarn(String::from("Test Log")).log_message();
let mut config = config::CONFIG let mut config = config::LOG_SETTINGS
.write() .write()
.expect("Could not acquire write lock on config!"); .expect("Could not acquire write lock on config!");
take(&mut *config); take(&mut *config);
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
drop(config); drop(config);
create_dir_all(PathBuf::from(LOG_DIR.as_str())) create_dir_all(PathBuf::from(LOG_DIR.as_str()))
@ -211,12 +211,12 @@ fn log_macro_shared_config() {
fn log_concurrently_any_order() { fn log_concurrently_any_order() {
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let mut config = config::CONFIG let mut config = config::LOG_SETTINGS
.write() .write()
.expect("Could not acquire write lock on config!"); .expect("Could not acquire write lock on config!");
take(&mut *config); take(&mut *config);
let mut messages = Vec::with_capacity(CONCURRENT_MESSAGE_COUNT); let mut messages = Vec::with_capacity(CONCURRENT_MESSAGE_COUNT);
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
drop(config); drop(config);
for i in 0..CONCURRENT_MESSAGE_COUNT { for i in 0..CONCURRENT_MESSAGE_COUNT {
@ -256,12 +256,12 @@ fn log_concurrently_any_order() {
fn log_concurrently_correct_order() { fn log_concurrently_correct_order() {
let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4()); let log_path = &format!("{}/{}", *LOG_DIR, Uuid::new_v4());
println!("Log Path: {log_path}"); println!("Log Path: {log_path}");
let mut config = config::CONFIG let mut config = config::LOG_SETTINGS
.write() .write()
.expect("Could not acquire write lock on config!"); .expect("Could not acquire write lock on config!");
take(&mut *config); take(&mut *config);
let mut messages = Vec::with_capacity(CONCURRENT_MESSAGE_COUNT); let mut messages = Vec::with_capacity(CONCURRENT_MESSAGE_COUNT);
config.set_log_path(PathBuf::from(log_path)); config.set_path(PathBuf::from(log_path));
drop(config); drop(config);
for i in 0..CONCURRENT_MESSAGE_COUNT { for i in 0..CONCURRENT_MESSAGE_COUNT {