diff --git a/Cargo.toml b/Cargo.toml index c50ec810b49631..c5a288c17b0cd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,9 @@ serde = { version = "1", features = ["derive"] } log = "0.4" ron = "0.6" anyhow = "1.0" +tracing = { version = "0.1.21" } +tracing-chrome = { git = "https://github.com/superdump/tracing-chrome", branch = "name.fields" } +tracing-subscriber = { version = "0.2.15" } # bevy (Android) [target.'cfg(target_os = "android")'.dependencies] @@ -173,6 +176,10 @@ path = "examples/app/return_after_run.rs" name = "thread_pool_resources" path = "examples/app/thread_pool_resources.rs" +[[example]] +name = "tracing" +path = "examples/app/tracing.rs" + [[example]] name = "hot_asset_reloading" path = "examples/asset/hot_asset_reloading.rs" diff --git a/examples/README.md b/examples/README.md index f2eb9ed2571e18..00cd5ecd1112e3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -42,6 +42,7 @@ Example | File | Description `headless` | [`app/headless.rs`](./app/headless.rs) | An application that runs without default plugins `plugin` | [`app/plugin.rs`](./app/plugin.rs) | Demonstrates the creation and registration of a custom plugin `thread_pool_resources` | [`app/thread_pool_resources.rs`](./app/thread_pool_resources.rs) | Creates and customizes the internal thread pool +`tracing` | [`app/tracing.rs`](./app/tracing.rs) | Demonstrates `trace` feature output. Run with `RUST_LOG=info cargo run --example tracing --features trace` and then open the `trace-1234.json` file (where 1234 is a time since the beginning of the epoch) in `chrome://tracing` in Chrome. ## Assets diff --git a/examples/app/tracing.rs b/examples/app/tracing.rs new file mode 100644 index 00000000000000..a54f31e7289ec5 --- /dev/null +++ b/examples/app/tracing.rs @@ -0,0 +1,50 @@ +use bevy::{input::system::exit_on_esc_system, prelude::*}; +use std::{thread, time}; +use tracing::{info, trace_span}; +use tracing_chrome::ChromeLayerBuilder; +use tracing_subscriber::{fmt, prelude::*, registry::Registry, EnvFilter}; + +pub fn setup_global_subscriber() -> impl Drop { + let fmt_layer = fmt::Layer::default(); + let filter_layer = EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new("error")) + .unwrap(); + + let (chrome_layer, _guard) = ChromeLayerBuilder::new().build(); + + let subscriber = Registry::default() + .with(filter_layer) + .with(fmt_layer) + .with(chrome_layer); + + tracing::subscriber::set_global_default(subscriber).expect("Could not set global default"); + _guard +} + +fn main() { + let _guard = setup_global_subscriber(); + + let tracing_span = trace_span!("tracing"); + let _tracing_guard = tracing_span.enter(); + + App::build() + .add_plugins(DefaultPlugins) + .add_startup_system(a_system.system()) + .add_system(foo_bar_baz.system()) + .add_system(exit_on_esc_system.system()) + .run(); +} + +fn a_system(mut commands: Commands) { + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + + commands.spawn((GlobalTransform::default(), Transform::default())); +} + +fn foo_bar_baz(transform: &Transform) { + let five_millis = time::Duration::from_millis(5); + thread::sleep(five_millis); + + info!("{:#?}", transform); +}