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

tracing: move macro callsite impls out of macro expansion #869

Merged
merged 8 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 100 additions & 9 deletions tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,107 @@ pub mod subscriber;

#[doc(hidden)]
pub mod __macro_support {
pub use crate::stdlib::sync::atomic::{AtomicUsize, Ordering};
pub type Once = tracing_core::Once;
}
pub use crate::callsite::Callsite as _;
use crate::stdlib::sync::atomic::{AtomicUsize, Ordering};
use crate::{subscriber::Interest, Callsite, Metadata};
use tracing_core::Once;

#[doc(hidden)]
// resolves https://github.com/tokio-rs/tracing/issues/783 by forcing a monomorphization
// in tracing, not downstream crates.
#[inline]
pub fn is_enabled(meta: &crate::Metadata<'_>) -> bool {
crate::dispatcher::get_default(|current| current.enabled(meta))
/// Callsite implementation used by macro-generated code.
///
/// /!\ WARNING: This is *not* a stable API! /!\
/// This type, and all code contained in the `__macro_support` module, is
/// a *private* API of `tracing`. It is exposed publicly because it is used
/// by the `tracing` macros, but it is not part of the stable versioned API.
/// Breaking changes to this module may occur in small-numbered versions
/// warnings.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug)]
pub struct MacroCallsite {
interest: AtomicUsize,
meta: &'static Metadata<'static>,
registration: Once,
}

impl MacroCallsite {
/// Returns a new `MacroCallsite` with the specified `Metadata`.
///
/// /!\ WARNING: This is *not* a stable API! /!\
/// This method, and all code contained in the `__macro_support` module, is
/// a *private* API of `tracing`. It is exposed publicly because it is used
/// by the `tracing` macros, but it is not part of the stable versioned API.
/// Breaking changes to this module may occur in small-numbered versions
/// warnings.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
pub const fn new(meta: &'static Metadata<'static>) -> Self {
Self {
interest: AtomicUsize::new(0),
meta,
registration: Once::new(),
}
}

/// Returns `true` if the callsite is enabled by a cached interest, or
/// by the current `Dispatch`'s `enabled` method if the cached
/// `Interest` is `sometimes`.
///
/// /!\ WARNING: This is *not* a stable API! /!\
/// This method, and all code contained in the `__macro_support` module, is
/// a *private* API of `tracing`. It is exposed publicly because it is used
/// by the `tracing` macros, but it is not part of the stable versioned API.
/// Breaking changes to this module may occur in small-numbered versions
/// warnings.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
#[inline(always)]
pub fn is_enabled(&self) -> bool {
let interest = self.interest();
if interest.is_always() {
return true;
}
if interest.is_never() {
return false;
}

crate::dispatcher::get_default(|current| current.enabled(self.meta))
}

/// Registers this callsite with the global callsite registry.
///
/// If the callsite is already registered, this does nothing.
///
/// /!\ WARNING: This is *not* a stable API! /!\
/// This method, and all code contained in the `__macro_support` module, is
/// a *private* API of `tracing`. It is exposed publicly because it is used
/// by the `tracing` macros, but it is not part of the stable versioned API.
/// Breaking changes to this module may occur in small-numbered versions
/// warnings.
hawkw marked this conversation as resolved.
Show resolved Hide resolved
#[inline(always)]
pub fn register(&'static self) {
self.registration
.call_once(|| crate::callsite::register(self));
}

#[inline(always)]
fn interest(&self) -> Interest {
match self.interest.load(Ordering::Relaxed) {
0 => Interest::never(),
2 => Interest::always(),
_ => Interest::sometimes(),
}
}
}

impl Callsite for MacroCallsite {
fn set_interest(&self, interest: Interest) {
let interest = match () {
_ if interest.is_never() => 0,
_ if interest.is_always() => 2,
_ => 1,
};
self.interest.store(interest, Ordering::SeqCst);
}

#[inline(always)]
fn metadata(&self) -> &Metadata<'static> {
&self.meta
}
}
}

mod sealed {
Expand Down
Loading