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

Add once and per-level tracing macros that are called only once #2987

Open
mnmaita opened this issue May 29, 2024 · 0 comments
Open

Add once and per-level tracing macros that are called only once #2987

mnmaita opened this issue May 29, 2024 · 0 comments

Comments

@mnmaita
Copy link

mnmaita commented May 29, 2024

Feature Request

Crates

tracing

Motivation

While working on extracting some tracing code in bevy repo, a suggestion arose to include part of it in the tracing crate. The code has to do with per-level tracing macros that should only fire once. These are useful in bevy's context because of the "called-every-frame" nature of game systems, so we are wondering if this kind of functionality would be useful for the wider audience.

Proposal

Add the following macros to tracing crate:

/// 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::tracing::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::tracing::trace!($($arg)+))
    });
}

/// Call [`debug!`](crate::tracing::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::tracing::debug!($($arg)+))
    });
}

/// Call [`info!`](crate::tracing::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::tracing::info!($($arg)+))
    });
}

/// Call [`warn!`](crate::tracing::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::tracing::warn!($($arg)+))
    });
}

/// Call [`error!`](crate::tracing::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::tracing::error!($($arg)+))
    });
}

Alternatives

We can keep these within bevy if there's no interest in having these features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant