bevy/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

106 lines
3.1 KiB
Rust
Raw Normal View History

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::*;
use bevy_core::{Time, Timer};
use bevy_ecs::{IntoSystem, Res, ResMut};
use bevy_log::{debug, info};
use bevy_utils::Duration;
2020-05-04 20:42:49 +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>>,
}
/// 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>>,
}
impl Default for LogDiagnosticsPlugin {
2020-05-04 20:42:49 +00:00
fn default() -> Self {
LogDiagnosticsPlugin {
2020-05-04 20:42:49 +00:00
debug: false,
wait_duration: Duration::from_secs(1),
filter: None,
}
}
}
impl Plugin for LogDiagnosticsPlugin {
2020-05-04 20:42:49 +00:00
fn build(&self, app: &mut bevy_app::AppBuilder) {
app.insert_resource(LogDiagnosticsState {
timer: Timer::new(self.wait_duration, true),
2020-05-04 20:42:49 +00:00
filter: self.filter.clone(),
});
if self.debug {
app.add_system_to_stage(
stage::POST_UPDATE,
Self::log_diagnostics_debug_system.system(),
);
2020-05-04 20:42:49 +00:00
} else {
app.add_system_to_stage(stage::POST_UPDATE, Self::log_diagnostics_system.system());
2020-05-04 20:42:49 +00:00
}
}
}
impl LogDiagnosticsPlugin {
2020-05-04 20:42:49 +00:00
pub fn filtered(filter: Vec<DiagnosticId>) -> Self {
LogDiagnosticsPlugin {
2020-05-04 20:42:49 +00:00
filter: Some(filter),
..Default::default()
}
}
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() {
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
}
}
}
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
) {
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()) {
Self::log_diagnostic(diagnostic);
2020-05-04 20:42:49 +00:00
}
} else {
for diagnostic in diagnostics.iter() {
Self::log_diagnostic(diagnostic);
2020-05-04 20:42:49 +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
) {
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()) {
debug!("{:#?}\n", diagnostic);
2020-05-04 20:42:49 +00:00
}
} else {
for diagnostic in diagnostics.iter() {
debug!("{:#?}\n", diagnostic);
2020-05-04 20:42:49 +00:00
}
}
}
}
}