2020-05-04 20:42:49 +00:00
|
|
|
use super::{Diagnostic, DiagnosticId, Diagnostics};
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::prelude::*;
|
2020-07-17 01:26:21 +00:00
|
|
|
use bevy_core::{Time, Timer};
|
2020-11-17 02:18:00 +00:00
|
|
|
use bevy_ecs::{Res, ResMut};
|
2020-11-22 00:38:24 +00:00
|
|
|
use bevy_utils::Duration;
|
2020-05-04 20:42:49 +00:00
|
|
|
|
2020-08-16 03:27:41 +00:00
|
|
|
/// An App Plugin that prints diagnostics to the console
|
2020-05-04 20:42:49 +00:00
|
|
|
pub struct PrintDiagnosticsPlugin {
|
|
|
|
pub debug: bool,
|
|
|
|
pub wait_duration: Duration,
|
|
|
|
pub filter: Option<Vec<DiagnosticId>>,
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// State used by the [PrintDiagnosticsPlugin]
|
2020-05-04 20:42:49 +00:00
|
|
|
pub struct PrintDiagnosticsState {
|
2020-06-04 02:53:41 +00:00
|
|
|
timer: Timer,
|
2020-05-04 20:42:49 +00:00
|
|
|
filter: Option<Vec<DiagnosticId>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PrintDiagnosticsPlugin {
|
|
|
|
fn default() -> Self {
|
|
|
|
PrintDiagnosticsPlugin {
|
|
|
|
debug: false,
|
|
|
|
wait_duration: Duration::from_secs(1),
|
|
|
|
filter: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for PrintDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
fn build(&self, app: &mut bevy_app::AppBuilder) {
|
|
|
|
app.add_resource(PrintDiagnosticsState {
|
2020-08-21 21:57:25 +00:00
|
|
|
timer: Timer::new(self.wait_duration, true),
|
2020-05-04 20:42:49 +00:00
|
|
|
filter: self.filter.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if self.debug {
|
2020-11-17 02:18:00 +00:00
|
|
|
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_debug_system);
|
2020-05-04 20:42:49 +00:00
|
|
|
} else {
|
2020-11-17 02:18:00 +00:00
|
|
|
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrintDiagnosticsPlugin {
|
|
|
|
pub fn filtered(filter: Vec<DiagnosticId>) -> Self {
|
|
|
|
PrintDiagnosticsPlugin {
|
|
|
|
filter: Some(filter),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_diagnostic(diagnostic: &Diagnostic) {
|
|
|
|
if let Some(value) = diagnostic.value() {
|
2020-06-28 07:45:35 +00:00
|
|
|
print!("{:<65}: {:<10.6}", diagnostic.name, value);
|
2020-05-04 20:42:49 +00:00
|
|
|
if let Some(average) = diagnostic.average() {
|
|
|
|
print!(" (avg {:.6})", average);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_diagnostics_system(
|
2020-05-14 00:52:47 +00:00
|
|
|
mut state: ResMut<PrintDiagnosticsState>,
|
|
|
|
time: Res<Time>,
|
|
|
|
diagnostics: Res<Diagnostics>,
|
2020-05-04 20:42:49 +00:00
|
|
|
) {
|
2020-11-27 19:39:33 +00:00
|
|
|
if state.timer.tick(time.delta_seconds).finished() {
|
2020-05-04 20:42:49 +00:00
|
|
|
println!("Diagnostics:");
|
2020-06-28 07:45:35 +00:00
|
|
|
println!("{}", "-".repeat(93));
|
2020-05-04 20:42:49 +00:00
|
|
|
if let Some(ref filter) = state.filter {
|
|
|
|
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
|
|
|
|
Self::print_diagnostic(diagnostic);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for diagnostic in diagnostics.iter() {
|
|
|
|
Self::print_diagnostic(diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_diagnostics_debug_system(
|
2020-05-14 00:52:47 +00:00
|
|
|
mut state: ResMut<PrintDiagnosticsState>,
|
|
|
|
time: Res<Time>,
|
|
|
|
diagnostics: Res<Diagnostics>,
|
2020-05-04 20:42:49 +00:00
|
|
|
) {
|
2020-11-27 19:39:33 +00:00
|
|
|
if state.timer.tick(time.delta_seconds).finished() {
|
2020-05-04 20:42:49 +00:00
|
|
|
println!("Diagnostics (Debug):");
|
2020-06-28 07:45:35 +00:00
|
|
|
println!("{}", "-".repeat(93));
|
2020-05-04 20:42:49 +00:00
|
|
|
if let Some(ref filter) = state.filter {
|
|
|
|
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
|
|
|
|
println!("{:#?}\n", diagnostic);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for diagnostic in diagnostics.iter() {
|
|
|
|
println!("{:#?}\n", diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|