1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![deny(
    warnings,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications,
    missing_docs
)]

//! log diagnostics during run

use bevy::core::{Time, Timer};
use bevy::diagnostic::{Diagnostic, DiagnosticId, Diagnostics};
use bevy::ecs::{IntoQuerySystem, Res, ResMut};
use bevy::prelude::stage;
use std::time::Duration;
use tracing::debug;

/// An App Plugin that prints diagnostics to the console
pub struct LogDiagnosticsPlugin {
    /// How often to log diagnostics, by default every seconds
    pub wait_duration: Duration,
    /// Filter for specific diagnostics
    pub filter: Option<Vec<DiagnosticId>>,
}

/// State used by the [LogDiagnosticsPlugin]
pub struct LogDiagnosticsState {
    timer: Timer,
    filter: Option<Vec<DiagnosticId>>,
}

impl Default for LogDiagnosticsPlugin {
    fn default() -> Self {
        LogDiagnosticsPlugin {
            wait_duration: Duration::from_secs(1),
            filter: None,
        }
    }
}

impl bevy::prelude::Plugin for LogDiagnosticsPlugin {
    fn build(&self, app: &mut bevy::app::AppBuilder) {
        app.add_resource(LogDiagnosticsState {
            timer: Timer::new(self.wait_duration, true),
            filter: self.filter.clone(),
        });

        app.add_system_to_stage(stage::POST_UPDATE, Self::log_diagnostics_system.system());
    }
}

impl LogDiagnosticsPlugin {
    /// Create a new `LogDiagnosticsPlugin` that will filter on the given diagnostics
    pub fn filtered(filter: Vec<DiagnosticId>) -> Self {
        LogDiagnosticsPlugin {
            filter: Some(filter),
            ..Default::default()
        }
    }

    fn log_diagnostic(diagnostic: &Diagnostic) {
        if let Some(value) = diagnostic.value() {
            if let Some(average) = diagnostic.average() {
                debug!(
                    "diagnostic: {:<25}: {:<10.4}  (avg {:.4})",
                    diagnostic.name, value, average
                );
            } else {
                debug!("diagnostic: {:<25}: {:<10.4}", diagnostic.name, value);
            }
        }
    }

    fn log_diagnostics_system(
        mut state: ResMut<LogDiagnosticsState>,
        time: Res<Time>,
        diagnostics: Res<Diagnostics>,
    ) {
        state.timer.tick(time.delta_seconds);
        if state.timer.finished {
            if let Some(ref filter) = state.filter {
                for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
                    Self::log_diagnostic(diagnostic);
                }
            } else {
                for diagnostic in diagnostics.iter() {
                    Self::log_diagnostic(diagnostic);
                }
            }
        }
    }
}