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

Use macros for module declarations #1009

Merged
merged 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 12 additions & 32 deletions tracing-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,10 @@
use tracing_core::span::Id;

#[macro_use]
macro_rules! try_lock {
($lock:expr) => {
try_lock!($lock, else return)
};
($lock:expr, else $els:expr) => {
if let Ok(l) = $lock {
l
} else if std::thread::panicking() {
$els
} else {
panic!("lock poisoned")
}
};
}
mod macros;

pub mod field;
pub mod filter;
#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub mod fmt;
pub mod layer;
pub mod prelude;
pub mod registry;
Expand All @@ -132,24 +116,20 @@ pub use filter::EnvFilter;

pub use layer::Layer;

#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub use registry::Registry;

///
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub fn registry() -> Registry {
Registry::default()
cfg_fmt! {
pub mod fmt;
pub use fmt::Subscriber as FmtSubscriber;
pub use fmt::fmt;
}

#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub use fmt::Subscriber as FmtSubscriber;
cfg_registry! {
pub use registry::Registry;

#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub use fmt::fmt;
///
pub fn registry() -> Registry {
Registry::default()
}
}

use std::default::Default;
/// Tracks the currently executing span on a per-thread basis.
Expand Down
38 changes: 38 additions & 0 deletions tracing-subscriber/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
macro_rules! try_lock {
($lock:expr) => {
try_lock!($lock, else return)
};
($lock:expr, else $els:expr) => {
if let Ok(l) = $lock {
l
} else if std::thread::panicking() {
$els
} else {
panic!("lock poisoned")
}
};
}

/// Declares fmt items.
#[doc(hidden)]
macro_rules! cfg_fmt {
($($item:item)*) => {
$(
#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
$item
)*
}
}
Copy link
Contributor

@jyn514 jyn514 Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make this one macro instead of two by taking the feature as a parameter, something like this:

Suggested change
macro_rules! cfg_fmt {
($($item:item)*) => {
$(
#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
$item
)*
}
}
macro_rules! cfg_fmt {
(#[feature = $($name: lit) ] $($item:item)*) => {
$(
#[cfg(feature = $name)]
#[cfg_attr(docsrs, doc(cfg(feature = $name)))]
$item
)*
}
}

Copy link
Contributor Author

@k-nasa k-nasa Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it as follows.
9b4f0e3


/// Declares registry items.
#[doc(hidden)]
macro_rules! cfg_registry {
($($item:item)*) => {
$(
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
$item
)*
}
}