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 ValueSet construction out of closures #987

Merged
merged 5 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 3 additions & 24 deletions tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,9 @@ pub mod __macro_support {
}
}

pub fn dispatch_event(&'static self, interest: Interest, f: impl FnOnce(&crate::Dispatch)) {
tracing_core::dispatcher::get_current(|current| {
if interest.is_always() || current.enabled(self.meta) {
f(current)
}
});
pub fn is_enabled(&self, interest: Interest) -> bool {
interest.is_always()
|| crate::dispatcher::get_default(|default| default.enabled(self.meta))
}

#[inline]
Expand All @@ -1019,24 +1016,6 @@ pub mod __macro_support {
pub fn disabled_span(&self) -> crate::Span {
crate::Span::none()
}

pub fn dispatch_span(
&'static self,
interest: Interest,
f: impl FnOnce(&crate::Dispatch) -> crate::Span,
) -> crate::Span {
if interest.is_never() {
return self.disabled_span();
}

tracing_core::dispatcher::get_current(|current| {
if interest.is_always() || current.enabled(self.meta) {
return f(current);
}
self.disabled_span()
})
.unwrap_or_else(|| self.disabled_span())
}
}

impl Callsite for MacroCallsite {
Expand Down
66 changes: 36 additions & 30 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ macro_rules! span {
fields: $($fields)*
};
let mut interest = $crate::subscriber::Interest::never();
if $crate::level_enabled!($lvl) && { interest = CALLSITE.interest(); !interest.is_never() }{
CALLSITE.dispatch_span(interest, |current| {
let meta = CALLSITE.metadata();
// span with parent
$crate::Span::child_of_with(
$parent,
meta,
&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*),
current,
)
})
if $crate::level_enabled!($lvl)
&& { interest = CALLSITE.interest(); !interest.is_never() }
&& CALLSITE.is_enabled(interest)
{
let meta = CALLSITE.metadata();
// span with explicit parent
$crate::Span::child_of(
$parent,
meta,
&$crate::valueset!(meta.fields(), $($fields)*),
)
} else {
let span = CALLSITE.disabled_span();
$crate::if_log_enabled! {{
Expand All @@ -63,15 +63,16 @@ macro_rules! span {
fields: $($fields)*
};
let mut interest = $crate::subscriber::Interest::never();
if $crate::level_enabled!($lvl) && { interest = CALLSITE.interest(); !interest.is_never() }{
CALLSITE.dispatch_span(interest, |current| {
let meta = CALLSITE.metadata();
$crate::Span::new_with(
meta,
&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*),
current,
)
})
if $crate::level_enabled!($lvl)
&& { interest = CALLSITE.interest(); !interest.is_never() }
&& CALLSITE.is_enabled(interest)
{
let meta = CALLSITE.metadata();
// span with contextual parent
$crate::Span::new(
meta,
&$crate::valueset!(meta.fields(), $($fields)*),
)
} else {
let span = CALLSITE.disabled_span();
$crate::if_log_enabled! {{
Expand Down Expand Up @@ -605,11 +606,14 @@ macro_rules! event {
fields: $($fields)*
};
let interest = CALLSITE.interest();
if !interest.is_never() {
CALLSITE.dispatch_event(interest, |current| {
let meta = CALLSITE.metadata();
current.event(&$crate::Event::new_child_of($parent, meta, &$crate::valueset!(meta.fields(), $($fields)*)))
});
if !interest.is_never() && CALLSITE.is_enabled(interest) {
let meta = CALLSITE.metadata();
// event with explicit parent
$crate::Event::child_of(
$parent,
meta,
&$crate::valueset!(meta.fields(), $($fields)*)
);
}
}
);
Expand Down Expand Up @@ -649,11 +653,13 @@ macro_rules! event {
fields: $($fields)*
};
let interest = CALLSITE.interest();
if !interest.is_never() {
CALLSITE.dispatch_event(interest, |current| {
let meta = CALLSITE.metadata();
current.event(&$crate::Event::new(meta, &$crate::valueset!(meta.fields(), $($fields)*)));
});
if !interest.is_never() && CALLSITE.is_enabled(interest) {
let meta = CALLSITE.metadata();
// event with contextual parent
$crate::Event::dispatch(
meta,
&$crate::valueset!(meta.fields(), $($fields)*)
);
}
}
});
Expand Down
28 changes: 28 additions & 0 deletions tracing/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,34 @@ fn field_shorthand_only() {
event!(Level::TRACE, ?pos.x, ?pos.y);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn borrow_val_events() {
// Reproduces https://github.com/tokio-rs/tracing/issues/954
let mut foo = (String::new(), String::new());
let zero = &mut foo.0;
trace!(one = ?foo.1);
debug!(one = ?foo.1);
info!(one = ?foo.1);
warn!(one = ?foo.1);
error!(one = ?foo.1);
zero.push_str("hello world");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn borrow_val_spans() {
// Reproduces https://github.com/tokio-rs/tracing/issues/954
let mut foo = (String::new(), String::new());
let zero = &mut foo.0;
let _span = trace_span!("span", one = ?foo.1);
let _span = debug_span!("span", one = ?foo.1);
let _span = info_span!("span", one = ?foo.1);
let _span = warn_span!("span", one = ?foo.1);
let _span = error_span!("span", one = ?foo.1);
zero.push_str("hello world");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn callsite_macro_api() {
Expand Down