hack log together for more configs

This commit is contained in:
Christoph J. Scherr 2024-01-16 14:04:21 +01:00
parent 39a21d64e9
commit aa560b0e05
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 123 additions and 48 deletions

View File

@ -20,15 +20,21 @@
use std::{ use std::{
fmt, fmt,
path::PathBuf, path::PathBuf,
sync::atomic::{AtomicBool, Ordering}, sync::atomic::{AtomicBool, Ordering}, ops::Deref,
}; };
pub mod error; pub mod error;
use error::*; use error::*;
pub use tracing::{debug, error, info, trace, warn, Level}; pub use tracing::{debug, error, info, trace, warn, Level};
use tracing_appender; use tracing_appender::{
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*}; self,
non_blocking::{NonBlocking, WorkerGuard},
};
use tracing_subscriber::{
fmt::{format::FmtSpan, time::{self, FormatTime}},
prelude::*,
};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
@ -77,6 +83,33 @@ impl Logger {
false, false,
false, false,
false, false,
false,
true,
false,
)
}
/// ## initializes the logger
///
/// Will enable the logger to be used. This is a version that shows less information,
/// useful in cases with only one sender to the logging framework.
///
/// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control
pub fn init_mini(max_level: Option<Level>) -> Result<()> {
Self::init_customized(
false,
PathBuf::from(DEFAULT_LOG_DIR),
true,
false,
true,
false,
max_level.unwrap_or(DEFAULT_LOG_LEVEL),
false,
false,
false,
false,
false,
false,
) )
} }
@ -94,58 +127,96 @@ impl Logger {
display_thread_ids: bool, display_thread_ids: bool,
display_thread_names: bool, display_thread_names: bool,
display_line_number: bool, display_line_number: bool,
pretty: bool,
show_time: bool,
uptime: bool, // uptime instead of system time
) -> Result<()> { ) -> Result<()> {
// only init if no init has been performed yet // only init if no init has been performed yet
if INITIALIZED.load(Ordering::Relaxed) { if INITIALIZED.load(Ordering::Relaxed) {
warn!("trying to reinitialize the logger, ignoring"); warn!("trying to reinitialize the logger, ignoring");
bail!(Error::Usage(format!("logging is already initialized"))); bail!(Error::Usage(format!("logging is already initialized")));
} else { }
let filter = tracing_subscriber::filter::FilterFn::new(|_metadata| { let subscriber = tracing_subscriber::fmt::Subscriber::builder()
// let mut filter = false;
//
// // if it's this lib, continue
// filter |= metadata.target().contains(env!("CARGO_PKG_NAME"));
// filter |= metadata.target().contains("libpt");
//
// // if it's another crate, only show above debug
// filter |= metadata.level() > &Level::DEBUG;
//
// filter
// FIXME: Make the filter customizable with sane defaults. Don't block the
// executing crate.
true
});
let basic_subscriber = tracing_subscriber::fmt::Subscriber::builder()
// subscriber configuration
.with_ansi(ansi)
.with_file(display_filename)
.with_level(display_level) .with_level(display_level)
.with_target(display_target)
.with_max_level(max_level) .with_max_level(max_level)
.with_ansi(ansi)
.with_target(display_target)
.with_file(display_filename)
.with_thread_ids(display_thread_ids) .with_thread_ids(display_thread_ids)
.with_line_number(display_line_number) .with_line_number(display_line_number)
.with_thread_names(display_thread_names) .with_thread_names(display_thread_names)
.with_span_events(FmtSpan::FULL) .with_span_events(FmtSpan::FULL);
//.pretty // too verbose and over multiple lines, a bit like python tracebacks // I know this is hacky, but I couldn't get it any other way. I couldn't even find a
.finish() // project that could do it any other way. You can't apply one after another, because the
// add layers // type is changed every time. When using Box<dyn Whatever>, some methods complain about
.with(filter); // not being in trait bounds.
// TODO: somehow find a better solution for this
if log_to_file { match (log_to_file, show_time, pretty, uptime) {
let file_appender = tracing_appender::rolling::daily(log_dir, "log"); (true, true, true, true) => {
let (file_writer, _guard) = tracing_appender::non_blocking(file_appender); let subscriber = subscriber
let layered_subscriber = basic_subscriber .with_writer(new_file_appender(log_dir))
.with(tracing_subscriber::fmt::Layer::default().with_writer(file_writer)); .with_timer(time::uptime())
tracing::subscriber::set_global_default(layered_subscriber)?; .pretty()
} else { .finish();
tracing::subscriber::set_global_default(basic_subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
}
(true, true, true, false) => {
let subscriber = subscriber
.with_writer(new_file_appender(log_dir))
.pretty()
.finish();
tracing::subscriber::set_global_default(subscriber)?;
}
(true, false, true, _) => {
let subscriber = subscriber
.with_writer(new_file_appender(log_dir))
.without_time()
.pretty()
.finish();
tracing::subscriber::set_global_default(subscriber)?;
}
(true, true, false, true) => {
let subscriber = subscriber.with_writer(new_file_appender(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(log_dir)).finish();
tracing::subscriber::set_global_default(subscriber)?;
}
(true, false, false, _) => {
let file_appender = tracing_appender::rolling::daily(log_dir.clone(), "log");
let (file_writer, _guard) = tracing_appender::non_blocking(file_appender);
let subscriber = subscriber.with_writer(file_writer).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)?;
}
} }
INITIALIZED.store(true, Ordering::Relaxed); INITIALIZED.store(true, Ordering::Relaxed);
Ok(()) Ok(())
} }
}
/// ## logging at [`Level::ERROR`] /// ## logging at [`Level::ERROR`]
pub fn error<T>(&self, printable: T) pub fn error<T>(&self, printable: T)
@ -199,3 +270,7 @@ impl fmt::Debug for Logger {
//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// //// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// //// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
fn new_file_appender(log_dir: PathBuf) -> NonBlocking {
let file_appender = tracing_appender::rolling::daily(log_dir.clone(), "log");
return tracing_appender::non_blocking(file_appender).0;
}