Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual improvements to log_layers_ecs example #15949

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions examples/app/log_layers_ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@ 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,
}))
.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
Expand Down Expand Up @@ -59,10 +74,13 @@ impl<S: Subscriber> Layer<S> 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!");
}
}
Expand Down Expand Up @@ -91,25 +109,14 @@ fn custom_layer(app: &mut App) -> Option<BoxedLayer> {
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)]
Expand All @@ -124,6 +131,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()
Expand All @@ -143,7 +151,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,
})
}