Skip to content

Commit

Permalink
Add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Nov 4, 2020
1 parent 9b09a20 commit 10c21c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions examples/app/tracing.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 10c21c3

Please sign in to comment.