-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper macro's for logging only once (#10808)
# Objective Fixes #10291 This adds a way to easily log messages once within system which are called every frame. ## Solution Opted for a macro-based approach. The fact that the 'once' call is tracked per call site makes the `log_once!()` macro very versatile and easy-to-use. I suspect it will be very handy for all of us, but especially beginners, to get some initial feedback from systems without spamming up the place! I've made the macro's return its internal `has_fired` state, for situations in which that might be useful to know (trigger something else alongside the log, for example). Please let me know if I placed the macro's in the right location, and if you would like me to do something more clever with the macro's themselves, since its looking quite copy-pastey at the moment. I've tried ways to replace 5 with 1 macro's, but no success yet. One downside of this approach is: Say you wish to warn the user if a resource is invalid. In this situation, the `resource.is_valid()` check would still be performed every frame: ```rust fn my_system(my_res: Res<MyResource>) { if !my_res.is_valid() { warn_once!("resource is invalid!"); } } ``` If you want to prevent that, you would still need to introduce a local boolean. I don't think this is a very big deal, as expensive checks shouldn't be called every frame in any case. ## Changelog Added: `trace_once!()`, `debug_once!()`, `info_once!()`, `warn_once!()`, and `error_once!()` log macros which fire only once per call site.
- Loading branch information
1 parent
2653adf
commit 18ac125
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// Call some expression only once per call site. | ||
#[macro_export] | ||
macro_rules! once { | ||
($expression:expr) => {{ | ||
use ::std::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
static SHOULD_FIRE: AtomicBool = AtomicBool::new(true); | ||
if SHOULD_FIRE.swap(false, Ordering::Relaxed) { | ||
$expression; | ||
} | ||
}}; | ||
} | ||
|
||
/// Call [`trace!`](crate::trace) once per call site. | ||
/// | ||
/// Useful for logging within systems which are called every frame. | ||
#[macro_export] | ||
macro_rules! trace_once { | ||
($($arg:tt)+) => ({ | ||
$crate::once!($crate::trace!($($arg)+)) | ||
}); | ||
} | ||
|
||
/// Call [`debug!`](crate::debug) once per call site. | ||
/// | ||
/// Useful for logging within systems which are called every frame. | ||
#[macro_export] | ||
macro_rules! debug_once { | ||
($($arg:tt)+) => ({ | ||
$crate::once!($crate::debug!($($arg)+)) | ||
}); | ||
} | ||
|
||
/// Call [`info!`](crate::info) once per call site. | ||
/// | ||
/// Useful for logging within systems which are called every frame. | ||
#[macro_export] | ||
macro_rules! info_once { | ||
($($arg:tt)+) => ({ | ||
$crate::once!($crate::info!($($arg)+)) | ||
}); | ||
} | ||
|
||
/// Call [`warn!`](crate::warn) once per call site. | ||
/// | ||
/// Useful for logging within systems which are called every frame. | ||
#[macro_export] | ||
macro_rules! warn_once { | ||
($($arg:tt)+) => ({ | ||
$crate::once!($crate::warn!($($arg)+)) | ||
}); | ||
} | ||
|
||
/// Call [`error!`](crate::error) once per call site. | ||
/// | ||
/// Useful for logging within systems which are called every frame. | ||
#[macro_export] | ||
macro_rules! error_once { | ||
($($arg:tt)+) => ({ | ||
$crate::once!($crate::error!($($arg)+)) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters