From 6197532bc7786b274260153f5b7a69a4e4bcdeca Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 8 Sep 2024 00:35:19 +0200 Subject: [PATCH] refactor(log): refactor the build function --- members/libpt-log/Cargo.toml | 9 +-- members/libpt-log/src/lib.rs | 130 +++++++++++------------------------ 2 files changed, 45 insertions(+), 94 deletions(-) diff --git a/members/libpt-log/Cargo.toml b/members/libpt-log/Cargo.toml index 412d214..a44975c 100644 --- a/members/libpt-log/Cargo.toml +++ b/members/libpt-log/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libpt-log" publish.workspace = true -version = "0.6.1" +version = "0.6.2-alpha.0" edition.workspace = true authors.workspace = true license.workspace = true @@ -13,12 +13,13 @@ keywords.workspace = true categories.workspace = true [dependencies] -tracing = "0.1.37" -tracing-appender = "0.2.2" -tracing-subscriber = "0.3.17" +tracing = "0.1.40" +tracing-appender = "0.2.3" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } anyhow = { workspace = true } thiserror = { workspace = true } libpt-core = { workspace = true, optional = false } +chrono = "0.4.38" [dev-dependencies] gag = "1.0.0" diff --git a/members/libpt-log/src/lib.rs b/members/libpt-log/src/lib.rs index 8e882e1..bdce04f 100644 --- a/members/libpt-log/src/lib.rs +++ b/members/libpt-log/src/lib.rs @@ -18,7 +18,7 @@ #![warn(clippy::pedantic, clippy::style, clippy::nursery)] use std::{ - fmt, + fmt::{self, Debug}, path::PathBuf, sync::atomic::{AtomicBool, Ordering}, }; @@ -31,8 +31,9 @@ use error::Error; /// I'm just repackaging it a little to make it more ergonomic pub use tracing; pub use tracing::{debug, error, info, trace, warn, Level}; -use tracing_appender::{self}; -use tracing_subscriber::fmt::{format::FmtSpan, time}; +use tracing_subscriber::{ + fmt::format::FmtSpan, layer::SubscriberExt as _, util::SubscriberInitExt, Layer, +}; use anyhow::{bail, Result}; /// The log level used when none is specified @@ -130,100 +131,59 @@ impl LoggerBuilder { /// This function will return an error if a global Logger was aready initialized. This module /// uses the [tracing] crate for logging, so if a [tracing] logger is initialized elsewhere, /// this method will error. + #[allow(clippy::missing_panics_doc)] pub fn build(self) -> Result { // only init if no init has been performed yet if INITIALIZED.load(Ordering::Relaxed) { warn!("trying to reinitialize the logger, ignoring"); bail!(Error::Usage("logging is already initialized".to_string())); } - let subscriber = tracing_subscriber::fmt::Subscriber::builder() - .with_level(self.display_level) - .with_max_level(self.max_level) + let layer = tracing_subscriber::fmt::layer() .with_ansi(self.ansi) .with_target(self.display_target) .with_file(self.display_filename) .with_thread_ids(self.display_thread_ids) .with_line_number(self.display_line_number) .with_thread_names(self.display_thread_names) - .with_span_events(self.span_events); - // HACK: somehow find a better solution for this - // I know this is hacky, but I couldn't get it any other way. I couldn't even find a - // project that could do it any other way. You can't apply one after another, because the - // type is changed every time. When using `Box`, some methods complain about - // not being in trait bounds. - match (self.log_to_file, self.show_time, self.pretty, self.uptime) { - (true, true, true, true) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .with_timer(time::uptime()) - .pretty() - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (true, true, true, false) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .pretty() - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (true, false, true, _) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .without_time() - .pretty() - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (true, true, false, true) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .with_timer(time::uptime()) - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (true, true, false, false) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (true, false, false, _) => { - let subscriber = subscriber - .with_writer(new_file_appender(self.log_dir)) - .without_time() - .finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, true, true, true) => { - let subscriber = subscriber.pretty().with_timer(time::uptime()).finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, true, true, false) => { - let subscriber = subscriber.pretty().with_timer(time::uptime()).finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, false, true, _) => { - let subscriber = subscriber.without_time().pretty().finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, true, false, true) => { - let subscriber = subscriber.with_timer(time::uptime()).finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, true, false, false) => { - let subscriber = subscriber.finish(); - tracing::subscriber::set_global_default(subscriber)?; - } - (false, false, false, _) => { - let subscriber = subscriber.without_time().finish(); - tracing::subscriber::set_global_default(subscriber)?; - } + .with_span_events(self.span_events.clone()) + .with_filter(tracing::level_filters::LevelFilter::from_level( + self.max_level, + )); + if self.log_to_file { + tracing_subscriber::registry() + .with(layer.and_then( + tracing_subscriber::fmt::layer().with_writer(self.logfile().unwrap()), + )) + .init(); + } else { + tracing_subscriber::registry().with(layer).init(); } + INITIALIZED.store(true, Ordering::Relaxed); Ok(Logger {}) } + fn logfile(&self) -> Option { + if !self.log_to_file { + return None; + } + let mut path = self.log_dir.clone(); + path.push(format!( + "{}.{}.log", + libpt_core::get_crate_name().unwrap_or_else(|| "logfile".to_string()), + chrono::Local::now().date_naive() + )); + let file = match std::fs::File::create(path) { + Ok(f) => f, + Err(e) => { + eprintln!("libpt: could not start logging to file: {e}"); + return None; + } + }; + + Some(file) + } + /// enable or disable logging to and creating of logfiles /// /// If you want to log to a file, don't forget to set [`Self::log_dir`]! @@ -490,13 +450,3 @@ impl Default for Logger { .expect("building a Logger failed") } } - -fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender { - tracing_appender::rolling::daily( - log_dir, - format!( - "{}.log", - libpt_core::get_crate_name().unwrap_or_else(|| "logfile".to_string()) - ), - ) -}