]>
git.proxmox.com Git - rustc.git/blob - vendor/tracing-subscriber/src/util.rs
1 //! Extension traits and other utilities to make working with subscribers more
3 use std
::{error::Error, fmt}
;
4 use tracing_core
::dispatcher
::{self, Dispatch}
;
6 /// Extension trait adding utility methods for subscriber initialization.
8 /// This trait provides extension methods to make configuring and setting a
9 /// [default subscriber] more ergonomic. It is automatically implemented for all
10 /// types that can be converted into a [trace dispatcher]. Since `Dispatch`
11 /// implements `From<T>` for all `T: Subscriber`, all `Subscriber`
12 /// implementations will implement this extension trait as well. Types which
13 /// can be converted into `Subscriber`s, such as builders that construct a
14 /// `Subscriber`, may implement `Into<Dispatch>`, and will also receive an
15 /// implementation of this trait.
17 /// [default subscriber]: https://docs.rs/tracing/0.1.19/tracing/dispatcher/index.html#setting-the-default-subscriber
18 /// [trace dispatcher]: https://docs.rs/tracing/0.1.19/tracing/dispatcher/index.html
19 pub trait SubscriberInitExt
23 /// Sets `self` as the [default subscriber] in the current scope, returning a
24 /// guard that will unset it when dropped.
26 /// If the "tracing-log" feature flag is enabled, this will also initialize
27 /// a [`log`] compatibility layer. This allows the subscriber to consume
28 /// `log::Record`s as though they were `tracing` `Event`s.
30 /// [default subscriber]: https://docs.rs/tracing/0.1.19/tracing/dispatcher/index.html#setting-the-default-subscriber
31 /// [`log`]: https://crates.io/log
32 fn set_default(self) -> dispatcher
::DefaultGuard
{
33 #[cfg(feature = "tracing-log")]
34 let _
= tracing_log
::LogTracer
::init();
36 dispatcher
::set_default(&self.into())
39 /// Attempts to set `self` as the [global default subscriber] in the current
40 /// scope, returning an error if one is already set.
42 /// If the "tracing-log" feature flag is enabled, this will also attempt to
43 /// initialize a [`log`] compatibility layer. This allows the subscriber to
44 /// consume `log::Record`s as though they were `tracing` `Event`s.
46 /// This method returns an error if a global default subscriber has already
47 /// been set, or if a `log` logger has already been set (when the
48 /// "tracing-log" feature is enabled).
50 /// [global default subscriber]: https://docs.rs/tracing/0.1.19/tracing/dispatcher/index.html#setting-the-default-subscriber
51 /// [`log`]: https://crates.io/log
52 fn try_init(self) -> Result
<(), TryInitError
> {
53 #[cfg(feature = "tracing-log")]
54 tracing_log
::LogTracer
::init().map_err(TryInitError
::new
)?
;
56 dispatcher
::set_global_default(self.into()).map_err(TryInitError
::new
)?
;
61 /// Attempts to set `self` as the [global default subscriber] in the current
62 /// scope, panicking if this fails.
64 /// If the "tracing-log" feature flag is enabled, this will also attempt to
65 /// initialize a [`log`] compatibility layer. This allows the subscriber to
66 /// consume `log::Record`s as though they were `tracing` `Event`s.
68 /// This method panics if a global default subscriber has already been set,
69 /// or if a `log` logger has already been set (when the "tracing-log"
70 /// feature is enabled).
72 /// [global default subscriber]: https://docs.rs/tracing/0.1.19/tracing/dispatcher/index.html#setting-the-default-subscriber
73 /// [`log`]: https://crates.io/log
76 .expect("failed to set global default subscriber")
80 impl<T
> SubscriberInitExt
for T
where T
: Into
<Dispatch
> {}
82 /// Error returned by [`try_init`] if a global default subscriber could not be initialized.
83 pub struct TryInitError
{
84 inner
: Box
<dyn Error
+ Send
+ Sync
+ '
static>,
87 // ==== impl TryInitError ====
90 fn new(e
: impl Into
<Box
<dyn Error
+ Send
+ Sync
+ '
static>>) -> Self {
91 Self { inner: e.into() }
95 impl fmt
::Debug
for TryInitError
{
96 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
97 fmt
::Debug
::fmt(&self.inner
, f
)
101 impl fmt
::Display
for TryInitError
{
102 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
103 fmt
::Display
::fmt(&self.inner
, f
)
107 impl Error
for TryInitError
{
108 fn source(&self) -> Option
<&(dyn Error
+ '
static)> {