| 1 |
//! Collects and records trace data. |
| 2 |
pub use tracing_core::subscriber::*; |
| 3 |
|
| 4 |
#[cfg(feature = "std")] |
| 5 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 6 |
pub use tracing_core::dispatcher::DefaultGuard; |
| 7 |
|
| 8 |
/// Sets this [`Subscriber`] as the default for the current thread for the |
| 9 |
/// duration of a closure. |
| 10 |
/// |
| 11 |
/// The default subscriber is used when creating a new [`Span`] or |
| 12 |
/// [`Event`]. |
| 13 |
/// |
| 14 |
/// |
| 15 |
/// [`Span`]: super::span::Span |
| 16 |
/// [`Subscriber`]: super::subscriber::Subscriber |
| 17 |
/// [`Event`]: super::event::Event |
| 18 |
#[cfg(feature = "std")] |
| 19 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 20 |
pub fn with_default<T, S>(subscriber: S, f: impl FnOnce() -> T) -> T |
| 21 |
where |
| 22 |
S: Subscriber + Send + Sync + 'static, |
| 23 |
{ |
| 24 |
crate::dispatcher::with_default(&crate::Dispatch::new(subscriber), f) |
| 25 |
} |
| 26 |
|
| 27 |
/// Sets this subscriber as the global default for the duration of the entire program. |
| 28 |
/// Will be used as a fallback if no thread-local subscriber has been set in a thread (using `with_default`.) |
| 29 |
/// |
| 30 |
/// Can only be set once; subsequent attempts to set the global default will fail. |
| 31 |
/// Returns whether the initialization was successful. |
| 32 |
/// |
| 33 |
/// Note: Libraries should *NOT* call `set_global_default()`! That will cause conflicts when |
| 34 |
/// executables try to set them later. |
| 35 |
/// |
| 36 |
/// [span]: super::span |
| 37 |
/// [`Subscriber`]: super::subscriber::Subscriber |
| 38 |
/// [`Event`]: super::event::Event |
| 39 |
pub fn set_global_default<S>(subscriber: S) -> Result<(), SetGlobalDefaultError> |
| 40 |
where |
| 41 |
S: Subscriber + Send + Sync + 'static, |
| 42 |
{ |
| 43 |
crate::dispatcher::set_global_default(crate::Dispatch::new(subscriber)) |
| 44 |
} |
| 45 |
|
| 46 |
/// Sets the [`Subscriber`] as the default for the current thread for the |
| 47 |
/// duration of the lifetime of the returned [`DefaultGuard`]. |
| 48 |
/// |
| 49 |
/// The default subscriber is used when creating a new [`Span`] or [`Event`]. |
| 50 |
/// |
| 51 |
/// [`Span`]: super::span::Span |
| 52 |
/// [`Subscriber`]: super::subscriber::Subscriber |
| 53 |
/// [`Event`]: super::event::Event |
| 54 |
/// [`DefaultGuard`]: super::dispatcher::DefaultGuard |
| 55 |
#[cfg(feature = "std")] |
| 56 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 57 |
#[must_use = "Dropping the guard unregisters the subscriber."] |
| 58 |
pub fn set_default<S>(subscriber: S) -> DefaultGuard |
| 59 |
where |
| 60 |
S: Subscriber + Send + Sync + 'static, |
| 61 |
{ |
| 62 |
crate::dispatcher::set_default(&crate::Dispatch::new(subscriber)) |
| 63 |
} |
| 64 |
|
| 65 |
pub use tracing_core::dispatcher::SetGlobalDefaultError; |