Skip to content

Commit

Permalink
add: better tracing logs now
Browse files Browse the repository at this point in the history
  • Loading branch information
ActuallyHappening committed Feb 6, 2025
1 parent c879ea6 commit d42ad11
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 93 deletions.
33 changes: 3 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pueue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 4 additions & 31 deletions pueue/src/bin/pueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand All @@ -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 {
Expand Down
33 changes: 3 additions & 30 deletions pueue/src/bin/pueued.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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")]
{
Expand Down
58 changes: 57 additions & 1 deletion pueue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> = Box<dyn Layer<S> + 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;

0 comments on commit d42ad11

Please sign in to comment.