From 9b00f26e2f7e013b56e99da3cd7bce8d7a30a56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Fri, 18 Dec 2020 02:28:19 +0100 Subject: [PATCH 1/7] move print diagnostics to log --- Cargo.toml | 4 +- crates/bevy_diagnostic/Cargo.toml | 1 + crates/bevy_diagnostic/src/lib.rs | 4 +- ...cs_plugin.rs => log_diagnostics_plugin.rs} | 57 +++++++++---------- examples/3d/spawner.rs | 4 +- examples/README.md | 2 +- examples/app/plugin_group.rs | 2 +- examples/diagnostics/custom_diagnostic.rs | 4 +- ...rint_diagnostics.rs => log_diagnostics.rs} | 4 +- 9 files changed, 41 insertions(+), 41 deletions(-) rename crates/bevy_diagnostic/src/{print_diagnostics_plugin.rs => log_diagnostics_plugin.rs} (59%) rename examples/diagnostics/{print_diagnostics.rs => log_diagnostics.rs} (79%) diff --git a/Cargo.toml b/Cargo.toml index 207cd12341165..bac8fe0fd1456 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -190,8 +190,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_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/lib.rs b/crates/bevy_diagnostic/src/lib.rs index d54c32663ca62..44620b9193156 100644 --- a/crates/bevy_diagnostic/src/lib.rs +++ b/crates/bevy_diagnostic/src/lib.rs @@ -1,9 +1,9 @@ mod diagnostic; mod frame_time_diagnostics_plugin; -mod print_diagnostics_plugin; +mod log_diagnostics_plugin; pub use diagnostic::*; 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 59% rename from crates/bevy_diagnostic/src/print_diagnostics_plugin.rs rename to crates/bevy_diagnostic/src/log_diagnostics_plugin.rs index e71f4b9066634..dd4aeeea02344 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] +pub 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, + pub fn log_diagnostics_system( + mut state: ResMut, time: Res