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-attributes: support const values for target and name (backport v0.1.x) #2942

Open
wants to merge 3 commits into
base: v0.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/examples/sloggish/sloggish_subscriber.rs
Expand Up @@ -38,7 +38,7 @@ pub struct CurrentSpanPerThread {
impl CurrentSpanPerThread {
pub fn new() -> Self {
thread_local! {
static CURRENT: RefCell<Vec<Id>> = RefCell::new(vec![]);
static CURRENT: RefCell<Vec<Id>> = const { RefCell::new(vec![]) };
};
Self { current: &CURRENT }
}
Expand Down
1 change: 1 addition & 0 deletions tracing-attributes/tests/instrument.rs
Expand Up @@ -100,6 +100,7 @@ fn fields() {

#[test]
fn skip() {
#[allow(dead_code)]
struct UnDebug(pub u32);

#[instrument(target = "my_target", level = "debug", skip(_arg2, _arg3))]
Expand Down
1 change: 1 addition & 0 deletions tracing-core/src/dispatcher.rs
Expand Up @@ -455,6 +455,7 @@ fn get_global() -> &'static Dispatch {
unsafe {
// This is safe given the invariant that setting the global dispatcher
// also sets `GLOBAL_INIT` to `INITIALIZED`.
#[allow(static_mut_refs)]
&GLOBAL_DISPATCH
}
}
Expand Down
2 changes: 2 additions & 0 deletions tracing-core/src/field.rs
Expand Up @@ -1090,6 +1090,7 @@ mod test {
use crate::stdlib::{borrow::ToOwned, string::String};

// Make sure TEST_CALLSITE_* have non-zero size, so they can't be located at the same address.
#[allow(dead_code)]
struct TestCallsite1(u8);
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1(0);
static TEST_META_1: Metadata<'static> = metadata! {
Expand All @@ -1111,6 +1112,7 @@ mod test {
}
}

#[allow(dead_code)]
struct TestCallsite2(u8);
static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2(0);
static TEST_META_2: Metadata<'static> = metadata! {
Expand Down
1 change: 1 addition & 0 deletions tracing-futures/tests/std_future.rs
Expand Up @@ -54,6 +54,7 @@ fn span_on_drop() {
}
}

#[allow(dead_code)]
struct Fut(Option<AssertSpanOnDrop>);

impl Future for Fut {
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/fmt_layer.rs
Expand Up @@ -943,7 +943,7 @@ where

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
thread_local! {
static BUF: RefCell<String> = RefCell::new(String::new());
static BUF: RefCell<String> = const { RefCell::new(String::new()) };
}

BUF.with(|buf| {
Expand Down
3 changes: 2 additions & 1 deletion tracing-subscriber/src/registry/sharded.rs
Expand Up @@ -214,7 +214,7 @@ thread_local! {
/// track how many layers have processed the close.
/// For additional details, see [`CloseGuard`].
///
static CLOSE_COUNT: Cell<usize> = Cell::new(0);
static CLOSE_COUNT: Cell<usize> = const { Cell::new(0) };
}

impl Subscriber for Registry {
Expand Down Expand Up @@ -596,6 +596,7 @@ mod tests {
closed: Vec<(&'static str, Weak<()>)>,
}

#[allow(dead_code)]
struct SetRemoved(Arc<()>);

impl<S> Layer<S> for CloseLayer
Expand Down
5 changes: 0 additions & 5 deletions tracing/src/macros.rs
Expand Up @@ -1547,7 +1547,6 @@ macro_rules! trace {
$crate::event!(
target: module_path!(),
$crate::Level::TRACE,
{},
$($arg)+
)
);
Expand Down Expand Up @@ -1824,7 +1823,6 @@ macro_rules! debug {
$crate::event!(
target: module_path!(),
$crate::Level::DEBUG,
{},
$($arg)+
)
);
Expand Down Expand Up @@ -2112,7 +2110,6 @@ macro_rules! info {
$crate::event!(
target: module_path!(),
$crate::Level::INFO,
{},
$($arg)+
)
);
Expand Down Expand Up @@ -2393,7 +2390,6 @@ macro_rules! warn {
$crate::event!(
target: module_path!(),
$crate::Level::WARN,
{},
$($arg)+
)
);
Expand Down Expand Up @@ -2670,7 +2666,6 @@ macro_rules! error {
$crate::event!(
target: module_path!(),
$crate::Level::ERROR,
{},
$($arg)+
)
);
Expand Down
54 changes: 54 additions & 0 deletions tracing/tests/event.rs
Expand Up @@ -525,6 +525,12 @@ fn constant_field_name() {
)
};
let (subscriber, handle) = subscriber::mock()
.event(expect_event())
.event(expect_event())
.event(expect_event())
.event(expect_event())
.event(expect_event())
.event(expect_event())
.event(expect_event())
.event(expect_event())
.only()
Expand All @@ -548,6 +554,54 @@ fn constant_field_name() {
},
"quux"
);
tracing::info!(
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
"quux"
);
tracing::info!(
{
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
},
"quux"
);
tracing::event!(
Level::INFO,
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
"{}",
"quux"
);
tracing::event!(
Level::INFO,
{
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
},
"{}",
"quux"
);
tracing::info!(
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
"{}",
"quux"
);
tracing::info!(
{
{ std::convert::identity(FOO) } = "bar",
{ "constant string" } = "also works",
foo.bar = "baz",
},
"{}",
"quux"
);
});

handle.assert_finished();
Expand Down
1 change: 1 addition & 0 deletions tracing/tests/instrument.rs
Expand Up @@ -21,6 +21,7 @@ fn span_on_drop() {
}
}

#[allow(dead_code)]
struct Fut(Option<AssertSpanOnDrop>);

impl Future for Fut {
Expand Down
32 changes: 31 additions & 1 deletion tracing/tests/macros.rs
Expand Up @@ -545,12 +545,15 @@ fn trace() {
trace!(foo = ?3, bar.baz = %2, quux = false);
trace!(foo = 3, bar.baz = 2, quux = false);
trace!(foo = 3, bar.baz = 3,);
trace!("foo" = 3, bar.baz = 3,);
trace!(foo = 3, "bar.baz" = 3,);
trace!("foo");
trace!("foo: {}", 3);
trace!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
trace!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
trace!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
trace!({ foo = 3, bar.baz = 80 }, "quux");
trace!({ "foo" = 3, "bar.baz" = 80 }, "quux");
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
trace!({ foo = 2, bar.baz = 78 }, "quux");
Expand All @@ -569,6 +572,9 @@ fn trace() {
trace!(?foo);
trace!(%foo);
trace!(foo);
trace!("foo" = ?foo);
trace!("foo" = %foo);
trace!("foo" = foo);
trace!(name: "foo", ?foo);
trace!(name: "foo", %foo);
trace!(name: "foo", foo);
Expand All @@ -589,12 +595,15 @@ fn debug() {
debug!(foo = ?3, bar.baz = %2, quux = false);
debug!(foo = 3, bar.baz = 2, quux = false);
debug!(foo = 3, bar.baz = 3,);
debug!("foo" = 3, bar.baz = 3,);
debug!(foo = 3, "bar.baz" = 3,);
debug!("foo");
debug!("foo: {}", 3);
debug!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
debug!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
debug!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
debug!({ foo = 3, bar.baz = 80 }, "quux");
debug!({ "foo" = 3, "bar.baz" = 80 }, "quux");
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
debug!({ foo = 2, bar.baz = 78 }, "quux");
Expand All @@ -613,6 +622,9 @@ fn debug() {
debug!(?foo);
debug!(%foo);
debug!(foo);
debug!("foo" = ?foo);
debug!("foo" = %foo);
debug!("foo" = foo);
debug!(name: "foo", ?foo);
debug!(name: "foo", %foo);
debug!(name: "foo", foo);
Expand All @@ -633,12 +645,15 @@ fn info() {
info!(foo = ?3, bar.baz = %2, quux = false);
info!(foo = 3, bar.baz = 2, quux = false);
info!(foo = 3, bar.baz = 3,);
info!("foo" = 3, bar.baz = 3,);
info!(foo = 3, "bar.baz" = 3,);
info!("foo");
info!("foo: {}", 3);
info!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
info!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
info!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
info!({ foo = 3, bar.baz = 80 }, "quux");
info!({ "foo" = 3, "bar.baz" = 80 }, "quux");
info!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
info!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
info!({ foo = 2, bar.baz = 78 }, "quux");
Expand All @@ -657,6 +672,9 @@ fn info() {
info!(?foo);
info!(%foo);
info!(foo);
info!("foo" = ?foo);
info!("foo" = %foo);
info!("foo" = foo);
info!(name: "foo", ?foo);
info!(name: "foo", %foo);
info!(name: "foo", foo);
Expand All @@ -677,12 +695,15 @@ fn warn() {
warn!(foo = ?3, bar.baz = %2, quux = false);
warn!(foo = 3, bar.baz = 2, quux = false);
warn!(foo = 3, bar.baz = 3,);
warn!("foo" = 3, bar.baz = 3,);
warn!(foo = 3, "bar.baz" = 3,);
warn!("foo");
warn!("foo: {}", 3);
warn!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
warn!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
warn!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
warn!({ foo = 3, bar.baz = 80 }, "quux");
warn!({ "foo" = 3, "bar.baz" = 80 }, "quux");
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
warn!({ foo = 2, bar.baz = 78 }, "quux");
Expand All @@ -701,6 +722,9 @@ fn warn() {
warn!(?foo);
warn!(%foo);
warn!(foo);
warn!("foo" = ?foo);
warn!("foo" = %foo);
warn!("foo" = foo);
warn!(name: "foo", ?foo);
warn!(name: "foo", %foo);
warn!(name: "foo", foo);
Expand All @@ -721,15 +745,18 @@ fn error() {
error!(foo = ?3, bar.baz = %2, quux = false);
error!(foo = 3, bar.baz = 2, quux = false);
error!(foo = 3, bar.baz = 3,);
error!("foo" = 3, bar.baz = 3,);
error!(foo = 3, "bar.baz" = 3,);
error!("foo");
error!("foo: {}", 3);
error!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
error!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
error!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
error!({ foo = 3, bar.baz = 80 }, "quux");
error!({ "foo" = 3, "bar.baz" = 80 }, "quux");
error!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
error!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
error!({ foo = 2, bar.baz = 78, }, "quux");
error!({ foo = 2, bar.baz = 78 }, "quux");
error!({ foo = ?2, bar.baz = %78 }, "quux");
error!(name: "foo", foo = 3, bar.baz = 2, quux = false);
error!(name: "foo", target: "foo_events", foo = 3, bar.baz = 2, quux = false);
Expand All @@ -745,6 +772,9 @@ fn error() {
error!(?foo);
error!(%foo);
error!(foo);
error!("foo" = ?foo);
error!("foo" = %foo);
error!("foo" = foo);
error!(name: "foo", ?foo);
error!(name: "foo", %foo);
error!(name: "foo", foo);
Expand Down