Merge pull request #694 from Pauan/quiet

Adding in `--quiet` and `--log-level` flags
master
Pauan 5 years ago committed by GitHub
commit 846b989b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      docs/src/commands/index.md
  2. 15
      src/build/mod.rs
  3. 4
      src/install/mod.rs
  4. 12
      src/lib.rs
  5. 7
      src/main.rs
  6. 100
      src/progressbar.rs
  7. 10
      src/test/mod.rs
  8. 91
      tests/all/log_level.rs
  9. 1
      tests/all/main.rs

@ -14,3 +14,23 @@ a Rust-generated WebAssembly project.
[new]: ./new.html [new]: ./new.html
[build]: ./build.html [build]: ./build.html
[pack-pub]: ./pack-and-publish.html [pack-pub]: ./pack-and-publish.html
### Log levels
By default `wasm-pack` displays a lot of useful information.
You can cause it to display even *more* information by using `--verbose`, or you can silence *all* stdout by using `--quiet`.
You can also use `--log-level` to have fine-grained control over wasm-pack's log output:
* `--log-level info` is the default, it causes all messages to be logged.
* `--log-level warn` causes warnings and errors to be displayed, but not info.
* `--log-level error` causes only errors to be displayed.
These flags are global flags, so they can be used with every command, and they must come *before* the command:
```sh
wasm-pack --log-level error build
wasm-pack --quiet build
wasm-pack --verbose build
```

@ -80,8 +80,14 @@ pub fn cargo_build_wasm(
) -> Result<(), Error> { ) -> Result<(), Error> {
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE); let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
PBAR.info(&msg); PBAR.info(&msg);
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--lib"); cmd.current_dir(path).arg("build").arg("--lib");
if PBAR.quiet() {
cmd.arg("--quiet");
}
match profile { match profile {
BuildProfile::Profiling => { BuildProfile::Profiling => {
// Once there are DWARF debug info consumers, force enable debug // Once there are DWARF debug info consumers, force enable debug
@ -99,6 +105,7 @@ pub fn cargo_build_wasm(
// debug info by default. // debug info by default.
} }
} }
cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options); cmd.args(extra_options);
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
@ -111,11 +118,19 @@ pub fn cargo_build_wasm(
/// wasm-bindgen-cli to use when running tests. /// wasm-bindgen-cli to use when running tests.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> { pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests"); cmd.current_dir(path).arg("build").arg("--tests");
if PBAR.quiet() {
cmd.arg("--quiet");
}
if !debug { if !debug {
cmd.arg("--release"); cmd.arg("--release");
} }
cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(cmd, "cargo build").context("Compilation of your program failed")?; child::run(cmd, "cargo build").context("Compilation of your program failed")?;
Ok(()) Ok(())
} }

@ -210,6 +210,10 @@ pub fn cargo_install(
.arg("--root") .arg("--root")
.arg(&tmp); .arg(&tmp);
if PBAR.quiet() {
cmd.arg("--quiet");
}
let context = format!("Installing {} with cargo", tool); let context = format!("Installing {} with cargo", tool);
child::run(cmd, "cargo install").context(context)?; child::run(cmd, "cargo install").context(context)?;

@ -45,10 +45,10 @@ pub mod target;
pub mod test; pub mod test;
pub mod wasm_opt; pub mod wasm_opt;
use progressbar::ProgressOutput; use progressbar::{LogLevel, ProgressOutput};
/// The global progress bar and user-facing message output. /// The global progress bar and user-facing message output.
pub static PBAR: ProgressOutput = ProgressOutput; pub static PBAR: ProgressOutput = ProgressOutput::new();
/// 📦 ✨ pack and publish your wasm! /// 📦 ✨ pack and publish your wasm!
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -60,4 +60,12 @@ pub struct Cli {
/// Log verbosity is based off the number of v used /// Log verbosity is based off the number of v used
#[structopt(long = "verbose", short = "v", parse(from_occurrences))] #[structopt(long = "verbose", short = "v", parse(from_occurrences))]
pub verbosity: u8, pub verbosity: u8,
#[structopt(long = "quiet", short = "q")]
/// No output printed to stdout
pub quiet: bool,
#[structopt(long = "log-level", default_value = "info")]
/// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error]
pub log_level: LogLevel,
} }

