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
51 changes: 50 additions & 1 deletion crates/bevy_ecs/src/system/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ where
/// A collection of common adapters for [piping](super::PipeSystem) the result of a system.
pub mod adapter {
use crate::system::In;
use std::fmt::Debug;
use std::fmt::{Debug, Display};
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.
Edwox marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```
///
/// ```
pub fn info<T: Display>(In(data): In<T>) {
tracing::info!("{}", data);
}
Edwox marked this conversation as resolved.
Show resolved Hide resolved

/// System adapter that utilizes the debug! macro to debug system piping.
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.
Edwox marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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 which useful for fallible systems that should print the error message in the case of an error.
Edwox marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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
26 changes: 25 additions & 1 deletion examples/ecs/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
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(Message("42A".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))
.run();
}

Expand All @@ -28,3 +40,15 @@ fn handler_system(In(result): In<Result<usize>>) {
Err(err) => println!("encountered an error: {err:?}"),
}
}

fn data_pipe_system(message: Res<Message>) -> String {
message.0.clone()
}

fn warning_pipe_system(message: Res<Message>) -> Option<String> {

match message.0.as_str() {
Edwox marked this conversation as resolved.
Show resolved Hide resolved
"No warning" => None,
x => Some(x.to_string()),
}
}