diff --git a/Cargo.lock b/Cargo.lock index 4812ed7a..8ca8ad21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1263,13 +1263,14 @@ dependencies = [ "serde_yaml", "shell-escape", "similar-asserts", - "simplelog", "snap", "strum", "tempfile", + "time", "tokio", "toml", "tracing", + "tracing-error", "tracing-subscriber", "tracing-test", "whoami", @@ -1725,17 +1726,6 @@ dependencies = [ "similar", ] -[[package]] -name = "simplelog" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0" -dependencies = [ - "log", - "termcolor", - "time", -] - [[package]] name = "slab" version = "0.4.9" @@ -1832,15 +1822,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "terminal_size" version = "0.4.1" @@ -2077,6 +2058,7 @@ dependencies = [ "sharded-slab", "smallvec", "thread_local", + "time", "tracing", "tracing-core", "tracing-log", @@ -2294,15 +2276,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" -dependencies = [ - "windows-sys 0.59.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/pueue/Cargo.toml b/pueue/Cargo.toml index 6f8edf10..bd41b254 100644 --- a/pueue/Cargo.toml +++ b/pueue/Cargo.toml @@ -36,13 +36,20 @@ pueue-lib = { version = "0.27.0", path = "../pueue_lib" } serde.workspace = true serde_json.workspace = true shell-escape = "0.1" -simplelog = "0.12" snap.workspace = true strum.workspace = true tempfile = "3" +time = { version = "0.3.37", features = ["macros"] } tokio.workspace = true toml = "0.8" tracing.workspace = true +tracing-error = "0.2.1" +tracing-subscriber = { version = "0.3.19", features = [ + "env-filter", + "fmt", + "local-time", + "time", +] } [dev-dependencies] assert_cmd = "2" diff --git a/pueue/src/bin/pueue.rs b/pueue/src/bin/pueue.rs index a5796385..47f533a4 100644 --- a/pueue/src/bin/pueue.rs +++ b/pueue/src/bin/pueue.rs @@ -4,8 +4,6 @@ use clap::{CommandFactory, Parser}; use clap_complete::{generate, generate_to, shells}; use color_eyre::eyre::{bail, WrapErr}; use color_eyre::Result; -use simplelog::{Config, ConfigBuilder, LevelFilter, SimpleLogger, TermLogger, TerminalMode}; -use tracing::*; use pueue_lib::settings::Settings; @@ -22,6 +20,8 @@ use pueue::client::client::Client; /// Once all this is done, we init the [Client] struct and start the main loop via [Client::start]. #[tokio::main(flavor = "current_thread")] async fn main() -> Result<()> { + color_eyre::install()?; + // Parse commandline options. let opt = CliArguments::parse(); @@ -35,38 +35,11 @@ async fn main() -> Result<()> { } // Init the logger and set the verbosity level depending on the `-v` flags. - let level = match opt.verbose { - 0 => LevelFilter::Warn, - 1 => LevelFilter::Info, - 2 => LevelFilter::Debug, - _ => LevelFilter::Trace, - }; - - // Try to initialize the logger with the timezone set to the Local time of the machine. - let mut builder = ConfigBuilder::new(); - let logger_config = match builder.set_time_offset_to_local() { - Err(_) => { - warn!("Failed to determine the local time of this machine. Fallback to UTC."); - Config::default() - } - Ok(builder) => builder.build(), - }; - - // Init a terminal logger. If this fails for some reason, try fallback to a SimpleLogger - if TermLogger::init( - level, - logger_config.clone(), - TerminalMode::Stderr, - simplelog::ColorChoice::Auto, - ) - .is_err() - { - SimpleLogger::init(level, logger_config).unwrap(); - } + pueue::tracing::install_tracing(opt.verbose)?; // Try to read settings from the configuration file. let (mut settings, config_found) = - Settings::read(&opt.config).context("Failed to read configuration.")?; + Settings::read(&opt.config).wrap_err("Failed to read configuration.")?; // Load any requested profile. if let Some(profile) = &opt.profile { diff --git a/pueue/src/bin/pueued.rs b/pueue/src/bin/pueued.rs index d45ce5bf..f9c600d9 100644 --- a/pueue/src/bin/pueued.rs +++ b/pueue/src/bin/pueued.rs @@ -2,13 +2,13 @@ use std::process::Command; use clap::Parser; use color_eyre::Result; -use simplelog::{Config, ConfigBuilder, LevelFilter, SimpleLogger, TermLogger, TerminalMode}; -use tracing::*; use pueue::daemon::{cli::CliArguments, run}; #[tokio::main(flavor = "multi_thread", worker_threads = 4)] async fn main() -> Result<()> { + color_eyre::install()?; + // Parse commandline options. let opt = CliArguments::parse(); @@ -26,34 +26,7 @@ async fn main() -> Result<()> { } // Set the verbosity level of the logger. - let level = match opt.verbose { - 0 => LevelFilter::Warn, - 1 => LevelFilter::Info, - 2 => LevelFilter::Debug, - _ => LevelFilter::Trace, - }; - - // Try to initialize the logger with the timezone set to the Local time of the machine. - let mut builder = ConfigBuilder::new(); - let logger_config = match builder.set_time_offset_to_local() { - Err(_) => { - warn!("Failed to determine the local time of this machine. Fallback to UTC."); - Config::default() - } - Ok(builder) => builder.build(), - }; - - // Init a terminal logger. If this fails for some reason, try fallback to a SimpleLogger - if TermLogger::init( - level, - logger_config.clone(), - TerminalMode::Stderr, - simplelog::ColorChoice::Auto, - ) - .is_err() - { - SimpleLogger::init(level, logger_config).unwrap(); - } + pueue::tracing::install_tracing(opt.verbose)?; #[cfg(target_os = "windows")] { diff --git a/pueue/src/lib.rs b/pueue/src/lib.rs index 88568cea..71186534 100644 --- a/pueue/src/lib.rs +++ b/pueue/src/lib.rs @@ -11,9 +11,65 @@ pub(crate) mod prelude { } pub(crate) mod errors { - pub use color_eyre::eyre::{WrapErr, bail}; + pub use color_eyre::eyre::{bail, WrapErr}; pub use color_eyre::Result; } +pub mod tracing { + use crate::prelude::*; + use tracing::level_filters::LevelFilter; + use tracing_subscriber::{ + fmt::time::OffsetTime, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, + }; + + // idea: use local time zone? + pub fn install_tracing(verbosity: u8) -> Result<()> { + let mut pretty = false; + let level = match verbosity { + 0 => LevelFilter::WARN, + 1 => LevelFilter::INFO, + 2 => LevelFilter::DEBUG, + 3 => LevelFilter::TRACE, + _ => { + pretty = true; + LevelFilter::TRACE + } + }; + + type GenericLayer = Box + Send + Sync>; + let offset = time::UtcOffset::current_local_offset().expect("should get local offset!"); + let timer = OffsetTime::new( + offset, + time::macros::format_description!("[hour]:[minute]:[second]"), + ); + + let fmt_layer: GenericLayer<_> = match pretty { + false => Box::new(tracing_subscriber::fmt::layer().with_timer(timer)), + true => Box::new( + tracing_subscriber::fmt::layer() + .pretty() + .with_timer(timer) + .with_target(true) + .with_thread_ids(false) + .with_thread_names(true) + .with_level(true) + .with_ansi(true) + .with_span_events(tracing_subscriber::fmt::format::FmtSpan::ACTIVE), + ), + }; + let filter_layer = EnvFilter::builder() + .with_default_directive(level.into()) + .from_env() + .wrap_err("RUST_LOG env variable is invalid")?; + + tracing_subscriber::Registry::default() + .with(fmt_layer.with_filter(filter_layer)) + .with(tracing_error::ErrorLayer::default()) + .init(); + + Ok(()) + } +} + pub mod client; pub mod daemon;