From 2dea42893357802d3802592d0ea7e9e6e65be8c2 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 16 Oct 2024 07:01:31 -0700 Subject: [PATCH 1/4] Visual improvements to log_layers_ecs example --- examples/app/log_layers_ecs.rs | 72 +++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/examples/app/log_layers_ecs.rs b/examples/app/log_layers_ecs.rs index 41e69e522ae72..3a496974806c8 100644 --- a/examples/app/log_layers_ecs.rs +++ b/examples/app/log_layers_ecs.rs @@ -16,16 +16,32 @@ use std::sync::mpsc; use bevy::{ log::{ tracing_subscriber::{self, Layer}, - BoxedLayer, + BoxedLayer, Level, }, prelude::*, - utils::{tracing, tracing::Subscriber}, + utils::tracing::{self, Subscriber}, }; +fn main() { + App::new() + .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin { + // Show logs all the way up to the trace level, but only for logs + // produced by this example. + level: Level::TRACE, + filter: "warn,log_layers_ecs=trace".to_string(), + custom_layer, + ..default() + })) + .add_systems(Startup, (log_system, setup)) + .add_systems(Update, print_logs) + .run(); +} + /// A basic message. This is what we will be sending from the [`CaptureLayer`] to [`CapturedLogEvents`] non-send resource. #[derive(Debug, Event)] struct LogEvent { message: String, + level: Level, } /// This non-send resource temporarily stores [`LogEvent`]s before they are @@ -59,10 +75,13 @@ impl Layer for CaptureLayer { let mut message = None; event.record(&mut CaptureLayerVisitor(&mut message)); if let Some(message) = message { - // You can obtain metadata like this, but we wont use it for this example. - let _metadata = event.metadata(); + let metadata = event.metadata(); + self.sender - .send(LogEvent { message }) + .send(LogEvent { + message, + level: *metadata.level(), + }) .expect("LogEvents resource no longer exists!"); } } @@ -91,25 +110,14 @@ fn custom_layer(app: &mut App) -> Option { Some(layer.boxed()) } -fn main() { - App::new() - .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin { - custom_layer, - ..default() - })) - .add_systems(Startup, (log_system, setup)) - .add_systems(Update, print_logs) - .run(); -} - fn log_system() { - // here is how you write new logs at each "log level" (in "most important" to + // Here is how you write new logs at each "log level" (in "most important" to // "least important" order) - error!("something failed"); - warn!("something bad happened that isn't a failure, but thats worth calling out"); - info!("helpful information that is worth printing by default"); - debug!("helpful for debugging"); - trace!("very noisy"); + error!("Something failed"); + warn!("Something bad happened that isn't a failure, but thats worth calling out"); + info!("Helpful information that is worth printing by default"); + debug!("Helpful for debugging"); + trace!("Very noisy"); } #[derive(Component)] @@ -124,6 +132,7 @@ fn setup(mut commands: Commands) { width: Val::Vw(100.0), height: Val::Vh(100.0), flex_direction: FlexDirection::Column, + padding: UiRect::all(Val::Px(12.)), ..default() }, ..default() @@ -143,7 +152,24 @@ fn print_logs( commands.entity(root_entity).with_children(|child| { for event in events.read() { - child.spawn(Text::new(&event.message)); + child.spawn(Text::default()).with_children(|child| { + child.spawn(( + TextSpan::new(format!("{:5} ", event.level)), + TextColor(level_color(&event.level)), + )); + child.spawn(TextSpan::new(&event.message)); + }); } }); } + +fn level_color(level: &Level) -> Color { + use bevy::color::palettes::tailwind::*; + Color::from(match *level { + Level::WARN => ORANGE_400, + Level::ERROR => RED_400, + Level::INFO => GREEN_400, + Level::TRACE => PURPLE_400, + Level::DEBUG => BLUE_400, + }) +} From f68962e2aa66c0bf9b27053e4d1edc68a975e2f8 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 16 Oct 2024 07:07:18 -0700 Subject: [PATCH 2/4] Bring back bevy's system info logs --- examples/app/log_layers_ecs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/app/log_layers_ecs.rs b/examples/app/log_layers_ecs.rs index 3a496974806c8..ccdb302f6d7fe 100644 --- a/examples/app/log_layers_ecs.rs +++ b/examples/app/log_layers_ecs.rs @@ -28,7 +28,7 @@ fn main() { // Show logs all the way up to the trace level, but only for logs // produced by this example. level: Level::TRACE, - filter: "warn,log_layers_ecs=trace".to_string(), + filter: "warn,bevy=info,log_layers_ecs=trace".to_string(), custom_layer, ..default() })) From 784b366c4120cbf7ee565203e9c80d2359ba3808 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 16 Oct 2024 07:09:47 -0700 Subject: [PATCH 3/4] Revert "Bring back bevy's system info logs" This reverts commit f68962e2aa66c0bf9b27053e4d1edc68a975e2f8. These logs with OS version, window entity id, etc will make the example visually "flaky" in CI. --- examples/app/log_layers_ecs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/app/log_layers_ecs.rs b/examples/app/log_layers_ecs.rs index ccdb302f6d7fe..3a496974806c8 100644 --- a/examples/app/log_layers_ecs.rs +++ b/examples/app/log_layers_ecs.rs @@ -28,7 +28,7 @@ fn main() { // Show logs all the way up to the trace level, but only for logs // produced by this example. level: Level::TRACE, - filter: "warn,bevy=info,log_layers_ecs=trace".to_string(), + filter: "warn,log_layers_ecs=trace".to_string(), custom_layer, ..default() })) From ed5bc5a3b04d762650c7bb163ad26732e03190cc Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 16 Oct 2024 07:33:38 -0700 Subject: [PATCH 4/4] Clippy --- examples/app/log_layers_ecs.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/app/log_layers_ecs.rs b/examples/app/log_layers_ecs.rs index 3a496974806c8..4577f237020c5 100644 --- a/examples/app/log_layers_ecs.rs +++ b/examples/app/log_layers_ecs.rs @@ -30,7 +30,6 @@ fn main() { level: Level::TRACE, filter: "warn,log_layers_ecs=trace".to_string(), custom_layer, - ..default() })) .add_systems(Startup, (log_system, setup)) .add_systems(Update, print_logs)