Skip to content

Commit 60fb710

Browse files
committed
feat(cli): Log to chrome trace
You can browse these at https://ui.perfetto.dev
1 parent 9d3f4e4 commit 60fb710

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
9999
toml = "0.8.9"
100100
toml_edit = { version = "0.21.1", features = ["serde"] }
101101
tracing = "0.1.40" # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
102+
tracing-chrome = "0.7.1"
102103
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
103104
unicase = "2.7.0"
104105
unicode-width = "0.1.11"
@@ -198,6 +199,7 @@ time.workspace = true
198199
toml.workspace = true
199200
toml_edit.workspace = true
200201
tracing.workspace = true
202+
tracing-chrome.workspace = true
201203
tracing-subscriber.workspace = true
202204
unicase.workspace = true
203205
unicode-width.workspace = true

src/bin/cargo/main.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod commands;
1818
use crate::command_prelude::*;
1919

2020
fn main() {
21-
setup_logger();
21+
let _guard = setup_logger();
2222

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

@@ -35,16 +35,42 @@ fn main() {
3535
}
3636
}
3737

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

41-
tracing_subscriber::fmt()
41+
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
42+
let fmt_layer = tracing_subscriber::fmt::layer()
4243
.with_timer(tracing_subscriber::fmt::time::Uptime::default())
4344
.with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
4445
.with_writer(std::io::stderr)
45-
.with_env_filter(env)
46-
.init();
46+
.with_filter(env);
47+
48+
let (profile_layer, profile_guard) =
49+
#[allow(clippy::disallowed_methods)]
50+
if env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE").as_deref()) {
51+
let capture_args =
52+
env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE_CAPTURE_ARGS").as_deref());
53+
let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
54+
.include_args(capture_args)
55+
.build();
56+
(Some(layer), Some(guard))
57+
} else {
58+
(None, None)
59+
};
60+
61+
let registry = tracing_subscriber::registry()
62+
.with(fmt_layer)
63+
.with(profile_layer);
64+
registry.init();
4765
tracing::trace!(start = humantime::format_rfc3339(std::time::SystemTime::now()).to_string());
66+
profile_guard
67+
}
68+
69+
fn env_to_bool(os: Option<&OsStr>) -> bool {
70+
match os.and_then(|os| os.to_str()) {
71+
Some("1") | Some("true") => true,
72+
_ => false,
73+
}
4874
}
4975

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

0 commit comments

Comments
 (0)