@ -80,6 +80,13 @@ fn run() -> Result<(), failure::Error> {
} }
let args = Cli::from_args(); let args = Cli::from_args();
PBAR.set_log_level(args.log_level);
if args.quiet {
PBAR.set_quiet(true);
}
run_wasm_pack(args.cmd)?; run_wasm_pack(args.cmd)?;
if let Ok(wasm_pack_version) = wasm_pack_version.try_recv() { if let Ok(wasm_pack_version) = wasm_pack_version.try_recv() {

@ -2,47 +2,111 @@
use console::style; use console::style;
use emoji; use emoji;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
/// The maximum log level for wasm-pack
// The ordering is important: the least verbose must be at
// the top and the most verbose at the bottom
pub enum LogLevel {
/// Logs only error
Error,
/// Logs only warn and error
Warn,
/// Logs everything
Info,
}
impl std::str::FromStr for LogLevel {
type Err = failure::Error;
fn from_str(s: &str) -> Result<Self, failure::Error> {
match s {
"error" => Ok(LogLevel::Error),
"warn" => Ok(LogLevel::Warn),
"info" => Ok(LogLevel::Info),
_ => bail!("Unknown log-level: {}", s),
}
}
}
/// Synchronized progress bar and status message printing. /// Synchronized progress bar and status message printing.
pub struct ProgressOutput; pub struct ProgressOutput {
quiet: AtomicBool,
log_level: AtomicU8,
}
impl ProgressOutput { impl ProgressOutput {
/// Returns a new ProgressOutput
pub const fn new() -> Self {
Self {
quiet: AtomicBool::new(false),
log_level: AtomicU8::new(LogLevel::Info as u8),
}
}
/// Print the given message. /// Print the given message.
fn message(&self, message: &str) { fn message(&self, message: &str) {
eprintln!("{}", message); eprintln!("{}", message);
} }
/// Returns whether it should silence stdout or not
pub fn quiet(&self) -> bool {
self.quiet.load(Ordering::SeqCst)
}
/// Causes it to silence stdout
pub fn set_quiet(&self, quiet: bool) {
self.quiet.store(quiet, Ordering::SeqCst);
}
/// Returns whether the specified log level is enabled or not
pub fn is_log_enabled(&self, level: LogLevel) -> bool {
(level as u8) <= self.log_level.load(Ordering::SeqCst)
}
/// Sets the log level for wasm-pack
pub fn set_log_level(&self, log_level: LogLevel) {
self.log_level.store(log_level as u8, Ordering::SeqCst);
}
/// Add an informational message. /// Add an informational message.
pub fn info(&self, message: &str) { pub fn info(&self, message: &str) {
let info = format!("{}: {}", style("[INFO]").bold().dim(), message,); if !self.quiet() && self.is_log_enabled(LogLevel::Info) {
self.message(&info); let info = format!("{}: {}", style("[INFO]").bold().dim(), message,);
self.message(&info);
}
} }
/// Add a warning message. /// Add a warning message.
pub fn warn(&self, message: &str) { pub fn warn(&self, message: &str) {
let warn = format!( if !self.quiet() && self.is_log_enabled(LogLevel::Warn) {
"{} {}: {}", let warn = format!(
emoji::WARN, "{} {}: {}",
style("[WARN]").bold().dim(), emoji::WARN,
message style("[WARN]").bold().dim(),
); message
self.message(&warn); );
self.message(&warn);
}
} }
/// Add an error message. /// Add an error message.
pub fn error(&self, message: &str) { pub fn error(&self, message: &str) {
let err = format!( if self.is_log_enabled(LogLevel::Error) {
"{} {}: {}", let err = format!(
emoji::ERROR, "{} {}: {}",
style("[ERR]").bold().dim(), emoji::ERROR,
message style("[ERR]").bold().dim(),
); message
self.message(&err); );
self.message(&err);
}
} }
} }
impl Default for ProgressOutput { impl Default for ProgressOutput {
fn default() -> Self { fn default() -> Self {
ProgressOutput ProgressOutput::new()
} }
} }

@ -2,6 +2,7 @@
pub mod webdriver; pub mod webdriver;
use crate::PBAR;
use child; use child;
use failure::{self, ResultExt}; use failure::{self, ResultExt};
use std::ffi::OsStr; use std::ffi::OsStr;
@ -22,13 +23,22 @@ where
V: AsRef<OsStr>, V: AsRef<OsStr>,
{ {
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
cmd.envs(envs); cmd.envs(envs);
cmd.current_dir(path).arg("test"); cmd.current_dir(path).arg("test");
if PBAR.quiet() {
cmd.arg("--quiet");
}
if release { if release {
cmd.arg("--release"); cmd.arg("--release");
} }
cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options); cmd.args(extra_options);
child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?; child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?;
// NB: `child::run` took care of ensuring that test output gets printed. // NB: `child::run` took care of ensuring that test output gets printed.

@ -0,0 +1,91 @@
use assert_cmd::prelude::*;
use predicates::boolean::PredicateBooleanExt;
use predicates::prelude::predicate::str::contains;
use predicates::reflection::PredicateReflection;
use predicates::Predicate;
use utils;
fn matches_info() -> impl Predicate<str> + PredicateReflection {
contains("[INFO]: Checking for the Wasm target...")
.and(contains("[INFO]: Compiling to Wasm..."))
.and(contains("[INFO]: License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory"))
.and(contains("[INFO]: Installing wasm-bindgen..."))
.and(contains("[INFO]: Optimizing wasm binaries with `wasm-opt`..."))
.and(contains("[INFO]: :-) Done in "))
.and(contains("[INFO]: :-) Your wasm pkg is ready to publish at "))
}
fn matches_warn() -> impl Predicate<str> + PredicateReflection {
contains(":-) [WARN]: origin crate has no README")
}
fn matches_cargo() -> impl Predicate<str> + PredicateReflection {
contains("Finished release [optimized] target(s) in ")
}
#[test]
fn quiet() {
utils::fixture::Fixture::new()
.cargo_toml("js-hello-world")
.hello_world_src_lib()
.wasm_pack()
.arg("--quiet")
.arg("build")
.assert()
.success()
.stdout("")
.stderr("");
}
#[test]
fn log_level_info() {
utils::fixture::Fixture::new()
.cargo_toml("js-hello-world")
.hello_world_src_lib()
.wasm_pack()
.arg("--log-level")
.arg("info")
.arg("build")
.assert()
.success()
.stdout("")
.stderr(matches_cargo().and(matches_warn()).and(matches_info()));
}
#[test]
fn log_level_warn() {
utils::fixture::Fixture::new()
.cargo_toml("js-hello-world")
.hello_world_src_lib()
.wasm_pack()
.arg("--log-level")
.arg("warn")
.arg("build")
.assert()
.success()
.stdout("")
.stderr(
matches_cargo()
.and(matches_warn())
.and(matches_info().not()),
);
}
#[test]
fn log_level_error() {
utils::fixture::Fixture::new()
.cargo_toml("js-hello-world")
.hello_world_src_lib()
.wasm_pack()
.arg("--log-level")
.arg("error")
.arg("build")
.assert()
.success()
.stdout("")
.stderr(
matches_cargo()
.and(matches_warn().not())
.and(matches_info().not()),
);
}

@ -18,6 +18,7 @@ mod download;
mod generate; mod generate;
mod license; mod license;
mod lockfile; mod lockfile;
mod log_level;
mod manifest; mod manifest;
mod readme; mod readme;
mod stamps; mod stamps;

Loading…
Cancel
Save