diff --git a/src/display.rs b/src/display.rs index 2121c7f..f200d3e 100644 --- a/src/display.rs +++ b/src/display.rs @@ -5,8 +5,10 @@ use std::{ time, }; +use clap_verbosity_flag::{InfoLevel, Verbosity}; use console::style; use enum_dispatch::enum_dispatch; +use log::Level; use miette::{Context, IntoDiagnostic, Result}; use crate::{args::ProgressStyle, format_duration}; @@ -28,7 +30,17 @@ use crate::{args::ProgressStyle, format_duration}; pub const TICK_CHARS: &str = "⠷⠯⠟⠻⠽⠾⠿"; #[must_use] -pub fn current(progress: &ProgressStyle) -> Progress { +pub fn current(progress: &ProgressStyle, verbose: &Verbosity) -> Progress { + verbose + .log_level() + .map(|level| match level { + Level::Debug | Level::Trace => Progress::Plain(Plain::default()), + _ => current_style(progress), + }) + .unwrap_or_else(|| current_style(progress)) +} + +fn current_style(progress: &ProgressStyle) -> Progress { if match progress { ProgressStyle::Auto => atty::is(atty::Stream::Stdout), ProgressStyle::Fancy => true, diff --git a/src/git.rs b/src/git.rs index a870a9c..57925fa 100644 --- a/src/git.rs +++ b/src/git.rs @@ -51,7 +51,6 @@ impl fmt::Display for Tag { } } -#[tracing::instrument] pub async fn clone_fast(repo: &str, git_ref: &str, cwd: &Path) -> Result<()> { if cwd.exists() { let head_sha1 = String::from_utf8( @@ -88,7 +87,6 @@ pub async fn clone_fast(repo: &str, git_ref: &str, cwd: &Path) -> Result<()> { Ok(()) } -#[tracing::instrument] async fn fetch_and_checkout(cwd: &Path, git_ref: &str) -> Result<()> { Command::new("git") .env("GIT_TERMINAL_PROMPT", "0") @@ -104,7 +102,6 @@ async fn fetch_and_checkout(cwd: &Path, git_ref: &str) -> Result<()> { Ok(()) } -#[tracing::instrument] pub async fn fetch_tags(cwd: &Path) -> Result<()> { Command::new("git") .current_dir(cwd) @@ -114,7 +111,6 @@ pub async fn fetch_tags(cwd: &Path) -> Result<()> { .and(Ok(())) } -#[tracing::instrument] pub async fn tag_for_ref(cwd: &Path, git_ref: &str) -> Result { let output = Command::new("git") .current_dir(cwd) diff --git a/src/logging.rs b/src/logging.rs index 4425ae4..252e481 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -6,10 +6,8 @@ use std::{ use miette::{Context, IntoDiagnostic as _, Result}; use tracing::level_filters::LevelFilter; use tracing_appender::non_blocking::WorkerGuard; -#[cfg(debug_assertions)] -use tracing_error::ErrorLayer; use tracing_log::AsTrace; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +use tracing_subscriber::layer::SubscriberExt; use crate::{ args::{Args, LogColor}, @@ -26,46 +24,40 @@ pub fn init(args: &Args) -> Result { }; console::set_colors_enabled(color); let filter = args.verbose.log_level_filter().as_trace(); - let without_time = std::env::var("TSDL_LOG_TIME") - .map(|v| !matches!(v.to_lowercase().as_str(), "1" | "y" | "yes")) - .unwrap_or(true); let file = init_log_file(args)?; - Ok(init_tracing(file, color, filter, without_time)) + Ok(init_tracing(file, color, filter)) } -fn init_tracing(file: File, color: bool, filter: LevelFilter, without_time: bool) -> WorkerGuard { +fn init_tracing(file: File, color: bool, filter: LevelFilter) -> WorkerGuard { let (writer, guard) = tracing_appender::non_blocking(file); - let fmt_layer = tracing_subscriber::fmt::layer() + let stdout_layer = tracing_subscriber::fmt::layer() .compact() + .with_writer(std::io::stderr) .with_ansi(color) .with_file(true) .with_level(true) .with_line_number(true) .with_target(true) - .with_thread_ids(true) - .with_writer(writer); - if without_time { - let fmt_layer = fmt_layer.without_time(); - let registry = tracing_subscriber::registry().with(fmt_layer).with(filter); - #[cfg(debug_assertions)] - { - registry.with(ErrorLayer::default()).init(); - } - #[cfg(not(debug_assertions))] - { - registry.init(); - } + .with_thread_ids(false) + .without_time(); + let file_layer = tracing_subscriber::fmt::layer() + .compact() + .with_writer(writer) + .with_ansi(color) + .with_file(true) + .with_level(true) + .with_line_number(true) + .with_target(true) + .with_thread_ids(true); + if filter == LevelFilter::DEBUG || filter == LevelFilter::TRACE { + let subscriber = tracing_subscriber::registry() + .with(file_layer) + .with(stdout_layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); } else { - let registry = tracing_subscriber::registry().with(fmt_layer).with(filter); - #[cfg(debug_assertions)] - { - registry.with(ErrorLayer::default()).init(); - } - #[cfg(not(debug_assertions))] - { - registry.init(); - } - }; + let subscriber = tracing_subscriber::registry().with(file_layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + } guard } diff --git a/src/main.rs b/src/main.rs index 77b6e97..c7cbed0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn run(args: &args::Args) -> Result<()> { match &args.command { args::Command::Build(command) => build::run( &config::current(&args.config, Some(command))?, - display::current(&args.progress), + display::current(&args.progress, &args.verbose), ), args::Command::Config { command } => config::run(command, &args.config), } diff --git a/src/parser.rs b/src/parser.rs index 2f715bd..1f7af3f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -20,7 +20,6 @@ use crate::{ pub const NUM_STEPS: usize = 3; -#[tracing::instrument] pub async fn build_languages(languages: Vec) -> Result<()> { let buffer = if languages.is_empty() { 64 @@ -88,7 +87,6 @@ impl Language { } } - #[tracing::instrument(skip(self))] async fn process(&mut self, tx: mpsc::Sender>) { let res = self.steps().await; if res.is_err() { @@ -100,7 +98,6 @@ impl Language { } } - #[tracing::instrument(skip(self))] async fn steps(&mut self) -> Result<()> { self.handle.start(format!("Cloning {}", self.git_ref)); self.clone().await?; @@ -116,7 +113,6 @@ impl Language { Ok(()) } - #[tracing::instrument(skip(self))] async fn build_grammar(&self, dir: PathBuf) -> Result<()> { if self.build_script.is_none() { self.generate(&dir).await?; @@ -138,7 +134,6 @@ impl Language { Ok(()) } - #[tracing::instrument(skip(self))] async fn build(&self, dir: &Path) -> Result<()> { self.build_script .as_ref() @@ -263,7 +258,6 @@ impl Language { .and(Ok(())) } - #[tracing::instrument(skip(self))] async fn clone(&self) -> Result<()> { clone_fast(self.repo.as_str(), &self.git_ref, &self.build_dir) .await @@ -279,7 +273,6 @@ impl Language { }) } - #[tracing::instrument(skip(self))] async fn generate(&self, dir: &Path) -> Result<()> { Command::new(&*self.ts_cli) .current_dir(dir) diff --git a/src/sh.rs b/src/sh.rs index 02e9858..3e553e8 100644 --- a/src/sh.rs +++ b/src/sh.rs @@ -93,7 +93,6 @@ impl Script for Command { /// Your local hometown one-eyed which. /// /// stdin, stdout, and stderr are ignored. -#[tracing::instrument] pub async fn which(prog: &str) -> Result { let output = Command::new("which").arg(prog).exec().await?; Ok(PathBuf::from( @@ -101,12 +100,10 @@ pub async fn which(prog: &str) -> Result { )) } -#[tracing::instrument] pub async fn chmod_x(prog: &Path) -> Result { Command::new("chmod").arg("+x").arg(prog).exec().await } -#[tracing::instrument] pub async fn download(out: &Path, url: &str) -> Result { let which_prog = match which("curl").await { Ok(path) => Ok(path), @@ -126,7 +123,6 @@ pub async fn download(out: &Path, url: &str) -> Result { } } -#[tracing::instrument] pub async fn gunzip(gz: &Path) -> Result { Command::new("gunzip").arg(gz).exec().await } diff --git a/src/tree_sitter.rs b/src/tree_sitter.rs index 0a73b15..c17add6 100644 --- a/src/tree_sitter.rs +++ b/src/tree_sitter.rs @@ -20,7 +20,6 @@ use crate::{ }; #[allow(clippy::missing_panics_doc)] -#[tracing::instrument] pub async fn tag(repo: &str, version: &str) -> Result { let output = Command::new("git") .args(["ls-remote", "--refs", "--tags", repo]) @@ -51,7 +50,6 @@ pub async fn tag(repo: &str, version: &str) -> Result { )) } -#[tracing::instrument] async fn cli(args: &BuildCommand, tag: &Tag, handle: &ProgressHandle) -> Result { let build_dir = &args.build_dir; let platform = &args.tree_sitter.platform; @@ -81,7 +79,6 @@ async fn cli(args: &BuildCommand, tag: &Tag, handle: &ProgressHandle) -> Result< Ok(res) } -#[tracing::instrument] pub async fn prepare(args: &BuildCommand, progress: Arc>) -> Result { let mut handle = { progress