1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use command::Command;
use error::Error;
use slog::{Drain, Level, Logger};
use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator};
use std::fs::OpenOptions;
use std::path::PathBuf;
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> {
let log_path = log_file_path(&cmd);
let file = OpenOptions::new().create(true).append(true).open(log_path)?;
let decorator = PlainDecorator::new(file);
let drain = FullFormat::new(decorator).build().fuse();
let log_level = match verbosity {
0 => Level::Error,
1 => Level::Info,
2 => Level::Debug,
_ => Level::Trace,
};
let drain = Async::new(drain).build().filter_level(log_level).fuse();
Ok(Logger::root(drain, o!()))
}
fn log_file_path(cmd: &Command) -> PathBuf {
let path = match cmd {
Command::Init { path, .. } => path,
Command::Pack { path } => path,
Command::Publish { path } => path,
Command::Login { .. } => &None,
};
if let Some(ref path) = path {
let mut path_buf = PathBuf::from(path);
path_buf.push("Cargo.toml");
if path_buf.exists() {
path_buf.pop();
path_buf.push("wasm-pack.log");
path_buf
} else {
let mut path_buf = this_dir();
path_buf.push("wasm-pack.log");
path_buf
}
} else {
let mut path_buf = this_dir();
path_buf.push("wasm-pack.log");
path_buf
}
}
fn this_dir() -> PathBuf {
PathBuf::from(".")
}