Switch to tracing
This commit is contained in:
parent
ae1f6a2b3f
commit
e3b220a248
5 changed files with 194 additions and 161 deletions
|
|
@ -13,12 +13,12 @@ license = { workspace = true }
|
|||
anyhow = { workspace = true, features = ["std"] }
|
||||
bpaf = { workspace = true, features = [] }
|
||||
# derive_more = { workspace = true, features = ["std"] }
|
||||
env_logger = { workspace = true, features = ["auto-color", "humantime", "unstable-kv"] }
|
||||
indexmap = { workspace = true, features = ["std"] }
|
||||
log = { workspace = true, features = ["kv"] }
|
||||
rgb = { workspace = true, features = [] }
|
||||
shell-words = { workspace = true, features = ["std"] }
|
||||
strum = { workspace = true, features = ["derive", "std"] }
|
||||
tracing = { workspace = true, features = ["attributes", "std"] }
|
||||
tracing-subscriber = { workspace = true, features = ["ansi", "fmt", "smallvec", "std", "tracing-log"] }
|
||||
|
||||
[features]
|
||||
default = ["autocomplete"]
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
use anyhow::{Context, Result};
|
||||
use hyfetch::cli_options::options;
|
||||
use hyfetch::neofetch_util::get_distro_ascii;
|
||||
use log::debug;
|
||||
use tracing::debug;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
env_logger::init();
|
||||
|
||||
let options = options().fallback_to_usage().run();
|
||||
debug!(options:?; "CLI options");
|
||||
|
||||
init_tracing_subsriber(options.debug).context("Failed to init tracing subscriber")?;
|
||||
|
||||
debug!(?options, "CLI options");
|
||||
|
||||
// TODO
|
||||
|
||||
|
|
@ -23,3 +24,50 @@ fn main() -> Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_tracing_subsriber(debug: bool) -> Result<()> {
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::filter::{LevelFilter, Targets};
|
||||
use tracing_subscriber::fmt::Subscriber;
|
||||
use tracing_subscriber::layer::SubscriberExt as _;
|
||||
use tracing_subscriber::util::SubscriberInitExt as _;
|
||||
|
||||
let builder = Subscriber::builder();
|
||||
|
||||
// Remove the default max level filter from the subscriber; it will be added to
|
||||
// the `Targets` filter instead if no filter is set in `RUST_LOG`.
|
||||
// Replacing the default `LevelFilter` with an `EnvFilter` would imply this,
|
||||
// but we can't replace the builder's filter with a `Targets` filter yet.
|
||||
let builder = builder.with_max_level(LevelFilter::TRACE);
|
||||
|
||||
let subscriber = builder.finish();
|
||||
let subscriber = {
|
||||
let targets = match env::var("RUST_LOG") {
|
||||
Ok(var) => Targets::from_str(&var)
|
||||
.map_err(|e| {
|
||||
eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e);
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
Err(env::VarError::NotPresent) => {
|
||||
let targets = Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL);
|
||||
if debug {
|
||||
targets.with_target(env!("CARGO_CRATE_NAME"), Level::DEBUG)
|
||||
} else {
|
||||
targets
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Ignoring `RUST_LOG`: {}", e);
|
||||
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
|
||||
},
|
||||
};
|
||||
subscriber.with(targets)
|
||||
};
|
||||
|
||||
subscriber
|
||||
.try_init()
|
||||
.context("Failed to set the global default subscriber")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use std::process::Command;
|
|||
use std::{env, fmt};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::debug;
|
||||
use tracing::debug;
|
||||
|
||||
/// Gets the absolute path of the neofetch command.
|
||||
pub fn get_command_path() -> Result<PathBuf> {
|
||||
|
|
@ -44,6 +44,7 @@ pub fn get_command_path() -> Result<PathBuf> {
|
|||
Err(anyhow!("neofetch command not found"))
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
pub fn get_distro_ascii(distro: Option<String>) -> Result<String> {
|
||||
// TODO
|
||||
|
||||
|
|
@ -52,12 +53,13 @@ pub fn get_distro_ascii(distro: Option<String>) -> Result<String> {
|
|||
} else {
|
||||
get_distro_name().context("Failed to get distro name")?
|
||||
};
|
||||
debug!(distro:% = distro; "resolved distro name");
|
||||
debug!(distro, "resolved distro name");
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Runs neofetch command, returning the piped stdout output.
|
||||
#[tracing::instrument(level = "debug")]
|
||||
fn run_neofetch_command_piped<S>(args: &[S]) -> Result<String>
|
||||
where
|
||||
S: AsRef<OsStr> + fmt::Debug,
|
||||
|
|
@ -67,7 +69,7 @@ where
|
|||
let output = command
|
||||
.output()
|
||||
.context("Failed to execute neofetch as child process")?;
|
||||
debug!(output:?, args:?; "neofetch output");
|
||||
debug!(?output, "neofetch output");
|
||||
|
||||
if !output.status.success() {
|
||||
let err = if let Some(code) = output.status.code() {
|
||||
|
|
@ -113,6 +115,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
fn get_distro_name() -> Result<String> {
|
||||
run_neofetch_command_piped(&["ascii_distro_name"])
|
||||
.context("Failed to get distro name from neofetch")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue