feat(cli): Log to chrome trace

You can browse these at https://ui.perfetto.dev
This commit is contained in:
Ed Page 2024-02-05 14:25:01 -06:00
parent 19141bdc20
commit d188808d97
3 changed files with 47 additions and 6 deletions

12
Cargo.lock generated
View File

@ -342,6 +342,7 @@ dependencies = [
"toml",
"toml_edit",
"tracing",
"tracing-chrome",
"tracing-subscriber",
"unicase",
"unicode-width",
@ -3450,6 +3451,17 @@ dependencies = [
"syn 2.0.46",
]
[[package]]
name = "tracing-chrome"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "496b3cd5447f7ff527bbbf19b071ad542a000adf297d4127078b4dfdb931f41a"
dependencies = [
"serde_json",
"tracing-core",
"tracing-subscriber",
]
[[package]]
name = "tracing-core"
version = "0.1.32"

View File

@ -99,6 +99,7 @@ time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
toml = "0.8.10"
toml_edit = { version = "0.22.6", 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"
@ -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

View File

@ -18,7 +18,7 @@ mod commands;
use crate::command_prelude::*;
fn main() {
setup_logger();
let _guard = setup_logger();
let mut gctx = match GlobalContext::default() {
Ok(gctx) => gctx,
@ -41,16 +41,43 @@ fn main() {
}
}
fn setup_logger() {
let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
#![allow(clippy::disallowed_methods)]
tracing_subscriber::fmt()
use tracing_subscriber::prelude::*;
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) =
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`.