tracing/
subscriber.rs

1//! Collects and records trace data.
2pub use tracing_core::subscriber::*;
3
4#[cfg(feature = "std")]
5#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
6pub 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")))]
20pub fn with_default<T, S>(subscriber: S, f: impl FnOnce() -> T) -> T
21where
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/// [`Subscriber`]: super::subscriber::Subscriber
37/// [`Event`]: super::event::Event
38pub fn set_global_default<S>(subscriber: S) -> Result<(), SetGlobalDefaultError>
39where
40    S: Subscriber + Send + Sync + 'static,
41{
42    crate::dispatcher::set_global_default(crate::Dispatch::new(subscriber))
43}
44
45/// Sets the [`Subscriber`] as the default for the current thread for the
46/// duration of the lifetime of the returned [`DefaultGuard`].
47///
48/// The default subscriber is used when creating a new [`Span`] or [`Event`].
49///
50/// [`Span`]: super::span::Span
51/// [`Subscriber`]: super::subscriber::Subscriber
52/// [`Event`]: super::event::Event
53/// [`DefaultGuard`]: super::dispatcher::DefaultGuard
54#[cfg(feature = "std")]
55#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
56#[must_use = "Dropping the guard unregisters the subscriber."]
57pub fn set_default<S>(subscriber: S) -> DefaultGuard
58where
59    S: Subscriber + Send + Sync + 'static,
60{
61    crate::dispatcher::set_default(&crate::Dispatch::new(subscriber))
62}
63
64pub use tracing_core::dispatcher::SetGlobalDefaultError;