-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a logging abstraction and cleaned up the main argument parsing …
…logic
- Loading branch information
Michael-F-Bryan
committed
Jun 14, 2023
1 parent
e7e7419
commit eee6765
Showing
2 changed files
with
178 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,96 @@ | ||
//! Logging functions for the debug feature. | ||
use tracing::level_filters::LevelFilter; | ||
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; | ||
|
||
/// Initialize logging based on the `$RUST_LOG` environment variable. Logs will | ||
/// be disabled when `$RUST_LOG` isn't set. | ||
pub fn set_up_logging() { | ||
let fmt_layer = fmt::layer() | ||
.with_target(true) | ||
.with_span_events(fmt::format::FmtSpan::CLOSE) | ||
.with_ansi(should_emit_colors()) | ||
.with_thread_ids(true) | ||
.with_writer(std::io::stderr) | ||
.compact(); | ||
|
||
let filter_layer = EnvFilter::builder().from_env_lossy(); | ||
|
||
tracing_subscriber::registry() | ||
.with(filter_layer) | ||
.with(fmt_layer) | ||
.init(); | ||
const WHITELISTED_LOG_TARGETS: &[&str] = &["wasmer", "wasmer_wasix", "virtual_fs"]; | ||
|
||
/// Control the output generated by the CLI. | ||
#[derive(Debug, Clone, PartialEq, clap::Parser)] | ||
pub struct Output { | ||
/// Generate verbose output (repeat for more verbosity) | ||
#[clap(short, long, action = clap::ArgAction::Count, global = true, conflicts_with = "quiet")] | ||
pub verbose: usize, | ||
/// Do not print progress messages. | ||
#[clap(short, long, global = true, conflicts_with = "verbose")] | ||
pub quiet: bool, | ||
/// When to display colored output. | ||
#[clap(long, default_value_t = clap::ColorChoice::Auto, global = true)] | ||
pub color: clap::ColorChoice, | ||
} | ||
|
||
/// Check whether we should emit ANSI escape codes for log formatting. | ||
/// | ||
/// The `tracing-subscriber` crate doesn't have native support for | ||
/// "--color=always|never|auto", so we implement a poor man's version. | ||
/// | ||
/// For more, see https://github.com/tokio-rs/tracing/issues/2388 | ||
fn should_emit_colors() -> bool { | ||
isatty::stderr_isatty() && std::env::var_os("NO_COLOR").is_none() | ||
impl Output { | ||
/// Has the `--verbose` flag been set? | ||
pub fn is_verbose(&self) -> bool { | ||
self.verbose > 0 | ||
} | ||
|
||
/// Initialize logging based on the `$RUST_LOG` environment variable and | ||
/// command-line flags. | ||
pub fn initialize_logging(&self) { | ||
let fmt_layer = fmt::layer() | ||
.with_target(true) | ||
.with_span_events(fmt::format::FmtSpan::CLOSE) | ||
.with_ansi(self.should_emit_colors()) | ||
.with_thread_ids(true) | ||
.with_writer(std::io::stderr) | ||
.compact(); | ||
|
||
let filter_layer = self.log_filter(); | ||
|
||
tracing_subscriber::registry() | ||
.with(filter_layer) | ||
.with(fmt_layer) | ||
.init(); | ||
} | ||
|
||
fn log_filter(&self) -> EnvFilter { | ||
let default_filters = [ | ||
LevelFilter::OFF, | ||
LevelFilter::WARN, | ||
LevelFilter::INFO, | ||
LevelFilter::DEBUG, | ||
]; | ||
|
||
// First, we set up the default log level. | ||
let default_level = default_filters | ||
.get(self.verbose) | ||
.copied() | ||
.unwrap_or(LevelFilter::TRACE); | ||
let mut filter = EnvFilter::builder() | ||
.with_default_directive(default_level.into()) | ||
.from_env_lossy(); | ||
|
||
// Next we add level-specific directives, where verbosity=0 means don't | ||
// override anything. Note that these are shifted one level up so we'll | ||
// get something like RUST_LOG="warn,wasmer_wasix=info" | ||
let specific_filters = [LevelFilter::WARN, LevelFilter::INFO, LevelFilter::DEBUG]; | ||
if self.verbose > 0 { | ||
let level = specific_filters | ||
.get(self.verbose) | ||
.copied() | ||
.unwrap_or(LevelFilter::TRACE); | ||
|
||
for target in WHITELISTED_LOG_TARGETS { | ||
let directive = format!("{target}={level}").parse().unwrap(); | ||
filter = filter.add_directive(directive); | ||
} | ||
} | ||
|
||
filter | ||
} | ||
|
||
/// Check whether we should emit ANSI escape codes for log formatting. | ||
/// | ||
/// The `tracing-subscriber` crate doesn't have native support for | ||
/// "--color=always|never|auto", so we implement a poor man's version. | ||
/// | ||
/// For more, see https://github.com/tokio-rs/tracing/issues/2388 | ||
fn should_emit_colors(&self) -> bool { | ||
match self.color { | ||
clap::ColorChoice::Auto => isatty::stderr_isatty(), | ||
clap::ColorChoice::Always => true, | ||
clap::ColorChoice::Never => false, | ||
} | ||
} | ||
} |