/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
-/// [attributes]: index.html#configuring-attributes
-/// [Fields]: index.html#recording-fields
-/// [`span!`]: macro.span.html
+/// [lib]: crate#using-the-macros
+/// [attributes]: crate#configuring-attributes
+/// [Fields]: crate#recording-fields
+/// [`span!`]: crate::span!
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
-/// [attributes]: index.html#configuring-attributes
-/// [Fields]: index.html#recording-fields
-/// [`span!`]: macro.span.html
+/// [lib]: crate#using-the-macros
+/// [attributes]: crate#configuring-attributes
+/// [Fields]: crate#recording-fields
+/// [`span!`]: crate::span!
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
-/// [attributes]: index.html#configuring-attributes
-/// [Fields]: index.html#recording-fields
-/// [`span!`]: macro.span.html
+/// [lib]: crate#using-the-macros
+/// [attributes]: crate#configuring-attributes
+/// [Fields]: crate#recording-fields
+/// [`span!`]: crate::span!
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
-/// [attributes]: index.html#configuring-attributes
-/// [Fields]: index.html#recording-fields
-/// [`span!`]: macro.span.html
+/// [lib]: crate#using-the-macros
+/// [attributes]: crate#configuring-attributes
+/// [Fields]: crate#recording-fields
+/// [`span!`]: crate::span!
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
-/// [attributes]: index.html#configuring-attributes
-/// [Fields]: index.html#recording-fields
-/// [`span!`]: macro.span.html
+/// [lib]: crate#using-the-macros
+/// [attributes]: crate#configuring-attributes
+/// [Fields]: crate#recording-fields
+/// [`span!`]: crate::span!
///
/// # Examples
///
/// See [the top-level documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [lib]: index.html#using-the-macros
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
if enabled {
(|value_set: $crate::field::ValueSet| {
$crate::__tracing_log!(
- target: $target,
$lvl,
+ CALLSITE,
&value_set
);
let meta = CALLSITE.metadata();
})($crate::valueset!(CALLSITE.metadata().fields(), $($fields)*));
} else {
$crate::__tracing_log!(
- target: $target,
$lvl,
+ CALLSITE,
&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)
);
}
&value_set
);
$crate::__tracing_log!(
- target: $target,
$lvl,
+ CALLSITE,
&value_set
);
})($crate::valueset!(CALLSITE.metadata().fields(), $($fields)*));
} else {
$crate::__tracing_log!(
- target: $target,
$lvl,
+ CALLSITE,
&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)
);
}
);
}
+/// Tests whether an event with the specified level and target would be enabled.
+///
+/// This is similar to [`enabled!`], but queries the current subscriber specifically for
+/// an event, whereas [`enabled!`] queries for an event _or_ span.
+///
+/// See the documentation for [`enabled!]` for more details on using this macro.
+/// See also [`span_enabled!`].
+///
+/// # Examples
+///
+/// ```rust
+/// # use tracing::{event_enabled, Level};
+/// if event_enabled!(target: "my_crate", Level::DEBUG) {
+/// // some expensive work...
+/// }
+/// // simpler
+/// if event_enabled!(Level::DEBUG) {
+/// // some expensive work...
+/// }
+/// // with fields
+/// if event_enabled!(Level::DEBUG, foo_field) {
+/// // some expensive work...
+/// }
+/// ```
+///
+#[macro_export]
+macro_rules! event_enabled {
+ ($($rest:tt)*)=> (
+ $crate::enabled!(kind: $crate::metadata::Kind::EVENT, $($rest)*)
+ )
+}
+
+/// Tests whether a span with the specified level and target would be enabled.
+///
+/// This is similar to [`enabled!`], but queries the current subscriber specifically for
+/// an event, whereas [`enabled!`] queries for an event _or_ span.
+///
+/// See the documentation for [`enabled!]` for more details on using this macro.
+/// See also [`span_enabled!`].
+///
+/// # Examples
+///
+/// ```rust
+/// # use tracing::{span_enabled, Level};
+/// if span_enabled!(target: "my_crate", Level::DEBUG) {
+/// // some expensive work...
+/// }
+/// // simpler
+/// if span_enabled!(Level::DEBUG) {
+/// // some expensive work...
+/// }
+/// // with fields
+/// if span_enabled!(Level::DEBUG, foo_field) {
+/// // some expensive work...
+/// }
+/// ```
+///
+#[macro_export]
+macro_rules! span_enabled {
+ ($($rest:tt)*)=> (
+ $crate::enabled!(kind: $crate::metadata::Kind::SPAN, $($rest)*)
+ )
+}
+
/// Checks whether a span or event is [enabled] based on the provided [metadata].
///
/// [enabled]: crate::Subscriber::enabled
/// }
/// ```
///
+/// # Alternatives
+///
+/// `enabled!` queries subscribers with [`Metadata`] where
+/// [`is_event`] and [`is_span`] both return `false`. Alternatively,
+/// use [`event_enabled!`] or [`span_enabled!`] to ensure one of these
+/// returns true.
+///
+///
+/// [`Metadata`]: crate::Metadata
+/// [`is_event`]: crate::Metadata::is_event
+/// [`is_span`]: crate::Metadata::is_span
+///
#[macro_export]
macro_rules! enabled {
- (target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({
+ (kind: $kind:expr, target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({
if $crate::level_enabled!($lvl) {
use $crate::__macro_support::Callsite as _;
static CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! {
":",
line!()
),
- kind: $crate::metadata::Kind::HINT,
+ kind: $kind.hint(),
target: $target,
level: $lvl,
fields: $($fields)*
}
});
// Just target and level
+ (kind: $kind:expr, target: $target:expr, $lvl:expr ) => (
+ $crate::enabled!(kind: $kind, target: $target, $lvl, { })
+ );
(target: $target:expr, $lvl:expr ) => (
- $crate::enabled!(target: $target, $lvl, { })
+ $crate::enabled!(kind: $crate::metadata::Kind::HINT, target: $target, $lvl, { })
);
- // These two cases handle fields with no values
+ // These four cases handle fields with no values
+ (kind: $kind:expr, target: $target:expr, $lvl:expr, $($field:tt)*) => (
+ $crate::enabled!(
+ kind: $kind,
+ target: $target,
+ $lvl,
+ { $($field)*}
+ )
+ );
(target: $target:expr, $lvl:expr, $($field:tt)*) => (
$crate::enabled!(
+ kind: $crate::metadata::Kind::HINT,
target: $target,
$lvl,
{ $($field)*}
)
);
- ($lvl:expr, $($field:tt)*) => (
+
+ // Level and field case
+ (kind: $kind:expr, $lvl:expr, $($field:tt)*) => (
$crate::enabled!(
+ kind: $kind,
target: module_path!(),
$lvl,
{ $($field)*}
);
// Simplest `enabled!` case
- ( $lvl:expr ) => (
- $crate::enabled!(target: module_path!(), $lvl, { })
+ (kind: $kind:expr, $lvl:expr) => (
+ $crate::enabled!(kind: $kind, target: module_path!(), $lvl, { })
+ );
+ ($lvl:expr) => (
+ $crate::enabled!(kind: $crate::metadata::Kind::HINT, target: module_path!(), $lvl, { })
+ );
+
+ // Fallthrough from above
+ ($lvl:expr, $($field:tt)*) => (
+ $crate::enabled!(
+ kind: $crate::metadata::Kind::HINT,
+ target: module_path!(),
+ $lvl,
+ { $($field)*}
+ )
);
}
/// documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [`event!`]: macro.event.html
-/// [lib]: index.html#using-the-macros
+/// [`event!`]: crate::event!
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
/// documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [`event!`]: macro.event.html
-/// [lib]: index.html#using-the-macros
+/// [`event!`]: crate::event!
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
/// documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [`event!`]: macro.event.html
-/// [lib]: index.html#using-the-macros
+/// [`event!`]: crate::event!
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
/// documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [`event!`]: macro.event.html
-/// [lib]: index.html#using-the-macros
+/// [`event!`]: crate::event!
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
/// documentation][lib] for details on the syntax accepted by
/// this macro.
///
-/// [`event!`]: macro.event.html
-/// [lib]: index.html#using-the-macros
+/// [`event!`]: crate::event!
+/// [lib]: crate#using-the-macros
///
/// # Examples
///
#[doc(hidden)]
#[macro_export]
macro_rules! __tracing_log {
- (target: $target:expr, $level:expr, $value_set:expr ) => {};
+ ($level:expr, $callsite:expr, $value_set:expr) => {};
}
#[cfg(feature = "log")]
#[doc(hidden)]
#[macro_export]
macro_rules! __tracing_log {
- (target: $target:expr, $level:expr, $value_set:expr ) => {
+ ($level:expr, $callsite:expr, $value_set:expr) => {
$crate::if_log_enabled! { $level, {
use $crate::log;
let level = $crate::level_to_log!($level);
if level <= log::max_level() {
let log_meta = log::Metadata::builder()
.level(level)
- .target($target)
+ .target(CALLSITE.metadata().target())
.build();
let logger = log::logger();
if logger.enabled(&log_meta) {
- logger.log(&log::Record::builder()
- .file(Some(file!()))
- .module_path(Some(module_path!()))
- .line(Some(line!()))
- .metadata(log_meta)
- .args(format_args!("{}", $crate::__macro_support::LogValueSet($value_set)))
- .build());
+ $callsite.log(logger, log_meta, $value_set)
}
}
}}