Skip to content

Commit 739224f

Browse files
committed
fix diagnostic length for asset count (#2165)
fixes #2156 limit the diagnostic name to `MAX_DIAGNOSTIC_NAME_WIDTH` length
1 parent b4f80c2 commit 739224f

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{Asset, Assets};
22
use bevy_app::prelude::*;
3-
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics};
3+
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics, MAX_DIAGNOSTIC_NAME_WIDTH};
44
use bevy_ecs::system::{IntoSystem, Res, ResMut};
55

66
/// Adds "asset count" diagnostic to an App
@@ -29,9 +29,20 @@ impl<T: Asset> AssetCountDiagnosticsPlugin<T> {
2929
}
3030

3131
pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
32+
let asset_type_name = std::any::type_name::<T>();
33+
let max_length = MAX_DIAGNOSTIC_NAME_WIDTH - "asset_count ".len();
3234
diagnostics.add(Diagnostic::new(
3335
Self::diagnostic_id(),
34-
format!("asset_count {}", std::any::type_name::<T>()),
36+
format!(
37+
"asset_count {}",
38+
if asset_type_name.len() > max_length {
39+
asset_type_name
40+
.split_at(asset_type_name.len() - max_length + 1)
41+
.1
42+
} else {
43+
asset_type_name
44+
}
45+
),
3546
20,
3647
));
3748
}

crates/bevy_diagnostic/src/diagnostic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy_log::warn;
22
use bevy_utils::{Duration, Instant, StableHashMap, Uuid};
33
use std::{borrow::Cow, collections::VecDeque};
44

5-
use crate::log_diagnostics_plugin::MAX_LOG_NAME_WIDTH;
5+
use crate::MAX_DIAGNOSTIC_NAME_WIDTH;
66

77
/// Unique identifier for a [Diagnostic]
88
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
@@ -59,12 +59,12 @@ impl Diagnostic {
5959
max_history_length: usize,
6060
) -> Diagnostic {
6161
let name = name.into();
62-
if name.chars().count() > MAX_LOG_NAME_WIDTH {
62+
if name.chars().count() > MAX_DIAGNOSTIC_NAME_WIDTH {
6363
// This could be a false positive due to a unicode width being shorter
6464
warn!(
6565
"Diagnostic {:?} has name longer than {} characters, and so might overflow in the LogDiagnosticsPlugin\
6666
Consider using a shorter name.",
67-
name, MAX_LOG_NAME_WIDTH
67+
name, MAX_DIAGNOSTIC_NAME_WIDTH
6868
)
6969
}
7070
Diagnostic {

crates/bevy_diagnostic/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ impl Plugin for DiagnosticsPlugin {
1818
app.init_resource::<Diagnostics>();
1919
}
2020
}
21+
22+
/// The width which diagnostic names will be printed as
23+
/// Plugin names should not be longer than this value
24+
pub const MAX_DIAGNOSTIC_NAME_WIDTH: usize = 32;

crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ impl Default for LogDiagnosticsPlugin {
2828
}
2929
}
3030

31-
/// The width which diagnostic names will be printed as
32-
/// Plugin names should not be longer than this value
33-
pub(crate) const MAX_LOG_NAME_WIDTH: usize = 32;
34-
3531
impl Plugin for LogDiagnosticsPlugin {
3632
fn build(&self, app: &mut bevy_app::AppBuilder) {
3733
app.insert_resource(LogDiagnosticsState {
@@ -71,15 +67,15 @@ impl LogDiagnosticsPlugin {
7167
// Do not reserve one column for the suffix in the average
7268
// The ) hugging the value is more aesthetically pleasing
7369
format!("{:.6}{:}", average, diagnostic.suffix),
74-
name_width = MAX_LOG_NAME_WIDTH,
70+
name_width = crate::MAX_DIAGNOSTIC_NAME_WIDTH,
7571
);
7672
} else {
7773
info!(
7874
target: "bevy diagnostic",
7975
"{:<name_width$}: {:>}",
8076
diagnostic.name,
8177
format!("{:.6}{:}", value, diagnostic.suffix),
82-
name_width = MAX_LOG_NAME_WIDTH,
78+
name_width = crate::MAX_DIAGNOSTIC_NAME_WIDTH,
8379
);
8480
}
8581
}

0 commit comments

Comments
 (0)