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-12-16 05:57:16 +00:00
|
|
|
use bevy_ecs::{IntoSystem, Res, ResMut};
|
2020-12-24 19:28:31 +00:00
|
|
|
use bevy_log::{debug, info};
|
2020-11-22 00:38:24 +00:00
|
|
|
use bevy_utils::Duration;
|
2020-05-04 20:42:49 +00:00
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
/// An App Plugin that logs diagnostics to the console
|
|
|
|
pub struct LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
pub debug: bool,
|
|
|
|
pub wait_duration: Duration,
|
|
|
|
pub filter: Option<Vec<DiagnosticId>>,
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
/// State used by the [LogDiagnosticsPlugin]
|
|
|
|
struct LogDiagnosticsState {
|
2020-06-04 02:53:41 +00:00
|
|
|
timer: Timer,
|
2020-05-04 20:42:49 +00:00
|
|
|
filter: Option<Vec<DiagnosticId>>,
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
impl Default for LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
fn default() -> Self {
|
2020-12-24 19:28:31 +00:00
|
|
|
LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
debug: false,
|
|
|
|
wait_duration: Duration::from_secs(1),
|
|
|
|
filter: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
impl Plugin for LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
fn build(&self, app: &mut bevy_app::AppBuilder) {
|
2020-12-24 19:28:31 +00:00
|
|
|
app.add_resource(LogDiagnosticsState {
|
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-12-16 05:57:16 +00:00
|
|
|
app.add_system_to_stage(
|
|
|
|
stage::POST_UPDATE,
|
2020-12-24 19:28:31 +00:00
|
|
|
Self::log_diagnostics_debug_system.system(),
|
2020-12-16 05:57:16 +00:00
|
|
|
);
|
2020-05-04 20:42:49 +00:00
|
|
|
} else {
|
2020-12-24 19:28:31 +00:00
|
|
|
app.add_system_to_stage(stage::POST_UPDATE, Self::log_diagnostics_system.system());
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
impl LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
pub fn filtered(filter: Vec<DiagnosticId>) -> Self {
|
2020-12-24 19:28:31 +00:00
|
|
|
LogDiagnosticsPlugin {
|
2020-05-04 20:42:49 +00:00
|
|
|
filter: Some(filter),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
fn log_diagnostic(diagnostic: &Diagnostic) {
|
2020-05-04 20:42:49 +00:00
|
|
|
if let Some(value) = diagnostic.value() {
|
|
|
|
if let Some(average) = diagnostic.average() {
|
2020-12-24 19:28:31 +00:00
|
|
|
info!(
|
|
|
|
"{:<65}: {:<10.6} (avg {:.6})",
|
|
|
|
diagnostic.name, value, average
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!("{:<65}: {:<10.6}", diagnostic.name, value);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
fn log_diagnostics_system(
|
|
|
|
mut state: ResMut<LogDiagnosticsState>,
|
2020-05-14 00:52:47 +00:00
|
|
|
time: Res<Time>,
|
|
|
|
diagnostics: Res<Diagnostics>,
|
2020-05-04 20:42:49 +00:00
|
|
|
) {
|
2020-11-28 21:08:31 +00:00
|
|
|
if state.timer.tick(time.delta_seconds()).finished() {
|
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()) {
|
2020-12-24 19:28:31 +00:00
|
|
|
Self::log_diagnostic(diagnostic);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-24 19:28:31 +00:00
|
|
|
for diagnostic in diagnostics.ordered_iter() {
|
|
|
|
Self::log_diagnostic(diagnostic);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:28:31 +00:00
|
|
|
fn log_diagnostics_debug_system(
|
|
|
|
mut state: ResMut<LogDiagnosticsState>,
|
2020-05-14 00:52:47 +00:00
|
|
|
time: Res<Time>,
|
|
|
|
diagnostics: Res<Diagnostics>,
|
2020-05-04 20:42:49 +00:00
|
|
|
) {
|
2020-11-28 21:08:31 +00:00
|
|
|
if state.timer.tick(time.delta_seconds()).finished() {
|
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()) {
|
2020-12-24 19:28:31 +00:00
|
|
|
debug!("{:#?}\n", diagnostic);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-24 19:28:31 +00:00
|
|
|
for diagnostic in diagnostics.ordered_iter() {
|
|
|
|
debug!("{:#?}\n", diagnostic);
|
2020-05-04 20:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|