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

[Merged by Bors] - [Fixes #6224] Add logging variants of system piping #6751

Closed
49 changes: 49 additions & 0 deletions crates/bevy_ecs/src/system/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ where
pub mod adapter {
use crate::system::In;
use std::fmt::Debug;
use bevy_utils::tracing;

/// Converts a regular function into a system adapter.
///
Expand Down Expand Up @@ -232,6 +233,54 @@ pub mod adapter {
res.unwrap()
}

/// System adapter that utilizes the [`info!`] macro to print system information.
///
/// # Examples
///
/// ```
///
/// ```
pub fn info<T: Debug>(In(data): In<T>) {
tracing::info!("{:?}", data);
}

/// System adapter that utilizes the [`warn!`] macro to print the output of a system.
Edwox marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```
///
/// ```
pub fn dbg<T: Debug>(In(data): In<T>) {
tracing::debug!("{:?}", data);
}

/// System adapter that utilizes the [`warn!`] macro to print the output of a system.
///
/// # Examples
///
/// ```
///
/// ```
pub fn warn<T: Debug>(In(res): In<Option<T>>) {
Edwox marked this conversation as resolved.
Show resolved Hide resolved
if let Some(warn) = res {
tracing::warn!("{:?}", warn);
}
}

/// System adapter that utilizes the [`error!`] macro to print the output of a system.
///
/// # Examples
///
/// ```
///
/// ```
pub fn error<T, E: Debug>(In(res): In<Result<T, E>>) {
Edwox marked this conversation as resolved.
Show resolved Hide resolved
if let Err(error) = res {
tracing::error!("{:?}", error);
}
}
Edwox marked this conversation as resolved.
Show resolved Hide resolved

/// System adapter that ignores the output of the previous system in a pipe.
/// This is useful for fallible systems that should simply return early in case of an `Err`/`None`.
///
Expand Down
30 changes: 29 additions & 1 deletion examples/ecs/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
use anyhow::Result;
use bevy::prelude::*;

use bevy::utils::tracing::Level;
use bevy::log::LogPlugin;

fn main() {
App::new()
.insert_resource(Message("42".to_string()))
.insert_resource(OptionalWarning(Some("Got to rusty?".to_string())))
.add_plugin(LogPlugin {
level: Level::TRACE,
filter: "".to_string(),
})
.add_system(parse_message_system.pipe(handler_system))
.add_system(data_pipe_system.pipe(system_adapter::info))
Edwox marked this conversation as resolved.
Show resolved Hide resolved
.add_system(parse_message_system.pipe(system_adapter::dbg))
.add_system(warning_pipe_system.pipe(system_adapter::warn))
.add_system(parse_message_system.pipe(system_adapter::error))
.add_system(parse_message_system.pipe(system_adapter::ignore))
.run();
}

#[derive(Resource, Deref)]
struct Message(String);

// this system produces a Result<usize> output by trying to parse the Message resource
#[derive(Resource, Deref)]
struct OptionalWarning(Option<String>);

// This system produces a Result<usize> output by trying to parse the Message resource.
fn parse_message_system(message: Res<Message>) -> Result<usize> {
Ok(message.parse::<usize>()?)
}
Expand All @@ -28,3 +44,15 @@ fn handler_system(In(result): In<Result<usize>>) {
Err(err) => println!("encountered an error: {err:?}"),
}
}

// This system produces a String output by trying to clone the String from the Message resource.
fn data_pipe_system(message: Res<Message>) -> String {
message.0.clone()
}

// This system produces an Option<String> output by trying to extract Some(String) from the
// OptionalWarning resource. Try changing the OptionalWarning resource to None. You should
// not see the warning message printed.
fn warning_pipe_system(message: Res<OptionalWarning>) -> Option<String> {
Some(message.0.to_owned()?)
}