diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
commit | d1b2d29528b7794b41e66fc2136e395a02f8529b (patch) | |
tree | a4a17504b260206dec3cf55b2dca82929a348ac2 /vendor/tracing-core/src | |
parent | Releasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tracing-core/src')
-rw-r--r-- | vendor/tracing-core/src/dispatcher.rs | 181 | ||||
-rw-r--r-- | vendor/tracing-core/src/field.rs | 4 | ||||
-rw-r--r-- | vendor/tracing-core/src/lib.rs | 10 | ||||
-rw-r--r-- | vendor/tracing-core/src/metadata.rs | 1 | ||||
-rw-r--r-- | vendor/tracing-core/src/subscriber.rs | 8 |
5 files changed, 139 insertions, 65 deletions
diff --git a/vendor/tracing-core/src/dispatcher.rs b/vendor/tracing-core/src/dispatcher.rs index 36b3cfd85..32632abdc 100644 --- a/vendor/tracing-core/src/dispatcher.rs +++ b/vendor/tracing-core/src/dispatcher.rs @@ -140,7 +140,7 @@ use crate::stdlib::{ #[cfg(feature = "std")] use crate::stdlib::{ - cell::{Cell, RefCell, RefMut}, + cell::{Cell, Ref, RefCell}, error, }; @@ -153,7 +153,7 @@ use core::ops::Deref; /// `Dispatch` trace data to a [`Subscriber`]. #[derive(Clone)] pub struct Dispatch { - subscriber: Arc<dyn Subscriber + Send + Sync>, + subscriber: Kind<Arc<dyn Subscriber + Send + Sync>>, } /// `WeakDispatch` is a version of [`Dispatch`] that holds a non-owning reference @@ -176,13 +176,12 @@ pub struct Dispatch { /// [here]: Subscriber#avoiding-memory-leaks #[derive(Clone)] pub struct WeakDispatch { - subscriber: Weak<dyn Subscriber + Send + Sync>, + subscriber: Kind<Weak<dyn Subscriber + Send + Sync>>, } -#[cfg(feature = "alloc")] #[derive(Clone)] enum Kind<T> { - Global(&'static (dyn Collect + Send + Sync)), + Global(&'static (dyn Subscriber + Send + Sync)), Scoped(T), } @@ -197,11 +196,20 @@ thread_local! { static EXISTS: AtomicBool = AtomicBool::new(false); static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED); +#[cfg(feature = "std")] +static SCOPED_COUNT: AtomicUsize = AtomicUsize::new(0); + const UNINITIALIZED: usize = 0; const INITIALIZING: usize = 1; const INITIALIZED: usize = 2; -static mut GLOBAL_DISPATCH: Option<Dispatch> = None; +static mut GLOBAL_DISPATCH: Dispatch = Dispatch { + subscriber: Kind::Global(&NO_SUBSCRIBER), +}; +static NONE: Dispatch = Dispatch { + subscriber: Kind::Global(&NO_SUBSCRIBER), +}; +static NO_SUBSCRIBER: NoSubscriber = NoSubscriber::new(); /// The dispatch state of a thread. #[cfg(feature = "std")] @@ -305,8 +313,20 @@ pub fn set_global_default(dispatcher: Dispatch) -> Result<(), SetGlobalDefaultEr ) .is_ok() { + let subscriber = { + let subscriber = match dispatcher.subscriber { + Kind::Global(s) => s, + Kind::Scoped(s) => unsafe { + // safety: this leaks the subscriber onto the heap. the + // reference count will always be at least 1, because the + // global default will never be dropped. + &*Arc::into_raw(s) + }, + }; + Kind::Global(subscriber) + }; unsafe { - GLOBAL_DISPATCH = Some(dispatcher); + GLOBAL_DISPATCH = Dispatch { subscriber }; } GLOBAL_INIT.store(INITIALIZED, Ordering::SeqCst); EXISTS.store(true, Ordering::Release); @@ -365,15 +385,21 @@ pub fn get_default<T, F>(mut f: F) -> T where F: FnMut(&Dispatch) -> T, { + if SCOPED_COUNT.load(Ordering::Acquire) == 0 { + // fast path if no scoped dispatcher has been set; just use the global + // default. + return f(get_global()); + } + CURRENT_STATE .try_with(|state| { if let Some(entered) = state.enter() { - return f(&*entered.current()); + return f(&entered.current()); } - f(&Dispatch::none()) + f(&NONE) }) - .unwrap_or_else(|_| f(&Dispatch::none())) + .unwrap_or_else(|_| f(&NONE)) } /// Executes a closure with a reference to this thread's current [dispatcher]. @@ -387,10 +413,16 @@ where #[doc(hidden)] #[inline(never)] pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> { + if SCOPED_COUNT.load(Ordering::Acquire) == 0 { + // fast path if no scoped dispatcher has been set; just use the global + // default. + return Some(f(get_global())); + } + CURRENT_STATE .try_with(|state| { let entered = state.enter()?; - Some(f(&*entered.current())) + Some(f(&entered.current())) }) .ok()? } @@ -401,8 +433,7 @@ pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> { #[cfg(not(feature = "std"))] #[doc(hidden)] pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> { - let dispatch = get_global()?; - Some(f(&dispatch)) + Some(f(get_global())) } /// Executes a closure with a reference to the current [dispatcher]. @@ -413,35 +444,30 @@ pub fn get_default<T, F>(mut f: F) -> T where F: FnMut(&Dispatch) -> T, { - if let Some(d) = get_global() { - f(d) - } else { - f(&Dispatch::none()) - } + f(&get_global()) } -fn get_global() -> Option<&'static Dispatch> { +#[inline] +fn get_global() -> &'static Dispatch { if GLOBAL_INIT.load(Ordering::SeqCst) != INITIALIZED { - return None; + return &NONE; } unsafe { // This is safe given the invariant that setting the global dispatcher // also sets `GLOBAL_INIT` to `INITIALIZED`. - Some(GLOBAL_DISPATCH.as_ref().expect( - "invariant violated: GLOBAL_DISPATCH must be initialized before GLOBAL_INIT is set", - )) + &GLOBAL_DISPATCH } } #[cfg(feature = "std")] -pub(crate) struct Registrar(Weak<dyn Subscriber + Send + Sync>); +pub(crate) struct Registrar(Kind<Weak<dyn Subscriber + Send + Sync>>); impl Dispatch { /// Returns a new `Dispatch` that discards events and spans. #[inline] pub fn none() -> Self { Dispatch { - subscriber: Arc::new(NoSubscriber::default()), + subscriber: Kind::Global(&NO_SUBSCRIBER), } } @@ -453,7 +479,7 @@ impl Dispatch { S: Subscriber + Send + Sync + 'static, { let me = Dispatch { - subscriber: Arc::new(subscriber), + subscriber: Kind::Scoped(Arc::new(subscriber)), }; callsite::register_dispatch(&me); me @@ -461,7 +487,7 @@ impl Dispatch { #[cfg(feature = "std")] pub(crate) fn registrar(&self) -> Registrar { - Registrar(Arc::downgrade(&self.subscriber)) + Registrar(self.subscriber.downgrade()) } /// Creates a [`WeakDispatch`] from this `Dispatch`. @@ -480,14 +506,16 @@ impl Dispatch { /// [here]: Subscriber#avoiding-memory-leaks pub fn downgrade(&self) -> WeakDispatch { WeakDispatch { - subscriber: Arc::downgrade(&self.subscriber), + subscriber: self.subscriber.downgrade(), } } #[inline(always)] - #[cfg(not(feature = "alloc"))] pub(crate) fn subscriber(&self) -> &(dyn Subscriber + Send + Sync) { - &self.subscriber + match self.subscriber { + Kind::Global(s) => s, + Kind::Scoped(ref s) => s.as_ref(), + } } /// Registers a new callsite with this collector, returning whether or not @@ -500,7 +528,7 @@ impl Dispatch { /// [`register_callsite`]: super::subscriber::Subscriber::register_callsite #[inline] pub fn register_callsite(&self, metadata: &'static Metadata<'static>) -> subscriber::Interest { - self.subscriber.register_callsite(metadata) + self.subscriber().register_callsite(metadata) } /// Returns the highest [verbosity level][level] that this [`Subscriber`] will @@ -516,7 +544,7 @@ impl Dispatch { // TODO(eliza): consider making this a public API? #[inline] pub(crate) fn max_level_hint(&self) -> Option<LevelFilter> { - self.subscriber.max_level_hint() + self.subscriber().max_level_hint() } /// Record the construction of a new span, returning a new [ID] for the @@ -530,7 +558,7 @@ impl Dispatch { /// [`new_span`]: super::subscriber::Subscriber::new_span #[inline] pub fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { - self.subscriber.new_span(span) + self.subscriber().new_span(span) } /// Record a set of values on a span. @@ -542,7 +570,7 @@ impl Dispatch { /// [`record`]: super::subscriber::Subscriber::record #[inline] pub fn record(&self, span: &span::Id, values: &span::Record<'_>) { - self.subscriber.record(span, values) + self.subscriber().record(span, values) } /// Adds an indication that `span` follows from the span with the id @@ -555,7 +583,7 @@ impl Dispatch { /// [`record_follows_from`]: super::subscriber::Subscriber::record_follows_from #[inline] pub fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { - self.subscriber.record_follows_from(span, follows) + self.subscriber().record_follows_from(span, follows) } /// Returns true if a span with the specified [metadata] would be @@ -569,7 +597,7 @@ impl Dispatch { /// [`enabled`]: super::subscriber::Subscriber::enabled #[inline] pub fn enabled(&self, metadata: &Metadata<'_>) -> bool { - self.subscriber.enabled(metadata) + self.subscriber().enabled(metadata) } /// Records that an [`Event`] has occurred. @@ -582,8 +610,9 @@ impl Dispatch { /// [`event`]: super::subscriber::Subscriber::event #[inline] pub fn event(&self, event: &Event<'_>) { - if self.subscriber.event_enabled(event) { - self.subscriber.event(event); + let subscriber = self.subscriber(); + if subscriber.event_enabled(event) { + subscriber.event(event); } } @@ -595,7 +624,7 @@ impl Dispatch { /// [`Subscriber`]: super::subscriber::Subscriber /// [`enter`]: super::subscriber::Subscriber::enter pub fn enter(&self, span: &span::Id) { - self.subscriber.enter(span); + self.subscriber().enter(span); } /// Records that a span has been exited. @@ -606,7 +635,7 @@ impl Dispatch { /// [`Subscriber`]: super::subscriber::Subscriber /// [`exit`]: super::subscriber::Subscriber::exit pub fn exit(&self, span: &span::Id) { - self.subscriber.exit(span); + self.subscriber().exit(span); } /// Notifies the subscriber that a [span ID] has been cloned. @@ -625,7 +654,7 @@ impl Dispatch { /// [`new_span`]: super::subscriber::Subscriber::new_span #[inline] pub fn clone_span(&self, id: &span::Id) -> span::Id { - self.subscriber.clone_span(id) + self.subscriber().clone_span(id) } /// Notifies the subscriber that a [span ID] has been dropped. @@ -654,7 +683,7 @@ impl Dispatch { #[deprecated(since = "0.1.2", note = "use `Dispatch::try_close` instead")] pub fn drop_span(&self, id: span::Id) { #[allow(deprecated)] - self.subscriber.drop_span(id); + self.subscriber().drop_span(id); } /// Notifies the subscriber that a [span ID] has been dropped, and returns @@ -673,7 +702,7 @@ impl Dispatch { /// [`try_close`]: super::subscriber::Subscriber::try_close /// [`new_span`]: super::subscriber::Subscriber::new_span pub fn try_close(&self, id: span::Id) -> bool { - self.subscriber.try_close(id) + self.subscriber().try_close(id) } /// Returns a type representing this subscriber's view of the current span. @@ -684,21 +713,21 @@ impl Dispatch { /// [`current`]: super::subscriber::Subscriber::current_span #[inline] pub fn current_span(&self) -> span::Current { - self.subscriber.current_span() + self.subscriber().current_span() } /// Returns `true` if this `Dispatch` forwards to a `Subscriber` of type /// `T`. #[inline] pub fn is<T: Any>(&self) -> bool { - <dyn Subscriber>::is::<T>(&self.subscriber) + <dyn Subscriber>::is::<T>(self.subscriber()) } /// Returns some reference to the `Subscriber` this `Dispatch` forwards to /// if it is of type `T`, or `None` if it isn't. #[inline] pub fn downcast_ref<T: Any>(&self) -> Option<&T> { - <dyn Subscriber>::downcast_ref(&self.subscriber) + <dyn Subscriber>::downcast_ref(self.subscriber()) } } @@ -711,9 +740,16 @@ impl Default for Dispatch { impl fmt::Debug for Dispatch { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("Dispatch") - .field(&format_args!("{:p}", self.subscriber)) - .finish() + match self.subscriber { + Kind::Scoped(ref s) => f + .debug_tuple("Dispatch::Scoped") + .field(&format_args!("{:p}", s)) + .finish(), + Kind::Global(s) => f + .debug_tuple("Dispatch::Global") + .field(&format_args!("{:p}", s)) + .finish(), + } } } @@ -757,12 +793,16 @@ impl WeakDispatch { impl fmt::Debug for WeakDispatch { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut tuple = f.debug_tuple("WeakDispatch"); - match self.subscriber.upgrade() { - Some(subscriber) => tuple.field(&format_args!("Some({:p})", subscriber)), - None => tuple.field(&format_args!("None")), - }; - tuple.finish() + match self.subscriber { + Kind::Scoped(ref s) => f + .debug_tuple("WeakDispatch::Scoped") + .field(&format_args!("{:p}", s)) + .finish(), + Kind::Global(s) => f + .debug_tuple("WeakDispatch::Global") + .field(&format_args!("{:p}", s)) + .finish(), + } } } @@ -775,6 +815,26 @@ impl Registrar { // ===== impl State ===== +impl Kind<Arc<dyn Subscriber + Send + Sync>> { + fn downgrade(&self) -> Kind<Weak<dyn Subscriber + Send + Sync>> { + match self { + Kind::Global(s) => Kind::Global(*s), + Kind::Scoped(ref s) => Kind::Scoped(Arc::downgrade(s)), + } + } +} + +impl Kind<Weak<dyn Subscriber + Send + Sync>> { + fn upgrade(&self) -> Option<Kind<Arc<dyn Subscriber + Send + Sync>>> { + match self { + Kind::Global(s) => Some(Kind::Global(*s)), + Kind::Scoped(ref s) => Some(Kind::Scoped(s.upgrade()?)), + } + } +} + +// ===== impl State ===== + #[cfg(feature = "std")] impl State { /// Replaces the current default dispatcher on this thread with the provided @@ -792,6 +852,7 @@ impl State { .ok() .flatten(); EXISTS.store(true, Ordering::Release); + SCOPED_COUNT.fetch_add(1, Ordering::Release); DefaultGuard(prior) } @@ -810,10 +871,11 @@ impl State { #[cfg(feature = "std")] impl<'a> Entered<'a> { #[inline] - fn current(&self) -> RefMut<'a, Dispatch> { - let default = self.0.default.borrow_mut(); - RefMut::map(default, |default| { - default.get_or_insert_with(|| get_global().cloned().unwrap_or_else(Dispatch::none)) + fn current(&self) -> Ref<'a, Dispatch> { + let default = self.0.default.borrow(); + Ref::map(default, |default| match default { + Some(default) => default, + None => get_global(), }) } } @@ -838,6 +900,7 @@ impl Drop for DefaultGuard { // could then also attempt to access the same thread local // state -- causing a clash. let prev = CURRENT_STATE.try_with(|state| state.default.replace(self.0.take())); + SCOPED_COUNT.fetch_sub(1, Ordering::Release); drop(prev) } } diff --git a/vendor/tracing-core/src/field.rs b/vendor/tracing-core/src/field.rs index e103c75a9..04b8e1b29 100644 --- a/vendor/tracing-core/src/field.rs +++ b/vendor/tracing-core/src/field.rs @@ -315,6 +315,7 @@ pub trait Visit { /// <strong>Note</strong>: This is only enabled when the Rust standard library is /// present. /// </pre> + /// </div> #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { @@ -819,6 +820,7 @@ impl FieldSet { /// /// [`Identifier`]: super::callsite::Identifier /// [`Callsite`]: super::callsite::Callsite + #[inline] pub(crate) fn callsite(&self) -> callsite::Identifier { callsite::Identifier(self.callsite.0) } @@ -856,6 +858,7 @@ impl FieldSet { } /// Returns an iterator over the `Field`s in this `FieldSet`. + #[inline] pub fn iter(&self) -> Iter { let idxs = 0..self.len(); Iter { @@ -959,6 +962,7 @@ impl PartialEq for FieldSet { impl Iterator for Iter { type Item = Field; + #[inline] fn next(&mut self) -> Option<Field> { let i = self.idxs.next()?; Some(Field { diff --git a/vendor/tracing-core/src/lib.rs b/vendor/tracing-core/src/lib.rs index c1f87b22f..f5de4eeba 100644 --- a/vendor/tracing-core/src/lib.rs +++ b/vendor/tracing-core/src/lib.rs @@ -23,7 +23,7 @@ //! In addition, it defines the global callsite registry and per-thread current //! dispatcher which other components of the tracing system rely on. //! -//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! *Compiler support: [requires `rustc` 1.56+][msrv]* //! //! [msrv]: #supported-rust-versions //! @@ -92,14 +92,14 @@ //! ## Supported Rust Versions //! //! Tracing is built against the latest stable release. The minimum supported -//! version is 1.49. The current Tracing version is not guaranteed to build on +//! version is 1.56. The current Tracing version is not guaranteed to build on //! Rust versions earlier than the minimum supported version. //! //! Tracing follows the same compiler support policies as the rest of the Tokio //! project. The current stable Rust compiler and the three most recent minor //! versions before it will always be supported. For example, if the current -//! stable compiler version is 1.45, the minimum supported version will not be -//! increased past 1.42, three minor versions prior. Increasing the minimum +//! stable compiler version is 1.69, the minimum supported version will not be +//! increased past 1.66, three minor versions prior. Increasing the minimum //! supported compiler version is not considered a semver breaking change as //! long as doing so complies with this policy. //! @@ -116,7 +116,6 @@ //! [`Dispatch`]: dispatcher::Dispatch //! [`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing //! [`tracing`]: https://crates.io/crates/tracing -#![doc(html_root_url = "https://docs.rs/tracing-core/0.1.22")] #![doc( html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" @@ -129,7 +128,6 @@ rust_2018_idioms, unreachable_pub, bad_style, - const_err, dead_code, improper_ctypes, non_shorthand_field_patterns, diff --git a/vendor/tracing-core/src/metadata.rs b/vendor/tracing-core/src/metadata.rs index a154419a7..5e475c129 100644 --- a/vendor/tracing-core/src/metadata.rs +++ b/vendor/tracing-core/src/metadata.rs @@ -273,6 +273,7 @@ impl<'a> Metadata<'a> { } /// Returns the names of the fields on the described span or event. + #[inline] pub fn fields(&self) -> &field::FieldSet { &self.fields } diff --git a/vendor/tracing-core/src/subscriber.rs b/vendor/tracing-core/src/subscriber.rs index e8f444119..17b631697 100644 --- a/vendor/tracing-core/src/subscriber.rs +++ b/vendor/tracing-core/src/subscriber.rs @@ -699,6 +699,14 @@ impl Subscriber for NoSubscriber { fn exit(&self, _span: &span::Id) {} } +impl NoSubscriber { + /// Returns a new `NoSubscriber`. + #[must_use] + pub const fn new() -> Self { + Self(()) + } +} + impl<S> Subscriber for Box<S> where S: Subscriber + ?Sized, |