Skip to content

Commit 0562859

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

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-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: 31 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,41 @@ 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+
if env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE").as_deref()) {
50+
let capture_args =
51+
env_to_bool(std::env::var_os("_CARGO_LOG_PROFILE_CAPTURE_ARGS").as_deref());
52+
let (layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
53+
.include_args(capture_args)
54+
.build();
55+
(Some(layer), Some(guard))
56+
} else {
57+
(None, None)
58+
};
59+
60+
let registry = tracing_subscriber::registry()
61+
.with(fmt_layer)
62+
.with(profile_layer);
63+
registry.init();
4764
tracing::trace!(start = humantime::format_rfc3339(std::time::SystemTime::now()).to_string());
65+
profile_guard
66+
}
67+
68+
fn env_to_bool(os: Option<&OsStr>) -> bool {
69+
match os.and_then(|os| os.to_str()) {
70+
Some("1") | Some("true") => true,
71+
_ => false,
72+
}
4873
}
4974

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

0 commit comments

Comments
 (0)