From 8630b194dc63a18f16effc8d4feb9eda31d4280a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 13 Apr 2022 23:35:28 +0000 Subject: [PATCH] add more logs when despawning entities (#3851) # Objective - Provide more information when despawning an entity ## Solution - Add a debug log when despawning an entity - Add spans to the recursive ways of despawning an entity ```sh RUST_LOG=debug cargo run --example panic --features trace # RUST_LOG=debug needed to show debug logs from bevy_ecs # --features trace needed to have the extra spans ... DEBUG bevy_app:frame:stage{name=Update}:system_commands{name="panic::despawn_parent"}:command{name="DespawnRecursive" entity=0v0}: bevy_ecs::world: Despawning entity 1v0 DEBUG bevy_app:frame:stage{name=Update}:system_commands{name="panic::despawn_parent"}:command{name="DespawnRecursive" entity=0v0}: bevy_ecs::world: Despawning entity 0v0 ``` --- crates/bevy_ecs/src/world/mod.rs | 3 ++- crates/bevy_hierarchy/src/hierarchy.rs | 36 ++++++++++++++++++++++++-- crates/bevy_internal/Cargo.toml | 9 ++++++- crates/bevy_transform/Cargo.toml | 3 +++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index f47e88e9b8bfd..24c7db76f15a7 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -17,13 +17,13 @@ use crate::{ storage::{Column, SparseSet, Storages}, system::Resource, }; +use bevy_utils::tracing::debug; use std::{ any::TypeId, fmt, mem::ManuallyDrop, sync::atomic::{AtomicU32, Ordering}, }; - mod identifier; pub use identifier::WorldId; @@ -463,6 +463,7 @@ impl World { /// ``` #[inline] pub fn despawn(&mut self, entity: Entity) -> bool { + debug!("Despawning entity {:?}", entity); self.get_entity_mut(entity) .map(|e| { e.despawn(); diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index 56c9b0f8ad938..4d6753e719089 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -9,13 +9,15 @@ use bevy_utils::tracing::debug; /// Despawns the given entity and all its children recursively #[derive(Debug)] pub struct DespawnRecursive { - entity: Entity, + /// Target entity + pub entity: Entity, } /// Despawns the given entity's children recursively #[derive(Debug)] pub struct DespawnChildrenRecursive { - entity: Entity, + /// Target entity + pub entity: Entity, } /// Function for despawning an entity and all its children @@ -54,12 +56,26 @@ fn despawn_children(world: &mut World, entity: Entity) { impl Command for DespawnRecursive { fn write(self, world: &mut World) { + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!( + "command", + name = "DespawnRecursive", + entity = bevy_utils::tracing::field::debug(self.entity) + ) + .entered(); despawn_with_children_recursive(world, self.entity); } } impl Command for DespawnChildrenRecursive { fn write(self, world: &mut World) { + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!( + "command", + name = "DespawnChildrenRecursive", + entity = bevy_utils::tracing::field::debug(self.entity) + ) + .entered(); despawn_children(world, self.entity); } } @@ -90,6 +106,14 @@ impl<'w> DespawnRecursiveExt for EntityMut<'w> { /// Despawns the provided entity and its children. fn despawn_recursive(mut self) { let entity = self.id(); + + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!( + "despawn_recursive", + entity = bevy_utils::tracing::field::debug(entity) + ) + .entered(); + // SAFE: EntityMut is consumed so even though the location is no longer // valid, it cannot be accessed again with the invalid location. unsafe { @@ -99,6 +123,14 @@ impl<'w> DespawnRecursiveExt for EntityMut<'w> { fn despawn_descendants(&mut self) { let entity = self.id(); + + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!( + "despawn_descendants", + entity = bevy_utils::tracing::field::debug(entity) + ) + .entered(); + // SAFE: The location is updated. unsafe { despawn_children(self.world_mut(), entity); diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index c2a560fec1371..e48722beecad0 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -10,7 +10,14 @@ keywords = ["game", "engine", "gamedev", "graphics", "bevy"] categories = ["game-engines", "graphics", "gui", "rendering"] [features] -trace = [ "bevy_app/trace", "bevy_ecs/trace", "bevy_log/trace", "bevy_render/trace", "bevy_core_pipeline/trace" ] +trace = [ + "bevy_app/trace", + "bevy_core_pipeline/trace", + "bevy_ecs/trace", + "bevy_log/trace", + "bevy_render/trace", + "bevy_transform/trace" +] trace_chrome = [ "bevy_log/tracing-chrome" ] trace_tracy = ["bevy_render/tracing-tracy", "bevy_log/tracing-tracy" ] wgpu_trace = ["bevy_render/wgpu_trace"] diff --git a/crates/bevy_transform/Cargo.toml b/crates/bevy_transform/Cargo.toml index 572f9378ca347..0102cbf203c07 100644 --- a/crates/bevy_transform/Cargo.toml +++ b/crates/bevy_transform/Cargo.toml @@ -8,6 +8,9 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT OR Apache-2.0" keywords = ["bevy"] +[features] +trace = [] + [dependencies] # bevy bevy_app = { path = "../bevy_app", version = "0.7.0-dev" }