diff --git a/Cargo.toml b/Cargo.toml index 9a6c0fc5ad88d..5fe7da2a416e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -191,8 +191,8 @@ name = "custom_diagnostic" path = "examples/diagnostics/custom_diagnostic.rs" [[example]] -name = "print_diagnostics" -path = "examples/diagnostics/print_diagnostics.rs" +name = "log_diagnostics" +path = "examples/diagnostics/log_diagnostics.rs" [[example]] name = "event" diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index e5644853e567b..1b8a01e6a3bf6 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -19,6 +19,7 @@ filesystem_watcher = ["notify"] [dependencies] # bevy bevy_app = { path = "../bevy_app", version = "0.4.0" } +bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.4.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] } bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" } diff --git a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs new file mode 100644 index 0000000000000..db3d773d5004d --- /dev/null +++ b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs @@ -0,0 +1,35 @@ +use crate::{Asset, Assets}; +use bevy_app::prelude::*; +use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics}; +use bevy_ecs::{IntoSystem, Res, ResMut}; + +/// Adds "asset count" diagnostic to an App +#[derive(Default)] +pub struct AssetCountDiagnosticsPlugin { + marker: std::marker::PhantomData, +} + +impl Plugin for AssetCountDiagnosticsPlugin { + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(Self::setup_system.system()) + .add_system(Self::diagnostic_system.system()); + } +} + +impl AssetCountDiagnosticsPlugin { + pub fn diagnostic_id() -> DiagnosticId { + DiagnosticId(T::TYPE_UUID) + } + + pub fn setup_system(mut diagnostics: ResMut) { + diagnostics.add(Diagnostic::new( + Self::diagnostic_id(), + &format!("asset_count {}", std::any::type_name::()), + 20, + )); + } + + pub fn diagnostic_system(mut diagnostics: ResMut, assets: Res>) { + diagnostics.add_measurement(Self::diagnostic_id(), assets.len() as f64); + } +} diff --git a/crates/bevy_asset/src/diagnostic/mod.rs b/crates/bevy_asset/src/diagnostic/mod.rs new file mode 100644 index 0000000000000..f093527f9755a --- /dev/null +++ b/crates/bevy_asset/src/diagnostic/mod.rs @@ -0,0 +1,2 @@ +mod asset_count_diagnostics_plugin; +pub use asset_count_diagnostics_plugin::AssetCountDiagnosticsPlugin; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 5c9b4133b9d81..e65ce22bd0c39 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -1,5 +1,6 @@ mod asset_server; mod assets; +pub mod diagnostic; #[cfg(all( feature = "filesystem_watcher", all(not(target_arch = "wasm32"), not(target_os = "android")) diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index 2b39528e295ef..c96f967c05f0e 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -18,6 +18,7 @@ keywords = ["bevy"] bevy_app = { path = "../bevy_app", version = "0.4.0" } bevy_core = { path = "../bevy_core", version = "0.4.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" } +bevy_log = { path = "../bevy_log", version = "0.4.0" } bevy_utils = { path = "../bevy_utils", version = "0.4.0" } # other diff --git a/crates/bevy_diagnostic/src/diagnostic.rs b/crates/bevy_diagnostic/src/diagnostic.rs index 20d7edcf93e3e..549ecdb20f755 100644 --- a/crates/bevy_diagnostic/src/diagnostic.rs +++ b/crates/bevy_diagnostic/src/diagnostic.rs @@ -1,8 +1,8 @@ use bevy_utils::{Duration, HashMap, Instant, Uuid}; -use std::collections::VecDeque; +use std::collections::{BTreeSet, VecDeque}; /// Unique identifier for a [Diagnostic] -#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)] pub struct DiagnosticId(pub Uuid); impl DiagnosticId { @@ -102,10 +102,12 @@ impl Diagnostic { #[derive(Debug, Default)] pub struct Diagnostics { diagnostics: HashMap, + ordered_diagnostics: BTreeSet, } impl Diagnostics { pub fn add(&mut self, diagnostic: Diagnostic) { + self.ordered_diagnostics.insert(diagnostic.id); self.diagnostics.insert(diagnostic.id, diagnostic); } @@ -132,4 +134,10 @@ impl Diagnostics { pub fn iter(&self) -> impl Iterator { self.diagnostics.values() } + + pub fn ordered_iter(&self) -> impl Iterator { + self.ordered_diagnostics + .iter() + .filter_map(move |k| self.diagnostics.get(k)) + } } diff --git a/crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs new file mode 100644 index 0000000000000..36a709f453795 --- /dev/null +++ b/crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs @@ -0,0 +1,29 @@ +use crate::{Diagnostic, DiagnosticId, Diagnostics}; +use bevy_app::prelude::*; +use bevy_ecs::{IntoSystem, ResMut, Resources, World}; + +/// Adds "entity count" diagnostic to an App +#[derive(Default)] +pub struct EntityCountDiagnosticsPlugin; + +impl Plugin for EntityCountDiagnosticsPlugin { + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(Self::setup_system.system()) + .add_system(Self::diagnostic_system.system()); + } +} + +impl EntityCountDiagnosticsPlugin { + pub const ENTITY_COUNT: DiagnosticId = + DiagnosticId::from_u128(187513512115068938494459732780662867798); + + pub fn setup_system(mut diagnostics: ResMut) { + diagnostics.add(Diagnostic::new(Self::ENTITY_COUNT, "entity_count", 20)); + } + + pub fn diagnostic_system(world: &mut World, resources: &mut Resources) { + if let Some(mut diagnostics) = resources.get_mut::() { + diagnostics.add_measurement(Self::ENTITY_COUNT, world.entity_count() as f64); + } + } +} diff --git a/crates/bevy_diagnostic/src/lib.rs b/crates/bevy_diagnostic/src/lib.rs index d54c32663ca62..dfb6b6d324e4f 100644 --- a/crates/bevy_diagnostic/src/lib.rs +++ b/crates/bevy_diagnostic/src/lib.rs @@ -1,9 +1,11 @@ mod diagnostic; +mod entity_count_diagnostics_plugin; mod frame_time_diagnostics_plugin; -mod print_diagnostics_plugin; +mod log_diagnostics_plugin; pub use diagnostic::*; +pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin; pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin; -pub use print_diagnostics_plugin::PrintDiagnosticsPlugin; +pub use log_diagnostics_plugin::LogDiagnosticsPlugin; use bevy_app::prelude::*; diff --git a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs similarity index 55% rename from crates/bevy_diagnostic/src/print_diagnostics_plugin.rs rename to crates/bevy_diagnostic/src/log_diagnostics_plugin.rs index e71f4b9066634..276480786f611 100644 --- a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs @@ -2,24 +2,25 @@ use super::{Diagnostic, DiagnosticId, Diagnostics}; use bevy_app::prelude::*; use bevy_core::{Time, Timer}; use bevy_ecs::{IntoSystem, Res, ResMut}; +use bevy_log::{debug, info}; use bevy_utils::Duration; -/// An App Plugin that prints diagnostics to the console -pub struct PrintDiagnosticsPlugin { +/// An App Plugin that logs diagnostics to the console +pub struct LogDiagnosticsPlugin { pub debug: bool, pub wait_duration: Duration, pub filter: Option>, } -/// State used by the [PrintDiagnosticsPlugin] -pub struct PrintDiagnosticsState { +/// State used by the [LogDiagnosticsPlugin] +struct LogDiagnosticsState { timer: Timer, filter: Option>, } -impl Default for PrintDiagnosticsPlugin { +impl Default for LogDiagnosticsPlugin { fn default() -> Self { - PrintDiagnosticsPlugin { + LogDiagnosticsPlugin { debug: false, wait_duration: Duration::from_secs(1), filter: None, @@ -27,9 +28,9 @@ impl Default for PrintDiagnosticsPlugin { } } -impl Plugin for PrintDiagnosticsPlugin { +impl Plugin for LogDiagnosticsPlugin { fn build(&self, app: &mut bevy_app::AppBuilder) { - app.add_resource(PrintDiagnosticsState { + app.add_resource(LogDiagnosticsState { timer: Timer::new(self.wait_duration, true), filter: self.filter.clone(), }); @@ -37,68 +38,66 @@ impl Plugin for PrintDiagnosticsPlugin { if self.debug { app.add_system_to_stage( stage::POST_UPDATE, - Self::print_diagnostics_debug_system.system(), + Self::log_diagnostics_debug_system.system(), ); } else { - app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system.system()); + app.add_system_to_stage(stage::POST_UPDATE, Self::log_diagnostics_system.system()); } } } -impl PrintDiagnosticsPlugin { +impl LogDiagnosticsPlugin { pub fn filtered(filter: Vec) -> Self { - PrintDiagnosticsPlugin { + LogDiagnosticsPlugin { filter: Some(filter), ..Default::default() } } - fn print_diagnostic(diagnostic: &Diagnostic) { + fn log_diagnostic(diagnostic: &Diagnostic) { if let Some(value) = diagnostic.value() { - print!("{:<65}: {:<10.6}", diagnostic.name, value); if let Some(average) = diagnostic.average() { - print!(" (avg {:.6})", average); + info!( + "{:<65}: {:<10.6} (avg {:.6})", + diagnostic.name, value, average + ); + } else { + info!("{:<65}: {:<10.6}", diagnostic.name, value); } - - println!("\n"); } } - pub fn print_diagnostics_system( - mut state: ResMut, + fn log_diagnostics_system( + mut state: ResMut, time: Res