]> git.proxmox.com Git - rustc.git/blame - vendor/tracing-core/src/callsite.rs
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / vendor / tracing-core / src / callsite.rs
CommitLineData
f035d41b
XL
1//! Callsites represent the source locations from which spans or events
2//! originate.
3use crate::stdlib::{
4 fmt,
5 hash::{Hash, Hasher},
f035d41b
XL
6 sync::Mutex,
7 vec::Vec,
8};
9use crate::{
3dfed10e
XL
10 dispatcher::{self, Dispatch},
11 metadata::{LevelFilter, Metadata},
f035d41b 12 subscriber::Interest,
f035d41b
XL
13};
14
15lazy_static! {
16 static ref REGISTRY: Mutex<Registry> = Mutex::new(Registry {
17 callsites: Vec::new(),
18 dispatchers: Vec::new(),
19 });
20}
21
22struct Registry {
23 callsites: Vec<&'static dyn Callsite>,
24 dispatchers: Vec<dispatcher::Registrar>,
25}
26
27impl Registry {
28 fn rebuild_callsite_interest(&self, callsite: &'static dyn Callsite) {
29 let meta = callsite.metadata();
30
1b1a35ee
XL
31 // Iterate over the subscribers in the registry, and — if they are
32 // active — register the callsite with them.
33 let mut interests = self
34 .dispatchers
35 .iter()
36 .filter_map(|registrar| registrar.try_register(meta));
f035d41b 37
1b1a35ee
XL
38 // Use the first subscriber's `Interest` as the base value.
39 let interest = if let Some(interest) = interests.next() {
40 // Combine all remaining `Interest`s.
41 interests.fold(interest, Interest::and)
42 } else {
43 // If nobody was interested in this thing, just return `never`.
44 Interest::never()
45 };
f035d41b
XL
46
47 callsite.set_interest(interest)
48 }
49
50 fn rebuild_interest(&mut self) {
3dfed10e
XL
51 let mut max_level = LevelFilter::OFF;
52 self.dispatchers.retain(|registrar| {
53 if let Some(dispatch) = registrar.upgrade() {
54 // If the subscriber did not provide a max level hint, assume
55 // that it may enable every level.
56 let level_hint = dispatch.max_level_hint().unwrap_or(LevelFilter::TRACE);
57 if level_hint > max_level {
58 max_level = level_hint;
59 }
60 true
61 } else {
62 false
63 }
64 });
f035d41b
XL
65
66 self.callsites.iter().for_each(|&callsite| {
67 self.rebuild_callsite_interest(callsite);
68 });
3dfed10e 69 LevelFilter::set_max(max_level);
f035d41b
XL
70 }
71}
72
73/// Trait implemented by callsites.
74///
29967ef6 75/// These functions are only intended to be called by the callsite registry, which
f035d41b
XL
76/// correctly handles determining the common interest between all subscribers.
77pub trait Callsite: Sync {
78 /// Sets the [`Interest`] for this callsite.
79 ///
80 /// [`Interest`]: ../subscriber/struct.Interest.html
81 fn set_interest(&self, interest: Interest);
82
83 /// Returns the [metadata] associated with the callsite.
84 ///
85 /// [metadata]: ../metadata/struct.Metadata.html
86 fn metadata(&self) -> &Metadata<'_>;
87}
88
89/// Uniquely identifies a [`Callsite`]
90///
91/// Two `Identifier`s are equal if they both refer to the same callsite.
92///
93/// [`Callsite`]: ../callsite/trait.Callsite.html
94#[derive(Clone)]
95pub struct Identifier(
96 /// **Warning**: The fields on this type are currently `pub` because it must
97 /// be able to be constructed statically by macros. However, when `const
98 /// fn`s are available on stable Rust, this will no longer be necessary.
99 /// Thus, these fields are *not* considered stable public API, and they may
100 /// change warning. Do not rely on any fields on `Identifier`. When
101 /// constructing new `Identifier`s, use the `identify_callsite!` macro or
102 /// the `Callsite::id` function instead.
103 // TODO: When `Callsite::id` is a const fn, this need no longer be `pub`.
104 #[doc(hidden)]
105 pub &'static dyn Callsite,
106);
107
108/// Clear and reregister interest on every [`Callsite`]
109///
110/// This function is intended for runtime reconfiguration of filters on traces
111/// when the filter recalculation is much less frequent than trace events are.
112/// The alternative is to have the [`Subscriber`] that supports runtime
113/// reconfiguration of filters always return [`Interest::sometimes()`] so that
114/// [`enabled`] is evaluated for every event.
115///
3dfed10e 116/// This function will also re-compute the global maximum level as determined by
29967ef6 117/// the [`max_level_hint`] method. If a [`Subscriber`]
3dfed10e
XL
118/// implementation changes the value returned by its `max_level_hint`
119/// implementation at runtime, then it **must** call this function after that
120/// value changes, in order for the change to be reflected.
121///
29967ef6 122/// [`max_level_hint`]: ../subscriber/trait.Subscriber.html#method.max_level_hint
f035d41b
XL
123/// [`Callsite`]: ../callsite/trait.Callsite.html
124/// [`enabled`]: ../subscriber/trait.Subscriber.html#tymethod.enabled
125/// [`Interest::sometimes()`]: ../subscriber/struct.Interest.html#method.sometimes
126/// [`Subscriber`]: ../subscriber/trait.Subscriber.html
127pub fn rebuild_interest_cache() {
128 let mut registry = REGISTRY.lock().unwrap();
129 registry.rebuild_interest();
130}
131
132/// Register a new `Callsite` with the global registry.
133///
134/// This should be called once per callsite after the callsite has been
135/// constructed.
136pub fn register(callsite: &'static dyn Callsite) {
137 let mut registry = REGISTRY.lock().unwrap();
138 registry.rebuild_callsite_interest(callsite);
139 registry.callsites.push(callsite);
140}
141
142pub(crate) fn register_dispatch(dispatch: &Dispatch) {
143 let mut registry = REGISTRY.lock().unwrap();
144 registry.dispatchers.push(dispatch.registrar());
145 registry.rebuild_interest();
146}
147
148// ===== impl Identifier =====
149
150impl PartialEq for Identifier {
151 fn eq(&self, other: &Identifier) -> bool {
3dfed10e 152 self.0 as *const _ as *const () == other.0 as *const _ as *const ()
f035d41b
XL
153 }
154}
155
156impl Eq for Identifier {}
157
158impl fmt::Debug for Identifier {
159 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160 write!(f, "Identifier({:p})", self.0)
161 }
162}
163
164impl Hash for Identifier {
165 fn hash<H>(&self, state: &mut H)
166 where
167 H: Hasher,
168 {
169 (self.0 as *const dyn Callsite).hash(state)
170 }
171}