2024-03-04 00:01:05 +00:00
|
|
|
//! This example illustrates how to transfer log events from the [`Layer`] to Bevy's ECS.
|
|
|
|
//!
|
|
|
|
//! The way we will do this is via a [`mpsc`] channel. [`mpsc`] channels allow 2 unrelated
|
|
|
|
//! parts of the program to communicate (in this case, [`Layer`]s and Bevy's ECS).
|
|
|
|
//!
|
2024-05-12 21:16:56 +00:00
|
|
|
//! Inside the `custom_layer` function we will create a [`mpsc::Sender`] and a [`mpsc::Receiver`] from a
|
2024-04-14 15:23:44 +00:00
|
|
|
//! [`mpsc::channel`]. The [`Sender`](mpsc::Sender) will go into the `AdvancedLayer` and the [`Receiver`](mpsc::Receiver) will
|
|
|
|
//! go into a non-send resource called `LogEvents` (It has to be non-send because [`Receiver`](mpsc::Receiver) is [`!Sync`](Sync)).
|
|
|
|
//! From there we will use `transfer_log_events` to transfer log events from `LogEvents` to an ECS event called `LogEvent`.
|
2024-03-04 00:01:05 +00:00
|
|
|
//!
|
2024-04-14 15:23:44 +00:00
|
|
|
//! Finally, after all that we can access the `LogEvent` event from our systems and use it.
|
2024-03-04 00:01:05 +00:00
|
|
|
//! In this example we build a simple log viewer.
|
|
|
|
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
use bevy::{
|
2024-09-24 11:42:59 +00:00
|
|
|
log::{
|
|
|
|
tracing_subscriber::{self, Layer},
|
|
|
|
BoxedLayer,
|
|
|
|
},
|
2024-03-04 00:01:05 +00:00
|
|
|
prelude::*,
|
2024-09-24 11:42:59 +00:00
|
|
|
utils::{tracing, tracing::Subscriber},
|
2024-03-04 00:01:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This non-send resource temporarily stores [`LogEvent`]s before they are
|
|
|
|
/// written to [`Events<LogEvent>`] by [`transfer_log_events`].
|
|
|
|
#[derive(Deref, DerefMut)]
|
|
|
|
struct CapturedLogEvents(mpsc::Receiver<LogEvent>);
|
|
|
|
|
2024-04-14 15:23:44 +00:00
|
|
|
/// Transfers information from the `LogEvents` resource to [`Events<LogEvent>`](LogEvent).
|
2024-03-04 00:01:05 +00:00
|
|
|
fn transfer_log_events(
|
2024-03-04 07:48:09 +00:00
|
|
|
receiver: NonSend<CapturedLogEvents>,
|
2024-03-04 00:01:05 +00:00
|
|
|
mut log_events: EventWriter<LogEvent>,
|
|
|
|
) {
|
|
|
|
// Make sure to use `try_iter()` and not `iter()` to prevent blocking.
|
2024-03-04 07:48:09 +00:00
|
|
|
log_events.send_batch(receiver.try_iter());
|
2024-03-04 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is the [`Layer`] that we will use to capture log events and then send them to Bevy's
|
|
|
|
/// ECS via it's [`mpsc::Sender`].
|
|
|
|
struct CaptureLayer {
|
|
|
|
sender: mpsc::Sender<LogEvent>,
|
|
|
|
}
|
|
|
|
impl<S: Subscriber> Layer<S> for CaptureLayer {
|
|
|
|
fn on_event(
|
|
|
|
&self,
|
|
|
|
event: &tracing::Event<'_>,
|
|
|
|
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
|
|
|
) {
|
|
|
|
// In order to obtain the log message, we have to create a struct that implements
|
|
|
|
// Visit and holds a reference to our string. Then we use the `record` method and
|
|
|
|
// the struct to modify the reference to hold the message string.
|
|
|
|
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();
|
|
|
|
self.sender
|
|
|
|
.send(LogEvent { message })
|
|
|
|
.expect("LogEvents resource no longer exists!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 07:48:09 +00:00
|
|
|
/// A [`Visit`](tracing::field::Visit)or that records log messages that are transferred to [`CaptureLayer`].
|
2024-03-04 00:01:05 +00:00
|
|
|
struct CaptureLayerVisitor<'a>(&'a mut Option<String>);
|
|
|
|
impl tracing::field::Visit for CaptureLayerVisitor<'_> {
|
|
|
|
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
|
|
|
// This if statement filters out unneeded events sometimes show up
|
|
|
|
if field.name() == "message" {
|
|
|
|
*self.0 = Some(format!("{value:?}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-12 21:16:56 +00:00
|
|
|
fn custom_layer(app: &mut App) -> Option<BoxedLayer> {
|
2024-03-04 07:48:09 +00:00
|
|
|
let (sender, receiver) = mpsc::channel();
|
2024-03-04 00:01:05 +00:00
|
|
|
|
|
|
|
let layer = CaptureLayer { sender };
|
2024-03-04 07:48:09 +00:00
|
|
|
let resource = CapturedLogEvents(receiver);
|
2024-03-04 00:01:05 +00:00
|
|
|
|
|
|
|
app.insert_non_send_resource(resource);
|
|
|
|
app.add_event::<LogEvent>();
|
|
|
|
app.add_systems(Update, transfer_log_events);
|
|
|
|
|
2024-05-12 21:16:56 +00:00
|
|
|
Some(layer.boxed())
|
2024-03-04 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
|
2024-05-12 21:16:56 +00:00
|
|
|
custom_layer,
|
2024-03-04 00:01:05 +00:00
|
|
|
..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
|
|
|
|
// "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");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct LogViewerRoot;
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2024-03-04 00:01:05 +00:00
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
width: Val::Vw(100.0),
|
|
|
|
height: Val::Vh(100.0),
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
LogViewerRoot,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is how we can read our LogEvents.
|
|
|
|
// In this example we are reading the LogEvents and inserting them as text into our log viewer.
|
|
|
|
fn print_logs(
|
|
|
|
mut events: EventReader<LogEvent>,
|
|
|
|
mut commands: Commands,
|
|
|
|
log_viewer_root: Query<Entity, With<LogViewerRoot>>,
|
|
|
|
) {
|
|
|
|
let root_entity = log_viewer_root.single();
|
|
|
|
|
|
|
|
commands.entity(root_entity).with_children(|child| {
|
|
|
|
for event in events.read() {
|
Text rework (#15591)
**Ready for review. Examples migration progress: 100%.**
# Objective
- Implement https://github.com/bevyengine/bevy/discussions/15014
## Solution
This implements [cart's
proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459)
faithfully except for one change. I separated `TextSpan` from
`TextSpan2d` because `TextSpan` needs to require the `GhostNode`
component, which is a `bevy_ui` component only usable by UI.
Extra changes:
- Added `EntityCommands::commands_mut` that returns a mutable reference.
This is a blocker for extension methods that return something other than
`self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable
reference for this reason.
## Testing
- [x] Text examples all work.
---
## Showcase
TODO: showcase-worthy
## Migration Guide
TODO: very breaking
### Accessing text spans by index
Text sections are now text sections on different entities in a
hierarchy, Use the new `TextReader` and `TextWriter` system parameters
to access spans by index.
Before:
```rust
fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) {
let text = query.single_mut();
text.sections[1].value = format_time(time.elapsed());
}
```
After:
```rust
fn refresh_text(
query: Query<Entity, With<TimeText>>,
mut writer: UiTextWriter,
time: Res<Time>
) {
let entity = query.single();
*writer.text(entity, 1) = format_time(time.elapsed());
}
```
### Iterating text spans
Text spans are now entities in a hierarchy, so the new `UiTextReader`
and `UiTextWriter` system parameters provide ways to iterate that
hierarchy. The `UiTextReader::iter` method will give you a normal
iterator over spans, and `UiTextWriter::for_each` lets you visit each of
the spans.
---------
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-09 18:35:36 +00:00
|
|
|
child.spawn(Text::new(&event.message));
|
2024-03-04 00:01:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|