Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add verbosity flag and debug log #118

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ tempfile = "3.5.0"
tokio = { version = "1.27.0", features = ["macros", "rt-multi-thread"] }
toml_edit = { version = "0.19.8", features = ["serde"] }
tracing = "0.1.37"
clap-verbosity-flag = "2.0.1"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

[profile.release-lto]
inherits = "release"
Expand Down
33 changes: 32 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::util::IndicatifWriter;
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use clap_verbosity_flag::Verbosity;

use crate::environment::get_up_to_date_prefix;
use crate::Project;
use crate::{environment::get_up_to_date_prefix, progress};
use anyhow::Error;
use tracing_subscriber::{filter::LevelFilter, util::SubscriberInitExt, EnvFilter};

mod add;
mod global;
Expand All @@ -15,6 +18,11 @@ mod run;
struct Args {
#[command(subcommand)]
command: Option<Command>,

/// The verbosity level
/// (-v for verbose, -vv for debug, -vvv for trace, -q for quiet)
#[command(flatten)]
verbose: Verbosity,
}

/// Generates a completion script for a shell.
Expand Down Expand Up @@ -65,6 +73,29 @@ async fn default() -> Result<(), Error> {
pub async fn execute() -> anyhow::Result<()> {
let args = Args::parse();

let level_filter = match args.verbose.log_level_filter() {
clap_verbosity_flag::LevelFilter::Off => LevelFilter::OFF,
clap_verbosity_flag::LevelFilter::Error => LevelFilter::ERROR,
clap_verbosity_flag::LevelFilter::Warn => LevelFilter::WARN,
clap_verbosity_flag::LevelFilter::Info => LevelFilter::INFO,
clap_verbosity_flag::LevelFilter::Debug => LevelFilter::DEBUG,
clap_verbosity_flag::LevelFilter::Trace => LevelFilter::TRACE,
};

let env_filter = EnvFilter::builder()
.with_default_directive(level_filter.into())
.from_env()?
// filter logs from apple codesign because they are very noisy
.add_directive("apple_codesign=off".parse()?);

// Setup the tracing subscriber
tracing_subscriber::fmt()
.with_env_filter(env_filter)
.with_writer(IndicatifWriter::new(progress::global_multi_progress()))
.without_time()
.finish()
.try_init()?;

match args.command {
Some(Command::Completion(cmd)) => completion(cmd),
Some(Command::Init(cmd)) => init::execute(cmd).await,
Expand Down
2 changes: 2 additions & 0 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub async fn execute(args: Args) -> anyhow::Result<()> {
command.write_invoke_script(&mut script, &shell, &project, &activator_result)?;
}

tracing::debug!("Activation script:\n{}", script);

// Write the contents of the script to a temporary file that we can execute with the shell.
let mut temp_file = tempfile::Builder::new()
.suffix(&format!(".{}", shell.extension()))
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod prefix;
mod progress;
mod project;
mod repodata;
mod util;
mod virtual_packages;

pub use project::Project;
Expand Down
32 changes: 32 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use indicatif::MultiProgress;
use std::io;
use tracing_subscriber::fmt::MakeWriter;

#[derive(Clone)]
pub struct IndicatifWriter {
progress_bars: MultiProgress,
}

impl IndicatifWriter {
pub(crate) fn new(pb: MultiProgress) -> Self {
Self { progress_bars: pb }
}
}

impl io::Write for IndicatifWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.progress_bars.suspend(|| io::stderr().write(buf))
}

fn flush(&mut self) -> io::Result<()> {
self.progress_bars.suspend(|| io::stderr().flush())
}
}

impl<'a> MakeWriter<'a> for IndicatifWriter {
type Writer = IndicatifWriter;

fn make_writer(&'a self) -> Self::Writer {
self.clone()
}
}