Skip to content

Commit

Permalink
Make verbose flag a global option in forc (FuelLabs#2798)
Browse files Browse the repository at this point in the history
Closes FuelLabs#2305

Adds the `--verbose` flag to all forc commands and makes it so
`tracing::debug` logs shows when `-v` or `--verbose` is used, and
`tracing::trace` logs show when `-vv` is used.

If both `RUST_LOG` and the verbose flag are set, the verbose flag takes
precedence.

Tested with permutations of:
```
cargo run -p forc -- clean --verbose
cargo run -p forc -- init -vv
```

Co-authored-by: Joshua Batty <[email protected]>
Co-authored-by: Kaya Gökalp <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2022
1 parent 22ba988 commit 56dc0f4
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/bin/deploy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;

#[tokio::main]
pub async fn main() {
init_tracing_subscriber();
init_tracing_subscriber(None);
let command = DeployCommand::parse();
if let Err(err) = deploy(command).await {
tracing::error!("Error: {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/bin/run/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;

#[tokio::main]
async fn main() {
init_tracing_subscriber();
init_tracing_subscriber(None);
let command = RunCommand::parse();
if let Err(err) = run(command).await {
tracing::error!("Error: {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-explore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn clean() -> Result<()> {
}

async fn run(app: App) -> Result<()> {
init_tracing_subscriber();
init_tracing_subscriber(None);
let App { port, .. } = app;
let releases = get_github_releases().await?;
let release = releases
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct App {
}

fn main() {
init_tracing_subscriber();
init_tracing_subscriber(None);
if let Err(err) = run() {
error!("Error: {:?}", err);
std::process::exit(1);
Expand Down
29 changes: 22 additions & 7 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use sway_core::{error::LineCol, CompileError, CompileWarning, TreeType};
use sway_types::Spanned;
use sway_utils::constants;
use tracing::{Level, Metadata};
use tracing_subscriber::{filter::EnvFilter, fmt::MakeWriter};
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
fmt::MakeWriter,
};

pub mod restricted;

Expand Down Expand Up @@ -417,22 +420,34 @@ impl<'a> MakeWriter<'a> for StdioTracingWriter {
/// A subscriber built from default `tracing_subscriber::fmt::SubscriberBuilder` such that it would match directly using `println!` throughout the repo.
///
/// `RUST_LOG` environment variable can be used to set different minimum level for the subscriber, default is `INFO`.
pub fn init_tracing_subscriber() {
let filter = match env::var_os(LOG_FILTER) {
pub fn init_tracing_subscriber(verbosity: Option<u8>) {
let env_filter = match env::var_os(LOG_FILTER) {
Some(_) => EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided"),
None => EnvFilter::new("info"),
};

tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(filter)
let level_filter = verbosity.and_then(|verbosity| match verbosity {
1 => Some(LevelFilter::DEBUG), // matches --verbose or -v
2 => Some(LevelFilter::TRACE), // matches -vv
_ => None,
});

let builder = tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(env_filter)
.with_ansi(true)
.with_level(false)
.with_file(false)
.with_line_number(false)
.without_time()
.with_target(false)
.with_writer(StdioTracingWriter)
.init();
.with_writer(StdioTracingWriter);

// If verbosity is set, it overrides the RUST_LOG setting
if let Some(level_filter) = level_filter {
builder.with_max_level(level_filter).init();
} else {
builder.init();
}
}

#[cfg(all(feature = "uwu", any(target_arch = "x86", target_arch = "x86_64")))]
Expand Down
3 changes: 0 additions & 3 deletions forc/src/cli/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub struct Command {
/// Create a package with a library target (src/lib.sw).
#[clap(long)]
pub library: bool,
/// Use verbose output.
#[clap(short = 'v', long)]
pub verbose: bool,
/// Set the package name. Defaults to the directory name
#[clap(long)]
pub name: Option<String>,
Expand Down
5 changes: 0 additions & 5 deletions forc/src/cli/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub struct Command {
/// Set the package name. Defaults to the directory name
#[clap(long)]
pub name: Option<String>,
/// Use verbose output.
#[clap(short = 'v', long)]
pub verbose: bool,
/// The path at which the project directory will be created.
pub path: String,
}
Expand All @@ -39,7 +36,6 @@ pub(crate) fn exec(command: Command) -> Result<()> {
predicate,
library,
name,
verbose,
path,
} = command;

Expand All @@ -61,7 +57,6 @@ pub(crate) fn exec(command: Command) -> Result<()> {
script,
predicate,
library,
verbose,
name,
};

Expand Down
10 changes: 8 additions & 2 deletions forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use addr2line::Command as Addr2LineCommand;
use anyhow::{anyhow, Result};
pub use build::Command as BuildCommand;
pub use check::Command as CheckCommand;
use clap::Parser;
use clap::{Parser, Subcommand};
pub use clean::Command as CleanCommand;
pub use completions::Command as CompletionsCommand;
use forc_util::init_tracing_subscriber;
pub use init::Command as InitCommand;
pub use new::Command as NewCommand;
use parse_bytecode::Command as ParseBytecodeCommand;
Expand All @@ -26,9 +27,12 @@ struct Opt {
/// the command to run
#[clap(subcommand)]
command: Forc,
/// Use verbose output.
#[clap(short, long, parse(from_occurrences), global = true)]
verbose: u8,
}

#[derive(Debug, Parser)]
#[derive(Subcommand, Debug)]
enum Forc {
#[clap(name = "addr2line")]
Addr2Line(Addr2LineCommand),
Expand Down Expand Up @@ -59,6 +63,8 @@ enum Forc {

pub async fn run_cli() -> Result<()> {
let opt = Opt::parse();
init_tracing_subscriber(Some(opt.verbose));

match opt.command {
Forc::Addr2Line(command) => addr2line::exec(command),
Forc::Build(command) => build::exec(command),
Expand Down
2 changes: 0 additions & 2 deletions forc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use forc_util::init_tracing_subscriber;
use tracing::error;

#[tokio::main]
async fn main() {
init_tracing_subscriber();
if let Err(err) = forc::cli::run_cli().await {
error!("Error: {:?}", err);
std::process::exit(1);
Expand Down
36 changes: 14 additions & 22 deletions forc/src/ops/forc_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use sway_utils::constants;
use tracing::info;
use tracing::{debug, info};

fn print_welcome_message() {
let read_the_docs = format!(
Expand Down Expand Up @@ -54,12 +54,10 @@ pub fn init(command: InitCommand) -> Result<()> {
);
}

if command.verbose {
info!(
"\nUsing project directory at {}",
project_dir.canonicalize()?.display()
);
}
debug!(
"\nUsing project directory at {}",
project_dir.canonicalize()?.display()
);

let project_name = match command.name {
Some(name) => name,
Expand Down Expand Up @@ -143,12 +141,10 @@ pub fn init(command: InitCommand) -> Result<()> {
let harness_path = Path::new(&project_dir).join("tests").join("harness.rs");
fs::write(&harness_path, defaults::default_test_program(&project_name))?;

if command.verbose {
info!(
"\nCreated test harness at {}",
harness_path.canonicalize()?.display()
);
}
debug!(
"\nCreated test harness at {}",
harness_path.canonicalize()?.display()
);

// Ignore default `out` and `target` directories created by forc and cargo.
let gitignore_path = Path::new(&project_dir).join(".gitignore");
Expand All @@ -160,16 +156,12 @@ pub fn init(command: InitCommand) -> Result<()> {
.open(&gitignore_path)?;
gitignore_file.write_all(defaults::default_gitignore().as_bytes())?;

if command.verbose {
info!(
"\nCreated .gitignore at {}",
gitignore_path.canonicalize()?.display()
);
}
debug!(
"\nCreated .gitignore at {}",
gitignore_path.canonicalize()?.display()
);

if command.verbose {
info!("\nSuccessfully created {program_type}: {project_name}",);
}
debug!("\nSuccessfully created {program_type}: {project_name}",);

print_welcome_message();

Expand Down
2 changes: 1 addition & 1 deletion test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct TestDescription {
}

pub fn run(locked: bool, filter_regex: Option<&regex::Regex>) {
init_tracing_subscriber();
init_tracing_subscriber(None);

let configured_tests = discover_test_configs().unwrap_or_else(|e| {
panic!("Discovering tests {e}");
Expand Down

0 comments on commit 56dc0f4

Please sign in to comment.