Skip to content

Commit

Permalink
add more logs when despawning entities (bevyengine#3851)
Browse files Browse the repository at this point in the history
# 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
```
  • Loading branch information
mockersf authored and ItsDoot committed Feb 1, 2023
1 parent 655580e commit 6951c35
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 34 additions & 2 deletions crates/bevy_hierarchy/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 6951c35

Please sign in to comment.