Skip to content

Commit d188808

Browse files
committed
feat(cli): Log to chrome trace
You can browse these at https://ui.perfetto.dev
1 parent 19141bd commit d188808

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-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.10"
100100
toml_edit = { version = "0.22.6", 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: 33 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 gctx = match GlobalContext::default() {
2424
Ok(gctx) => gctx,
@@ -41,16 +41,43 @@ fn main() {
4141
}
4242
}
4343

44-
fn setup_logger() {
45-
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
44+
fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
45+
#![allow(clippy::disallowed_methods)]
46+
47+
use tracing_subscriber::prelude::*;
4648

47-
tracing_subscriber::fmt()
49+
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
50+
let fmt_layer = tracing_subscriber::fmt::layer()
4851
.with_timer(tracing_subscriber::fmt::time::Uptime::default())
4952
.with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
5053
.with_writer(std::io::stderr)
51-
.with_env_filter(env)
52-
.init();
54+
.with_filter(env);
55+
56+
let (profile_layer, profile_guard) =
57+
if env_to_bool(std::env::var_os("__CARGO_LOG_PROFILE").as_deref()) {
58+
let capture_args =
59+
env_to_bool(std::env::var_os("__CARGO_LOG_PROFILE_CAPTURE_ARGS").as_deref());
60+
let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
61+
.include_args(capture_args)
62+
.build();
63+
(Some(layer), Some(guard))
64+
} else {
65+
(None, None)
66+
};
67+
68+
let registry = tracing_subscriber::registry()
69+
.with(fmt_layer)
70+
.with(profile_layer);
71+
registry.init();
5372
tracing::trace!(start = humantime::format_rfc3339(std::time::SystemTime::now()).to_string());
73+
profile_guard
74+
}
75+
76+
fn env_to_bool(os: Option<&OsStr>) -> bool {
77+
match os.and_then(|os| os.to_str()) {
78+
Some("1") | Some("true") => true,
79+
_ => false,
80+
}
5481
}
5582

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

0 commit comments

Comments
 (0)