From 01c051731a06e885be099d04a207785d68939617 Mon Sep 17 00:00:00 2001 From: Jean Mertz Date: Wed, 24 Apr 2024 12:26:10 +0200 Subject: [PATCH] improved tracing Signed-off-by: Jean Mertz --- crates/butter/src/plugins/canvas.rs | 12 ++- crates/butter/src/plugins/canvas/place.rs | 103 ++++++++++------------ crates/butter/src/plugins/debug.rs | 4 +- crates/butter/src/prelude.rs | 4 +- crates/parser/src/lib.rs | 36 ++++---- 5 files changed, 79 insertions(+), 80 deletions(-) diff --git a/crates/butter/src/plugins/canvas.rs b/crates/butter/src/plugins/canvas.rs index c58a547..0e0fad6 100644 --- a/crates/butter/src/plugins/canvas.rs +++ b/crates/butter/src/plugins/canvas.rs @@ -127,7 +127,7 @@ fn update_text_computed_size( for (entity, mut size, layout) in sizes.iter_mut() { // The logical size of a text node can be zero, which we interpret as "unknown". if layout.logical_size == Vec2::ZERO { - if size.as_ref() == &ComputedSize::Inherit { + if !matches!(size.as_ref(), &ComputedSize::Static(_)) { continue; } } @@ -150,14 +150,18 @@ fn update_transformed_computed_size( mut sizes: Query<(Entity, &mut ComputedSize, &Transform), Changed>, ) { for (entity, mut size, transform) in sizes.iter_mut() { + let _span = trace_span!("Checking if entity needs computed size update", ?entity).entered(); + let old = *size.as_ref(); if size.set_if_neq(old.transformed(*transform)) { let new = size.as_ref(); - debug!( - ?entity, + trace!(?old, ?new, "Updated transformed ComputedSize component"); + } else { + let new = size.as_ref(); + trace!( ?old, ?new, - "Updated transformed ComputedSize component" + "ComputedSize matches new Transform, nothing to update" ); } } diff --git a/crates/butter/src/plugins/canvas/place.rs b/crates/butter/src/plugins/canvas/place.rs index 0fd6665..d744a32 100644 --- a/crates/butter/src/plugins/canvas/place.rs +++ b/crates/butter/src/plugins/canvas/place.rs @@ -45,10 +45,10 @@ impl Plugin for PlacePlugin { position_body.run_if(run_position_body), ) .chain(), - position_place.map(err), - position_relative_place + // position_place.map(err), + position_place .map(err) - .run_if(any_with_component::), + .run_if(any_with_component::), toggle_numbering.run_if(resource_changed::), focus_next.run_if(input_just_pressed(KeyCode::ArrowRight)), focus_last.run_if(input_just_pressed(KeyCode::ArrowLeft)), @@ -68,7 +68,7 @@ pub(super) struct Place; /// A place that requires placement relative to another place. #[derive(Component)] -struct RequiresRelativePlacement { +struct RequiresPositioning { x: Coordinate, y: Coordinate, } @@ -160,10 +160,8 @@ fn create( .map(|pos| (pos.x, pos.y)) .unwrap_or_else(|| (Coordinate::Absolute(0), Coordinate::Absolute(0))); - error!(?x, ?y, "Positions"); - cmd.entity(place) - .insert((RequiresRelativePlacement { x, y }, Visibility::Hidden)); + .insert((RequiresPositioning { x, y }, Visibility::Hidden)); let header = create_header( &mut cmd, @@ -378,13 +376,13 @@ fn redraw_underline( .ok() }) .for_each( - |(underline, mut sprite, mut translation, size)| match size { - Ok(Some(size)) => { + |(underline, mut sprite, mut translation, title_size)| match title_size { + Ok(Some(title_size)) => { if let Some(custom_size) = sprite.custom_size.as_mut() { - custom_size.x = size.x * (1. + UNDERLINE_STRETCH); + custom_size.x = title_size.x * (1. + UNDERLINE_STRETCH); } - translation.y = -size.y; + translation.y = -title_size.y; info!( ?underline, ?translation, @@ -392,7 +390,7 @@ fn redraw_underline( ); } Ok(None) => { - debug!(?underline, "Waiting on pending size.") + debug!(?underline, "Waiting on pending title size.") } Err(error) => error!(?underline, %error, "Unexpected error."), }, @@ -474,50 +472,50 @@ fn run_position_body( .any(|b| headers.iter().any(|h| h.get() == b.get())) } -fn position_place( - mut events: EventReader, - places: Query>, - sizes: ComputedSizeParam<()>, -) -> Result<(), Error> { - // Find any place for which any of its children has an updated computed size. - let mut places: Vec<_> = events - .read() - .map(|event| places.iter().filter(|place| event.contains(*place))) - .flatten() - .collect(); - - places.sort(); - places.dedup(); - - for place in places { - let Some(size) = sizes.size_of(place)? else { - continue; - }; - - error!(?size); - } - - Ok(()) -} +// fn position_place( +// mut events: EventReader, +// places: Query>, +// sizes: ComputedSizeParam<()>, +// ) -> Result<(), Error> { +// // Find any place for which any of its children has an updated computed size. +// let mut places: Vec<_> = events +// .read() +// .map(|event| places.iter().filter(|place| event.contains(*place))) +// .flatten() +// .collect(); +// +// places.sort(); +// places.dedup(); +// +// for place in places { +// let Some(size) = sizes.size_of(place)? else { +// continue; +// }; +// +// error!(?size); +// } +// +// Ok(()) +// } #[instrument(skip_all)] -fn position_relative_place( +fn position_place( mut cmd: Commands, - positioning: Query<(Entity, &RequiresRelativePlacement)>, + positioning: Query<(Entity, &RequiresPositioning)>, names: Query<(Entity, &Name)>, places: Query< Entity, ( With, With, - Without, + Without, ), >, sizes: ComputedSizeParam<()>, parent: Query<&Parent>, ) -> Result<(), Error> { - for (place, RequiresRelativePlacement { x, y }) in &positioning { - error!(?x, ?y); + for (place, RequiresPositioning { x, y }) in &positioning { + debug!(?place, ?x, ?y, "Positioning place."); let position = match (x, y) { (Coordinate::Absolute(x), Coordinate::Absolute(y)) => Vec2::new(*x as f32, *y as f32), @@ -547,7 +545,6 @@ fn position_relative_place( continue; }; - error!("Doing it y!"); pos.y = pos.y + *offset as f32 + 200.; Vec2::new(*x as f32, pos.y) @@ -578,7 +575,6 @@ fn position_relative_place( continue; }; - error!("Doing it x!"); pos.x = pos.x + *offset as f32; Vec2::new(pos.x, *y as f32) @@ -626,23 +622,18 @@ fn position_relative_place( let x = pos.x + offset_x + size.x; let y = pos.y + *offset_y as f32; - error!(?x, ?y, "Doing it x/y!"); Vec2::new(x, y) } }; - error!(?position, "Positioning?"); - - cmd.entity(place) - .remove::() - .insert(( - Transform { - translation: position.extend(0.0), - ..default() - }, - Visibility::Visible, - )); + cmd.entity(place).remove::().insert(( + Transform { + translation: position.extend(0.0), + ..default() + }, + Visibility::Visible, + )); } Ok(()) diff --git a/crates/butter/src/plugins/debug.rs b/crates/butter/src/plugins/debug.rs index af13ba9..bb1dd7b 100644 --- a/crates/butter/src/plugins/debug.rs +++ b/crates/butter/src/plugins/debug.rs @@ -43,12 +43,14 @@ impl DebugPlugin { .add_directive("wgpu_hal=error".parse().unwrap()) .add_directive("bevy_time::virt=error".parse().unwrap()) .add_directive("bevy_mod_raycast=error".parse().unwrap()) + .add_directive("bevy_egui=error".parse().unwrap()) .add_directive("naga=error".parse().unwrap()); tracing_subscriber::fmt() .with_env_filter(filter) - .with_span_events(FmtSpan::ENTER) + .with_span_events(FmtSpan::ENTER | FmtSpan::EXIT) .with_target(true) + .without_time() .with_line_number(true) .init(); } diff --git a/crates/butter/src/prelude.rs b/crates/butter/src/prelude.rs index 053bf72..17dfa08 100644 --- a/crates/butter/src/prelude.rs +++ b/crates/butter/src/prelude.rs @@ -28,4 +28,6 @@ pub(crate) use bevy_text::{Text2dBounds, TextLayoutInfo}; pub(crate) use bevy_transform::prelude::*; pub(crate) use bevy_turborand::{DelegatedRng as _, RngComponent}; pub(crate) use bevy_utils::prelude::*; -pub(crate) use tracing::{debug, error, field, info, info_span, instrument, trace, warn}; +pub(crate) use tracing::{ + debug, debug_span, error, field, info, info_span, instrument, trace, trace_span, warn, +}; diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 607c088..47b24a2 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -103,7 +103,7 @@ pub fn parse(input: &str) -> Result { Ok(Breadboard { places, components }) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_comment<'a>(chars: &mut Chars<'_>) -> Vec { let mut comment = vec![]; @@ -157,7 +157,7 @@ fn parse_place(chars: &mut Chars<'_>, description: Vec) -> Result) -> Result, Error> { let mut references = vec![]; @@ -184,7 +184,7 @@ fn parse_component_references(chars: &mut Chars<'_>) -> Result, Erro Ok(references) } -#[instrument(skip_all)] +#[instrument(level = "debug", skip_all)] fn parse_position(chars: &mut Chars<'_>) -> Result, Error> { skip_whitespace(chars); @@ -237,7 +237,7 @@ fn parse_position(chars: &mut Chars<'_>) -> Result, Error> { Ok(Some(Position { x, y })) } -#[instrument(skip_all)] +#[instrument(level = "debug", skip_all)] fn parse_coordinate(chars: &mut Chars<'_>) -> Result, Error> { parse_while(chars, |c| c.is_whitespace() && c != '\n'); @@ -312,7 +312,7 @@ fn parse_coordinate(chars: &mut Chars<'_>) -> Result, Error> Ok(Some(position)) } -#[instrument(skip_all)] +#[instrument(level = "debug", skip_all)] fn parse_sketch(chars: &mut Chars<'_>) -> Result, Error> { skip_whitespace(chars); @@ -343,7 +343,7 @@ fn parse_sketch(chars: &mut Chars<'_>) -> Result, Error> { Ok(Some(Sketch { path, areas })) } -#[instrument(skip_all)] +#[instrument(level = "debug", skip_all)] fn parse_area(chars: &mut Chars<'_>) -> Result { if chars.next() != Some('[') { return Err(Error::ExpectedSketchArea); @@ -389,7 +389,7 @@ fn parse_area(chars: &mut Chars<'_>) -> Result { }) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_int>(chars: &mut Chars<'_>) -> Result { let mut sign = '+'; if let Some(c) = chars.clone().next() { @@ -412,7 +412,7 @@ fn parse_int>(chars: &mut Chars<'_>) -> Result< .map_err(|e| Error::InvalidInteger(e.to_string())) } -#[instrument(skip_all)] +#[instrument(level = "debug", skip_all)] fn parse_affordances(chars: &mut Chars<'_>) -> Result, Error> { skip_whitespace(chars); @@ -458,7 +458,7 @@ fn parse_affordances(chars: &mut Chars<'_>) -> Result, Error> { Ok(affordances) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_connections(chars: &mut Chars<'_>) -> Result, Error> { let mut connections = vec![]; while chars.clone().next().is_some() { @@ -487,7 +487,7 @@ fn parse_connections(chars: &mut Chars<'_>) -> Result, Error> { Ok(connections) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_level<'a>(chars: &'a mut Chars<'_>) -> usize { // Don't do any implicit trimming, the first character should be a "level" character. if !chars.as_str().starts_with(">") { @@ -500,7 +500,7 @@ fn parse_level<'a>(chars: &'a mut Chars<'_>) -> usize { str.matches('>').count() } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_affordance_or_target_name<'a>(chars: &'a mut Chars<'_>) -> Result<&'a str, Error> { let str = chars.as_str(); @@ -519,7 +519,7 @@ fn parse_affordance_or_target_name<'a>(chars: &'a mut Chars<'_>) -> Result<&'a s Ok(str[..str.len() - chars.as_str().len()].trim()) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_connection_description(chars: &mut Chars<'_>) -> Result { if chars.next() != Some('(') { return Err(Error::ExpectedConnectionDescription); @@ -548,7 +548,7 @@ fn parse_connection_description(chars: &mut Chars<'_>) -> Result Ok(desc) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_quoted_string<'a>(chars: &'a mut Chars<'_>) -> Result<&'a str, Error> { match chars.next() { Some('"') => (), @@ -571,7 +571,7 @@ fn parse_quoted_string<'a>(chars: &'a mut Chars<'_>) -> Result<&'a str, Error> { Err(Error::UnterminatedQuotedString) } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_while<'a>(chars: &'a mut Chars<'_>, fun: impl Fn(char) -> bool) -> &'a str { let str = chars.as_str(); @@ -582,7 +582,7 @@ fn parse_while<'a>(chars: &'a mut Chars<'_>, fun: impl Fn(char) -> bool) -> &'a &str[..str.len() - chars.as_str().len()] } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_until<'a>(chars: &'a mut Chars<'_>, until: &str) -> &'a str { let str = chars.as_str(); @@ -593,7 +593,7 @@ fn parse_until<'a>(chars: &'a mut Chars<'_>, until: &str) -> &'a str { &str[..str.len() - chars.as_str().len()] } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_word<'a>(chars: &'a mut Chars<'_>) -> &'a str { let str = chars.as_str(); @@ -604,7 +604,7 @@ fn parse_word<'a>(chars: &'a mut Chars<'_>) -> &'a str { &str[..str.len() - chars.as_str().len()] } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn parse_line<'a>(chars: &'a mut Chars<'_>) -> &'a str { let str = chars.as_str(); @@ -615,7 +615,7 @@ fn parse_line<'a>(chars: &'a mut Chars<'_>) -> &'a str { &str[..str.len() - chars.as_str().len()] } -#[instrument(skip_all)] +#[instrument(level = "trace", skip_all)] fn skip_whitespace(chars: &mut Chars<'_>) { while chars.clone().next().map_or(false, char::is_whitespace) { chars.next();