Skip to content

Commit

Permalink
macros: fix the tracing-macros crate not compiling (#1000)
Browse files Browse the repository at this point in the history
## Motivation

The `tracing-macros` crate doesn't currently compile with the latest
`tracing` release, because it uses internal private API that changed.
We need to fix this so CI isn't broken.

## Solution

I changed the `dbg!` macro so it no longer uses internal APIs. Ideally,
we would implement `dbg!` using the support for string-literal field
names that was recently added to `tracing`, but I couldn't actually get
it to work --- I think the `event!` macros may not handle string literal
field names properly (we only test them with the `span!` macros >_>).
Will try to fix that later, but for now, this fixes CI and I don't think
anyone actually uses the experimental, unreleased `tracing-macros` 
crate.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Oct 2, 2020
1 parent 386969b commit 5c457e5
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions tracing-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![cfg_attr(docsrs, deny(broken_intra_doc_links))]
#[doc(hidden)]
pub use tracing;

/// Alias of `dbg!` for avoiding conflicts with the `std::dbg!` macro.
#[macro_export]
Expand All @@ -10,10 +12,10 @@ macro_rules! trace_dbg {
$crate::dbg!(target: module_path!(), level: $level, $ex)
};
(target: $target:expr, $ex:expr) => {
$crate::dbg!(target: $target, level: tracing::Level::DEBUG, $ex)
$crate::dbg!(target: $target, level: $crate::tracing::Level::DEBUG, $ex)
};
($ex:expr) => {
$crate::dbg!(level: tracing::Level::DEBUG, $ex)
$crate::dbg!(level: $crate::tracing::Level::DEBUG, $ex)
};
}

Expand All @@ -25,42 +27,20 @@ macro_rules! trace_dbg {
#[macro_export]
macro_rules! dbg {
(target: $target:expr, level: $level:expr, $ex:expr) => {{
use tracing::callsite::Callsite;
use tracing::{
callsite,
field::{debug, Value},
Event, Id, Subscriber,
};
let callsite = tracing::callsite! {
name: concat!("event:trace_dbg(", stringify!($ex), ")"),
kind: tracing::metadata::Kind::EVENT,
target: $target,
level: $level,
fields: value,
};
let val = $ex;
if callsite.is_enabled() {
let meta = callsite.metadata();
let fields = meta.fields();
let key = meta
.fields()
.into_iter()
.next()
.expect("trace_dbg event must have one field");
Event::dispatch(
meta,
&fields.value_set(&[(&key, Some(&debug(&val) as &Value))]),
);
match $ex {
value => {
$crate::tracing::event!(target: $target, $level, ?value, stringify!($ex));
value
}
}
val
}};
(level: $level:expr, $ex:expr) => {
$crate::dbg!(target: module_path!(), level: $level, $ex)
};
(target: $target:expr, $ex:expr) => {
$crate::dbg!(target: $target, level: tracing::Level::DEBUG, $ex)
$crate::dbg!(target: $target, level: $crate::tracing::Level::DEBUG, $ex)
};
($ex:expr) => {
$crate::dbg!(level: tracing::Level::DEBUG, $ex)
$crate::dbg!(level: $crate::tracing::Level::DEBUG, $ex)
};
}

0 comments on commit 5c457e5

Please sign in to comment.