bevy_utils/
once.rs

1/// Call some expression only once per call site.
2#[macro_export]
3macro_rules! once {
4    ($expression:expr) => {{
5        use ::core::sync::atomic::{AtomicBool, Ordering};
6
7        static SHOULD_FIRE: AtomicBool = AtomicBool::new(true);
8        if SHOULD_FIRE.swap(false, Ordering::Relaxed) {
9            $expression;
10        }
11    }};
12}
13
14/// Call [`trace!`](crate::tracing::trace) once per call site.
15///
16/// Useful for logging within systems which are called every frame.
17#[macro_export]
18macro_rules! trace_once {
19    ($($arg:tt)+) => ({
20        $crate::once!($crate::tracing::trace!($($arg)+))
21    });
22}
23
24/// Call [`debug!`](crate::tracing::debug) once per call site.
25///
26/// Useful for logging within systems which are called every frame.
27#[macro_export]
28macro_rules! debug_once {
29    ($($arg:tt)+) => ({
30        $crate::once!($crate::tracing::debug!($($arg)+))
31    });
32}
33
34/// Call [`info!`](crate::tracing::info) once per call site.
35///
36/// Useful for logging within systems which are called every frame.
37#[macro_export]
38macro_rules! info_once {
39    ($($arg:tt)+) => ({
40        $crate::once!($crate::tracing::info!($($arg)+))
41    });
42}
43
44/// Call [`warn!`](crate::tracing::warn) once per call site.
45///
46/// Useful for logging within systems which are called every frame.
47#[macro_export]
48macro_rules! warn_once {
49    ($($arg:tt)+) => ({
50        $crate::once!($crate::tracing::warn!($($arg)+))
51    });
52}
53
54/// Call [`error!`](crate::tracing::error) once per call site.
55///
56/// Useful for logging within systems which are called every frame.
57#[macro_export]
58macro_rules! error_once {
59    ($($arg:tt)+) => ({
60        $crate::once!($crate::tracing::error!($($arg)+))
61    });
62}