Skip to content

Commit

Permalink
Replace slog with log
Browse files Browse the repository at this point in the history
This commit replaces the `slog` family of crates used by `wasm-pack`
with the `log` crate plus `env_logger`. This also means that by default
`wasm-pack` also won't create a `wasm-pack.log` file in the current
directory. Enabling logging will now be done through
`RUST_LOG=wasm_pack` instead of `-v` flags.

Closes #425
  • Loading branch information
alexcrichton authored and ashleygwilliams committed Jan 15, 2019
1 parent 92d24ff commit 80be336
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 504 deletions.
397 changes: 197 additions & 200 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ atty = "0.2.11"
cargo_metadata = "0.6.0"
console = "0.6.1"
dialoguer = "0.3.0"
curl = "0.4.13"
dirs = "1.0.4"
env_logger = { version = "0.5.13", default-features = false }
failure = "0.1.2"
human-panic = "1.0.1"
glob = "0.2"
indicatif = "0.9.0"
lazy_static = "1.1.0"
log = "0.4.6"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_ignored = "0.0.4"
serde_json = "1.0.26"
slog = "2.3"
slog-term = "2.4"
slog-async = "2.3"
strsim = "0.8.0"
siphasher = "0.2.3"
structopt = "0.2"
toml = "0.4"
which = "2.0.0"
Expand Down
29 changes: 10 additions & 19 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use child;
use command::build::BuildProfile;
use emoji;
use failure::{self, ResultExt};
use log::debug;
use log::{info, warn};
use manifest::CrateData;
use progressbar::Step;
use slog::Logger;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -26,20 +27,15 @@ pub fn install_wasm_bindgen(
version: &str,
install_permitted: bool,
step: &Step,
log: &Logger,
) -> Result<Download, failure::Error> {
// If `wasm-bindgen` is installed globally and it has the right version, use
// that. Assume that other tools are installed next to it.
//
// This situation can arise if `wasm-bindgen` is already installed via
// `cargo install`, for example.
if let Ok(path) = which("wasm-bindgen") {
debug!(
log,
"found global wasm-bindgen binary at: {}",
path.display()
);
if wasm_bindgen_version_check(&path, version, log) {
debug!("found global wasm-bindgen binary at: {}", path.display());
if wasm_bindgen_version_check(&path, version) {
return Ok(Download::at(path.parent().unwrap()));
}
}
Expand All @@ -52,14 +48,13 @@ pub fn install_wasm_bindgen(
Ok(dl) => return Ok(dl),
Err(e) => {
warn!(
log,
"could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.",
e
);
}
}

cargo_install_wasm_bindgen(log, &cache, version, install_permitted)
cargo_install_wasm_bindgen(&cache, version, install_permitted)
}

/// Downloads a precompiled copy of wasm-bindgen, if available.
Expand Down Expand Up @@ -102,7 +97,6 @@ fn prebuilt_url(version: &str) -> Option<String> {
/// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given
/// crate.
pub fn cargo_install_wasm_bindgen(
logger: &Logger,
cache: &Cache,
version: &str,
install_permitted: bool,
Expand Down Expand Up @@ -132,7 +126,7 @@ pub fn cargo_install_wasm_bindgen(
.arg("--root")
.arg(&tmp);

child::run(logger, cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;
child::run(cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;

fs::rename(&tmp, &destination)?;
Ok(Download::at(&destination))
Expand All @@ -148,7 +142,6 @@ pub fn wasm_bindgen_build(
target: &str,
profile: BuildProfile,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER);
PBAR.step(step, &msg);
Expand Down Expand Up @@ -196,26 +189,24 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug");
}

child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &Logger) -> bool {
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool {
let mut cmd = Command::new(bindgen_path);
cmd.arg("--version");
child::run(log, cmd, "wasm-bindgen")
child::run(cmd, "wasm-bindgen")
.map(|stdout| {
stdout
.trim()
.split_whitespace()
.nth(1)
.map(|v| {
info!(
log,
"Checking installed `wasm-bindgen` version == expected version: {} == {}",
v,
dep_version
v, dep_version
);
v == dep_version
})
Expand Down
19 changes: 6 additions & 13 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use command::build::BuildProfile;
use emoji;
use failure::{Error, ResultExt};
use progressbar::Step;
use slog::Logger;
use std::path::Path;
use std::process::Command;
use std::str;
Expand Down Expand Up @@ -52,23 +51,17 @@ fn rustc_minor_version() -> Option<u32> {

/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), Error> {
pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {
let msg = format!("{}Adding WASM target...", emoji::TARGET);
PBAR.step(step, &msg);
let mut cmd = Command::new("rustup");
cmd.arg("target").arg("add").arg("wasm32-unknown-unknown");
child::run(log, cmd, "rustup")
.context("Adding the wasm32-unknown-unknown target with rustup")?;
child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?;
Ok(())
}

/// Run `cargo build` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(
log: &Logger,
path: &Path,
profile: BuildProfile,
step: &Step,
) -> Result<(), Error> {
pub fn cargo_build_wasm(path: &Path, profile: BuildProfile, step: &Step) -> Result<(), Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
let mut cmd = Command::new("cargo");
Expand All @@ -91,18 +84,18 @@ pub fn cargo_build_wasm(
}
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}

/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm_tests(log: &Logger, path: &Path, debug: bool) -> Result<(), Error> {
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compilation of your program failed")?;
child::run(cmd, "cargo build").context("Compilation of your program failed")?;
Ok(())
}
10 changes: 5 additions & 5 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! properly logged and their output is logged as well.

use failure::Error;
use slog::Logger;
use log::info;
use std::{
io::{self, Read},
mem,
Expand Down Expand Up @@ -133,8 +133,8 @@ where
}

/// Run the given command and return its stdout.
pub fn run(logger: &Logger, mut command: Command, command_name: &str) -> Result<String, Error> {
info!(logger, "Running {:?}", command);
pub fn run(mut command: Command, command_name: &str) -> Result<String, Error> {
info!("Running {:?}", command);

let mut child = command
.stdout(Stdio::piped())
Expand All @@ -158,11 +158,11 @@ pub fn run(logger: &Logger, mut command: Command, command_name: &str) -> Result<
thread::spawn(move || read_and_send(stderr, stderr_send, OutputFragment::Stderr));

let mut stdout = OutputAccumulator::new(|line| {
info!(logger, "{} (stdout): {}", command_name, line);
info!("{} (stdout): {}", command_name, line);
PBAR.message(line)
});
let mut stderr = OutputAccumulator::new(|line| {
info!(logger, "{} (stderr): {}", command_name, line);
info!("{} (stderr): {}", command_name, line);
PBAR.message(line)
});

Expand Down
Loading

0 comments on commit 80be336

Please sign in to comment.