Skip to content

Commit

Permalink
feat(cli): Log to chrome trace
Browse files Browse the repository at this point in the history
You can browse these at https://ui.perfetto.dev
  • Loading branch information
epage committed Feb 5, 2024
1 parent 9d3f4e4 commit 60fb710
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
toml = "0.8.9"
toml_edit = { version = "0.21.1", features = ["serde"] }
tracing = "0.1.40" # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
tracing-chrome = "0.7.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
unicase = "2.7.0"
unicode-width = "0.1.11"
Expand Down Expand Up @@ -198,6 +199,7 @@ time.workspace = true
toml.workspace = true
toml_edit.workspace = true
tracing.workspace = true
tracing-chrome.workspace = true
tracing-subscriber.workspace = true
unicase.workspace = true
unicode-width.workspace = true
Expand Down
38 changes: 32 additions & 6 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod commands;
use crate::command_prelude::*;

fn main() {
setup_logger();
let _guard = setup_logger();

let mut config = cli::LazyConfig::new();

Expand All @@ -35,16 +35,42 @@ fn main() {
}
}

fn setup_logger() {
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
use tracing_subscriber::prelude::*;

tracing_subscriber::fmt()
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
let fmt_layer = tracing_subscriber::fmt::layer()
.with_timer(tracing_subscriber::fmt::time::Uptime::default())
.with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
.with_writer(std::io::stderr)
.with_env_filter(env)
.init();
.with_filter(env);

let (profile_layer, profile_guard) =
#[allow(clippy::disallowed_methods)]
if env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE").as_deref()) {
let capture_args =
env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE_CAPTURE_ARGS").as_deref());
let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
.include_args(capture_args)
.build();
(Some(layer), Some(guard))
} else {
(None, None)
};

let registry = tracing_subscriber::registry()
.with(fmt_layer)
.with(profile_layer);
registry.init();
tracing::trace!(start = humantime::format_rfc3339(std::time::SystemTime::now()).to_string());
profile_guard
}

fn env_to_bool(os: Option<&OsStr>) -> bool {
match os.and_then(|os| os.to_str()) {
Some("1") | Some("true") => true,
_ => false,
}
}

/// Table for defining the aliases which come builtin in `Cargo`.
Expand Down

0 comments on commit 60fb710

Please sign in to comment.