1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
//! Dispatches trace events to `Subscriber`s. use { callsite, span, subscriber::{self, Subscriber}, Event, Metadata, }; use std::{ cell::RefCell, fmt, sync::{Arc, Weak}, }; /// `Dispatch` trace data to a [`Subscriber`]. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html #[derive(Clone)] pub struct Dispatch { subscriber: Arc<Subscriber + Send + Sync>, } thread_local! { static CURRENT_DISPATCH: RefCell<Dispatch> = RefCell::new(Dispatch::none()); } /// Sets this dispatch as the default for the duration of a closure. /// /// The default dispatcher is used when creating a new [span] or /// [`Event`], _if no span is currently executing_. If a span is currently /// executing, new spans or events are dispatched to the subscriber that /// tagged that span, instead. /// /// [span]: ../span/index.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`Event`]: ../event/struct.Event.html pub fn with_default<T>(dispatcher: &Dispatch, f: impl FnOnce() -> T) -> T { // A drop guard that resets CURRENT_DISPATCH to the prior dispatcher. // Using this (rather than simply resetting after calling `f`) ensures // that we always reset to the prior dispatcher even if `f` panics. struct ResetGuard(Option<Dispatch>); impl Drop for ResetGuard { fn drop(&mut self) { if let Some(dispatch) = self.0.take() { let _ = CURRENT_DISPATCH.try_with(|current| { *current.borrow_mut() = dispatch; }); } } } let dispatcher = dispatcher.clone(); let prior = CURRENT_DISPATCH.try_with(|current| current.replace(dispatcher)); let _guard = ResetGuard(prior.ok()); f() } /// Executes a closure with a reference to this thread's current [dispatcher]. /// /// [dispatcher]: ../dispatcher/struct.Dispatch.html pub fn get_default<T, F>(mut f: F) -> T where F: FnMut(&Dispatch) -> T, { CURRENT_DISPATCH .try_with(|current| f(&*current.borrow())) .unwrap_or_else(|_| f(&Dispatch::none())) } pub(crate) struct Registrar(Weak<Subscriber + Send + Sync>); impl Dispatch { /// Returns a new `Dispatch` that discards events and spans. pub fn none() -> Self { Dispatch { subscriber: Arc::new(NoSubscriber), } } /// Returns a `Dispatch` that forwards to the given [`Subscriber`]. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html pub fn new<S>(subscriber: S) -> Self where S: Subscriber + Send + Sync + 'static, { let me = Dispatch { subscriber: Arc::new(subscriber), }; callsite::register_dispatch(&me); me } pub(crate) fn registrar(&self) -> Registrar { Registrar(Arc::downgrade(&self.subscriber)) } /// Registers a new callsite with this subscriber, returning whether or not /// the subscriber is interested in being notified about the callsite. /// /// This calls the [`register_callsite`] function on the [`Subscriber`] /// that this `Dispatch` forwards to. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`register_callsite`]: ../subscriber/trait.Subscriber.html#method.register_callsite #[inline] pub fn register_callsite(&self, metadata: &Metadata) -> subscriber::Interest { self.subscriber.register_callsite(metadata) } /// Record the construction of a new span, returning a new [ID] for the /// span being constructed. /// /// This calls the [`new_span`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [ID]: ../span/struct.Id.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`new_span`]: ../subscriber/trait.Subscriber.html#method.new_span #[inline] pub fn new_span(&self, span: &span::Attributes) -> span::Id { self.subscriber.new_span(span) } /// Record a set of values on a span. /// /// This calls the [`record`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`record`]: ../subscriber/trait.Subscriber.html#method.record #[inline] pub fn record(&self, span: &span::Id, values: &span::Record) { self.subscriber.record(span, values) } /// Adds an indication that `span` follows from the span with the id /// `follows`. /// /// This calls the [`record_follows_from`] function on the [`Subscriber`] /// that this `Dispatch` forwards to. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`record_follows_from`]: ../subscriber/trait.Subscriber.html#method.record_follows_from #[inline] pub fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { self.subscriber.record_follows_from(span, follows) } /// Returns true if a span with the specified [metadata] would be /// recorded. /// /// This calls the [`enabled`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [metadata]: ../metadata/struct.Metadata.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`enabled`]: ../subscriber/trait.Subscriber.html#method.enabled #[inline] pub fn enabled(&self, metadata: &Metadata) -> bool { self.subscriber.enabled(metadata) } /// Records that an [`Event`] has occurred. /// /// This calls the [`event`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [`Event`]: ../event/struct.Event.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`event`]: ../subscriber/trait.Subscriber.html#method.event #[inline] pub fn event(&self, event: &Event) { self.subscriber.event(event) } /// Records that a span has been entered. /// /// This calls the [`enter`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`event`]: ../subscriber/trait.Subscriber.html#method.event #[inline] pub fn enter(&self, span: &span::Id) { self.subscriber.enter(span) } /// Records that a span has been exited. /// /// This calls the [`exit`](::Subscriber::exit) function on the `Subscriber` /// that this `Dispatch` forwards to. #[inline] pub fn exit(&self, span: &span::Id) { self.subscriber.exit(span) } /// Notifies the subscriber that a [span ID] has been cloned. /// /// This function is guaranteed to only be called with span IDs that were /// returned by this `Dispatch`'s [`new_span`] function. /// /// This calls the [`clone_span`] function on the `Subscriber` that this /// `Dispatch` forwards to. /// /// [span ID]: ../span/struct.Id.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`clone_span`]: ../subscriber/trait.Subscriber.html#method.clone_span /// [`new_span`]: ../subscriber/trait.Subscriber.html#method.new_span #[inline] pub fn clone_span(&self, id: &span::Id) -> span::Id { self.subscriber.clone_span(&id) } /// Notifies the subscriber that a [span ID] has been dropped. /// /// This function is guaranteed to only be called with span IDs that were /// returned by this `Dispatch`'s [`new_span`] function. /// /// This calls the [`drop_span`] function on the [`Subscriber`] that this /// `Dispatch` forwards to. /// /// [span ID]: ../span/struct.Id.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`clone_span`]: ../subscriber/trait.Subscriber.html#method.clone_span /// [`new_span`]: ../subscriber/trait.Subscriber.html#method.new_span #[inline] pub fn drop_span(&self, id: span::Id) { self.subscriber.drop_span(id) } } impl fmt::Debug for Dispatch { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("Dispatch(...)") } } impl<S> From<S> for Dispatch where S: Subscriber + Send + Sync + 'static, { #[inline] fn from(subscriber: S) -> Self { Dispatch::new(subscriber) } } struct NoSubscriber; impl Subscriber for NoSubscriber { #[inline] fn register_callsite(&self, _: &Metadata) -> subscriber::Interest { subscriber::Interest::never() } fn new_span(&self, _: &span::Attributes) -> span::Id { span::Id::from_u64(0xDEAD) } fn event(&self, _event: &Event) {} fn record(&self, _span: &span::Id, _values: &span::Record) {} fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} #[inline] fn enabled(&self, _metadata: &Metadata) -> bool { false } fn enter(&self, _span: &span::Id) {} fn exit(&self, _span: &span::Id) {} } impl Registrar { pub(crate) fn try_register(&self, metadata: &Metadata) -> Option<subscriber::Interest> { self.0.upgrade().map(|s| s.register_callsite(metadata)) } }