From 6bc791e4f8c705abc40643f0234d632af67fb312 Mon Sep 17 00:00:00 2001 From: RamziA961 <60425857+RamziA961@users.noreply.github.com> Date: Wed, 27 Sep 2023 21:48:21 +0100 Subject: [PATCH] feat(trace): add internal tracing macro module Introduce several macros that act as wrappers around the `tracing` module's macros to allow for more concise syntax for conditional compilation of `tracing` macro calls. As `tracing` is unstable, the new `trace` module will facilitate transitioning `tracing` to an optional feature, as outlined in #2874 and #3326. BREAKING CHANGES: Integration of macros requires the replacement of uses of `trace` macros. --- src/lib.rs | 2 + src/trace.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/trace.rs diff --git a/src/lib.rs b/src/lib.rs index 7de04debc3..319ddc9c07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,6 +68,8 @@ pub use crate::error::{Error, Result}; #[macro_use] mod cfg; #[macro_use] +mod trace; +#[macro_use] mod common; pub mod body; mod error; diff --git a/src/trace.rs b/src/trace.rs new file mode 100644 index 0000000000..c001e4092f --- /dev/null +++ b/src/trace.rs @@ -0,0 +1,107 @@ +#![allow(unused_macros)] + +macro_rules! debug { + ($($arg:tt)+) => { + #[cfg(feature = "tracing")] + { + println!($($arg)+); + tracing::debug!($($arg)+); + } + } +} + +macro_rules! debug_span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::debug_span!($($arg)+); + let _ = span.enter(); + } + } +} + +macro_rules! error { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + tracing::error!($($arg)+); + } + } +} + +macro_rules! error_span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::error_span!($($arg)+); + let _ = span.enter(); + } + } +} + +macro_rules! info { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + tracing::info!($($arg)+); + } + } +} + +macro_rules! info_span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::info_span!($($arg)+); + let _ = span.enter(); + } + } +} + +macro_rules! trace { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + tracing::trace!($($arg)+); + } + } +} + +macro_rules! trace_span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::trace_span!($($arg)+); + let _ = span.enter(); + } + } +} + +macro_rules! span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::span!($($arg)+); + let _ = span.enter(); + } + } +} + +macro_rules! warn { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + tracing::warn!($($arg)+); + } + } +} + +macro_rules! warn_span { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + { + let span = tracing::warn_span!($($arg)+); + let _ = span.enter(); + } + } +}