Skip to content

Commit

Permalink
Tracing chrome span names (#979)
Browse files Browse the repository at this point in the history
* Update tracing-chrome to 0.3.0

* bevy_log: Add fields to span names for tracing-chrome

* Conditionally import tracing_subscriber modules based on feature
  • Loading branch information
superdump authored Dec 3, 2020
1 parent c097af4 commit 59010ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bevy_app = { path = "../bevy_app", version = "0.3.0" }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" }

tracing-subscriber = {version = "0.2.15", features = ["registry"]}
tracing-chrome = { version = "0.2.0", optional = true }
tracing-chrome = { version = "0.3.0", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
android_log-sys = "0.2.0"
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub use bevy_utils::tracing::{
};

use bevy_app::{AppBuilder, Plugin};
#[cfg(feature = "tracing-chrome")]
use tracing_subscriber::fmt::{format::DefaultFields, FormattedFields};
use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};

/// Adds logging to Apps.
Expand Down Expand Up @@ -55,7 +57,20 @@ impl Plugin for LogPlugin {
let subscriber = subscriber.with(fmt_layer);
#[cfg(feature = "tracing-chrome")]
{
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new().build();
let (chrome_layer, guard) = tracing_chrome::ChromeLayerBuilder::new()
.name_fn(Box::new(|event_or_span| match event_or_span {
tracing_chrome::EventOrSpan::Event(event) => event.metadata().name().into(),
tracing_chrome::EventOrSpan::Span(span) => {
if let Some(fields) =
span.extensions().get::<FormattedFields<DefaultFields>>()
{
format!("{}: {}", span.metadata().name(), fields.fields.as_str())
} else {
span.metadata().name().into()
}
}
}))
.build();
app.resources_mut().insert_thread_local(guard);
let subscriber = subscriber.with(chrome_layer);
bevy_utils::tracing::subscriber::set_global_default(subscriber)
Expand Down

0 comments on commit 59010ca

Please sign in to comment.