Skip to content

Commit ee7fa0f

Browse files
authored
Merge pull request #103 from ryanoneill/fix/component-tracing
Add tracing instrumentation to Component trait
2 parents 1885458 + 9f414f2 commit ee7fa0f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/component/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,25 @@ pub trait Component: Sized {
333333
/// [`Theme::nord()`] for the Nord color palette.
334334
fn view(state: &Self::State, frame: &mut Frame, area: Rect, theme: &Theme);
335335

336+
/// Renders the component with optional tracing instrumentation.
337+
///
338+
/// When the `tracing` feature is enabled, this emits a trace-level span
339+
/// around the [`view`](Component::view) call with the component type name
340+
/// and render area dimensions. When the feature is disabled, this is
341+
/// identical to calling `view` directly.
342+
fn traced_view(state: &Self::State, frame: &mut Frame, area: Rect, theme: &Theme) {
343+
#[cfg(feature = "tracing")]
344+
tracing::trace!(
345+
component = std::any::type_name::<Self>(),
346+
area.x = area.x,
347+
area.y = area.y,
348+
area.width = area.width,
349+
area.height = area.height,
350+
"view: rendering component"
351+
);
352+
Self::view(state, frame, area, theme);
353+
}
354+
336355
/// Maps an input event to a component message.
337356
///
338357
/// This is the read-only half of event handling. It inspects the
@@ -356,7 +375,19 @@ pub trait Component: Sized {
356375
/// This is the primary method users should call for event routing.
357376
fn dispatch_event(state: &mut Self::State, event: &Event) -> Option<Self::Output> {
358377
if let Some(msg) = Self::handle_event(state, event) {
359-
Self::update(state, msg)
378+
#[cfg(feature = "tracing")]
379+
tracing::debug!(
380+
component = std::any::type_name::<Self>(),
381+
"dispatch_event: updating state"
382+
);
383+
let output = Self::update(state, msg);
384+
#[cfg(feature = "tracing")]
385+
tracing::trace!(
386+
component = std::any::type_name::<Self>(),
387+
has_output = output.is_some(),
388+
"dispatch_event: update complete"
389+
);
390+
output
360391
} else {
361392
None
362393
}

0 commit comments

Comments
 (0)