Skip to content

Commit

Permalink
Auto merge of #13551 - epage:cfg, r=weihanglo
Browse files Browse the repository at this point in the history
fix(cli): Skip tracing-chrome for platforms without 64bit atomics

See rust-lang/rust#122054

I also created thoren-d/tracing-chrome#27
  • Loading branch information
bors committed Mar 6, 2024
2 parents bc0ab3d + 307ad0c commit a4c63fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ 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
url.workspace = true
walkdir.workspace = true

[target.'cfg(target_has_atomic = "64")'.dependencies]
tracing-chrome.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
cargo-credential-libsecret.workspace = true

Expand Down
54 changes: 40 additions & 14 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ fn main() {
}
}

fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
#![allow(clippy::disallowed_methods)]

fn setup_logger() -> Option<ChromeFlushGuard> {
use tracing_subscriber::prelude::*;

let env = tracing_subscriber::EnvFilter::from_env("CARGO_LOG");
Expand All @@ -53,17 +51,7 @@ fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
.with_writer(std::io::stderr)
.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 (profile_layer, profile_guard) = chrome_layer();

let registry = tracing_subscriber::registry()
.with(fmt_layer)
Expand All @@ -73,6 +61,44 @@ fn setup_logger() -> Option<tracing_chrome::FlushGuard> {
profile_guard
}

#[cfg(target_has_atomic = "64")]
type ChromeFlushGuard = tracing_chrome::FlushGuard;
#[cfg(target_has_atomic = "64")]
fn chrome_layer<S>() -> (
Option<tracing_chrome::ChromeLayer<S>>,
Option<ChromeFlushGuard>,
)
where
S: tracing::Subscriber
+ for<'span> tracing_subscriber::registry::LookupSpan<'span>
+ Send
+ Sync,
{
#![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)
}
}

#[cfg(not(target_has_atomic = "64"))]
type ChromeFlushGuard = ();
#[cfg(not(target_has_atomic = "64"))]
fn chrome_layer() -> (
Option<tracing_subscriber::layer::Identity>,
Option<ChromeFlushGuard>,
) {
(None, None)
}

#[cfg(target_has_atomic = "64")]
fn env_to_bool(os: Option<&OsStr>) -> bool {
match os.and_then(|os| os.to_str()) {
Some("1") | Some("true") => true,
Expand Down

0 comments on commit a4c63fe

Please sign in to comment.