bevy/examples/app/log_layers_ecs.rs

152 lines
5.3 KiB
Rust
Raw Normal View History

//! 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).
//!
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
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
Fix some doc warnings (#12961) # Objective - Fix some doc warnings - Add doc-scrape-examples to all examples Moved from #12692 I run `cargo +nightly doc --workspace --all-features --no-deps -Zunstable-options -Zrustdoc-scrape-examples` <details> ``` warning: public documentation for `GzAssetLoaderError` links to private item `GzAssetLoader` --> examples/asset/asset_decompression.rs:24:47 | 24 | /// Possible errors that can be produced by [`GzAssetLoader`] | ^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: `bevy` (example "asset_decompression") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d.rs:3:15 | 3 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d") generated 1 warning warning: unresolved link to `WorldQuery` --> examples/ecs/custom_query_param.rs:1:49 | 1 | //! This example illustrates the usage of the [`WorldQuery`] derive macro, which allows | ^^^^^^^^^^ no item named `WorldQuery` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "custom_query_param") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d_vertex_color_texture.rs:4:15 | 4 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d_vertex_color_texture") generated 1 warning warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:48:9 | 48 | /// * [`CoolText`]: a custom RON text format that supports dependencies and embedded dependencies | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `TextPlugin` links to private item `Text` --> examples/asset/processing/asset_processing.rs:49:9 | 49 | /// * [`Text`]: a "normal" plain text file | ^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:51:57 | 51 | /// It also defines an asset processor that will load [`CoolText`], resolve embedded dependenc... | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: `bevy` (example "asset_processing") generated 3 warnings warning: public documentation for `CustomAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:20:47 | 20 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `BlobAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:61:47 | 61 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` ``` warning: `bevy` (example "mesh2d") generated 1 warning warning: public documentation for `log_layers_ecs` links to private item `update_subscriber` --> examples/app/log_layers_ecs.rs:6:18 | 6 | //! Inside the [`update_subscriber`] function we will create a [`mpsc::Sender`] and a [`mpsc::R... | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: unresolved link to `AdvancedLayer` --> examples/app/log_layers_ecs.rs:7:72 | 7 | ... will go into the [`AdvancedLayer`] and the [`Receiver`](mpsc::Receiver) will | ^^^^^^^^^^^^^ no item named `AdvancedLayer` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:8:42 | 8 | //! go into a non-send resource called [`LogEvents`] (It has to be non-send because [`Receiver`... | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `transfer_log_events` --> examples/app/log_layers_ecs.rs:9:30 | 9 | //! From there we will use [`transfer_log_events`] to transfer log events from [`LogEvents`] to... | ^^^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:9:82 | 9 | ...nsfer log events from [`LogEvents`] to an ECS event called [`LogEvent`]. | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:9:119 | 9 | ...nts`] to an ECS event called [`LogEvent`]. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:11:49 | 11 | //! Finally, after all that we can access the [`LogEvent`] event from our systems and use it. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` <details/>
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`.
//!
Fix some doc warnings (#12961) # Objective - Fix some doc warnings - Add doc-scrape-examples to all examples Moved from #12692 I run `cargo +nightly doc --workspace --all-features --no-deps -Zunstable-options -Zrustdoc-scrape-examples` <details> ``` warning: public documentation for `GzAssetLoaderError` links to private item `GzAssetLoader` --> examples/asset/asset_decompression.rs:24:47 | 24 | /// Possible errors that can be produced by [`GzAssetLoader`] | ^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: `bevy` (example "asset_decompression") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d.rs:3:15 | 3 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d") generated 1 warning warning: unresolved link to `WorldQuery` --> examples/ecs/custom_query_param.rs:1:49 | 1 | //! This example illustrates the usage of the [`WorldQuery`] derive macro, which allows | ^^^^^^^^^^ no item named `WorldQuery` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "custom_query_param") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d_vertex_color_texture.rs:4:15 | 4 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d_vertex_color_texture") generated 1 warning warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:48:9 | 48 | /// * [`CoolText`]: a custom RON text format that supports dependencies and embedded dependencies | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `TextPlugin` links to private item `Text` --> examples/asset/processing/asset_processing.rs:49:9 | 49 | /// * [`Text`]: a "normal" plain text file | ^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:51:57 | 51 | /// It also defines an asset processor that will load [`CoolText`], resolve embedded dependenc... | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: `bevy` (example "asset_processing") generated 3 warnings warning: public documentation for `CustomAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:20:47 | 20 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `BlobAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:61:47 | 61 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` ``` warning: `bevy` (example "mesh2d") generated 1 warning warning: public documentation for `log_layers_ecs` links to private item `update_subscriber` --> examples/app/log_layers_ecs.rs:6:18 | 6 | //! Inside the [`update_subscriber`] function we will create a [`mpsc::Sender`] and a [`mpsc::R... | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: unresolved link to `AdvancedLayer` --> examples/app/log_layers_ecs.rs:7:72 | 7 | ... will go into the [`AdvancedLayer`] and the [`Receiver`](mpsc::Receiver) will | ^^^^^^^^^^^^^ no item named `AdvancedLayer` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:8:42 | 8 | //! go into a non-send resource called [`LogEvents`] (It has to be non-send because [`Receiver`... | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `transfer_log_events` --> examples/app/log_layers_ecs.rs:9:30 | 9 | //! From there we will use [`transfer_log_events`] to transfer log events from [`LogEvents`] to... | ^^^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:9:82 | 9 | ...nsfer log events from [`LogEvents`] to an ECS event called [`LogEvent`]. | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:9:119 | 9 | ...nts`] to an ECS event called [`LogEvent`]. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:11:49 | 11 | //! Finally, after all that we can access the [`LogEvent`] event from our systems and use it. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` <details/>
2024-04-14 15:23:44 +00:00
//! Finally, after all that we can access the `LogEvent` event from our systems and use it.
//! In this example we build a simple log viewer.
use std::sync::mpsc;
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
2024-05-12 21:16:56 +00:00
use bevy::log::BoxedLayer;
use bevy::{
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
2024-05-12 21:16:56 +00:00
log::tracing_subscriber::{self, Layer},
prelude::*,
utils::tracing,
utils::tracing::Subscriber,
};
/// 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>);
Fix some doc warnings (#12961) # Objective - Fix some doc warnings - Add doc-scrape-examples to all examples Moved from #12692 I run `cargo +nightly doc --workspace --all-features --no-deps -Zunstable-options -Zrustdoc-scrape-examples` <details> ``` warning: public documentation for `GzAssetLoaderError` links to private item `GzAssetLoader` --> examples/asset/asset_decompression.rs:24:47 | 24 | /// Possible errors that can be produced by [`GzAssetLoader`] | ^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: `bevy` (example "asset_decompression") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d.rs:3:15 | 3 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d") generated 1 warning warning: unresolved link to `WorldQuery` --> examples/ecs/custom_query_param.rs:1:49 | 1 | //! This example illustrates the usage of the [`WorldQuery`] derive macro, which allows | ^^^^^^^^^^ no item named `WorldQuery` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "custom_query_param") generated 1 warning warning: unresolved link to `shape::Quad` --> examples/2d/mesh2d_vertex_color_texture.rs:4:15 | 4 | //! [`Quad`]: shape::Quad | ^^^^^^^^^^^ no item named `shape` in scope | = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: `bevy` (example "mesh2d_vertex_color_texture") generated 1 warning warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:48:9 | 48 | /// * [`CoolText`]: a custom RON text format that supports dependencies and embedded dependencies | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `TextPlugin` links to private item `Text` --> examples/asset/processing/asset_processing.rs:49:9 | 49 | /// * [`Text`]: a "normal" plain text file | ^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `TextPlugin` links to private item `CoolText` --> examples/asset/processing/asset_processing.rs:51:57 | 51 | /// It also defines an asset processor that will load [`CoolText`], resolve embedded dependenc... | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: `bevy` (example "asset_processing") generated 3 warnings warning: public documentation for `CustomAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:20:47 | 20 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: public documentation for `BlobAssetLoaderError` links to private item `CustomAssetLoader` --> examples/asset/custom_asset.rs:61:47 | 61 | /// Possible errors that can be produced by [`CustomAssetLoader`] | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` ``` warning: `bevy` (example "mesh2d") generated 1 warning warning: public documentation for `log_layers_ecs` links to private item `update_subscriber` --> examples/app/log_layers_ecs.rs:6:18 | 6 | //! Inside the [`update_subscriber`] function we will create a [`mpsc::Sender`] and a [`mpsc::R... | ^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default warning: unresolved link to `AdvancedLayer` --> examples/app/log_layers_ecs.rs:7:72 | 7 | ... will go into the [`AdvancedLayer`] and the [`Receiver`](mpsc::Receiver) will | ^^^^^^^^^^^^^ no item named `AdvancedLayer` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:8:42 | 8 | //! go into a non-send resource called [`LogEvents`] (It has to be non-send because [`Receiver`... | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `transfer_log_events` --> examples/app/log_layers_ecs.rs:9:30 | 9 | //! From there we will use [`transfer_log_events`] to transfer log events from [`LogEvents`] to... | ^^^^^^^^^^^^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: unresolved link to `LogEvents` --> examples/app/log_layers_ecs.rs:9:82 | 9 | ...nsfer log events from [`LogEvents`] to an ECS event called [`LogEvent`]. | ^^^^^^^^^ no item named `LogEvents` in scope | = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:9:119 | 9 | ...nts`] to an ECS event called [`LogEvent`]. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` warning: public documentation for `log_layers_ecs` links to private item `LogEvent` --> examples/app/log_layers_ecs.rs:11:49 | 11 | //! Finally, after all that we can access the [`LogEvent`] event from our systems and use it. | ^^^^^^^^ this item is private | = note: this link will resolve properly if you pass `--document-private-items` ``` <details/>
2024-04-14 15:23:44 +00:00
/// Transfers information from the `LogEvents` resource to [`Events<LogEvent>`](LogEvent).
fn transfer_log_events(
receiver: NonSend<CapturedLogEvents>,
mut log_events: EventWriter<LogEvent>,
) {
// Make sure to use `try_iter()` and not `iter()` to prevent blocking.
log_events.send_batch(receiver.try_iter());
}
/// 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!");
}
}
}
/// A [`Visit`](tracing::field::Visit)or that records log messages that are transferred to [`CaptureLayer`].
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:?}"));
}
}
}
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
2024-05-12 21:16:56 +00:00
fn custom_layer(app: &mut App) -> Option<BoxedLayer> {
let (sender, receiver) = mpsc::channel();
let layer = CaptureLayer { sender };
let resource = CapturedLogEvents(receiver);
app.insert_non_send_resource(resource);
app.add_event::<LogEvent>();
app.add_systems(Update, transfer_log_events);
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
2024-05-12 21:16:56 +00:00
Some(layer.boxed())
}
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
Improve tracing layer customization (#13159) # Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
2024-05-12 21:16:56 +00:00
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
// "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) {
commands.spawn(Camera2dBundle::default());
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() {
child.spawn(TextBundle::from_section(
&event.message,
TextStyle::default(),
));
}
});
}