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
278
279
280
281
282
283
284
285
use crate::layer;
use crate::sync::RwLock;
use std::{
error, fmt,
marker::PhantomData,
sync::{Arc, Weak},
};
use tracing_core::{
callsite, span,
subscriber::{Interest, Subscriber},
Event, Metadata,
};
#[derive(Debug)]
pub struct Layer<L, S> {
inner: Arc<RwLock<L>>,
_s: PhantomData<fn(S)>,
}
#[derive(Debug)]
pub struct Handle<L, S> {
inner: Weak<RwLock<L>>,
_s: PhantomData<fn(S)>,
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
#[derive(Debug)]
enum ErrorKind {
SubscriberGone,
Poisoned,
}
impl<L, S> crate::Layer<S> for Layer<L, S>
where
L: crate::Layer<S> + 'static,
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
try_lock!(self.inner.write(), else return).on_layer(subscriber);
}
#[inline]
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
try_lock!(self.inner.read(), else return Interest::sometimes()).register_callsite(metadata)
}
#[inline]
fn enabled(&self, metadata: &Metadata<'_>, ctx: layer::Context<'_, S>) -> bool {
try_lock!(self.inner.read(), else return false).enabled(metadata, ctx)
}
#[inline]
fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_new_span(attrs, id, ctx)
}
#[inline]
fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_record(span, values, ctx)
}
#[inline]
fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_follows_from(span, follows, ctx)
}
#[inline]
fn on_event(&self, event: &Event<'_>, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_event(event, ctx)
}
#[inline]
fn on_enter(&self, id: &span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_enter(id, ctx)
}
#[inline]
fn on_exit(&self, id: &span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_exit(id, ctx)
}
#[inline]
fn on_close(&self, id: span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_close(id, ctx)
}
#[inline]
fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: layer::Context<'_, S>) {
try_lock!(self.inner.read()).on_id_change(old, new, ctx)
}
}
impl<L, S> Layer<L, S>
where
L: crate::Layer<S> + 'static,
S: Subscriber,
{
pub fn new(inner: L) -> (Self, Handle<L, S>) {
let this = Self {
inner: Arc::new(RwLock::new(inner)),
_s: PhantomData,
};
let handle = this.handle();
(this, handle)
}
pub fn handle(&self) -> Handle<L, S> {
Handle {
inner: Arc::downgrade(&self.inner),
_s: PhantomData,
}
}
}
impl<L, S> Handle<L, S>
where
L: crate::Layer<S> + 'static,
S: Subscriber,
{
pub fn reload(&self, new_layer: impl Into<L>) -> Result<(), Error> {
self.modify(|layer| {
*layer = new_layer.into();
})
}
pub fn modify(&self, f: impl FnOnce(&mut L)) -> Result<(), Error> {
let inner = self.inner.upgrade().ok_or(Error {
kind: ErrorKind::SubscriberGone,
})?;
let mut lock = try_lock!(inner.write(), else return Err(Error::poisoned()));
f(&mut *lock);
drop(lock);
callsite::rebuild_interest_cache();
Ok(())
}
pub fn clone_current(&self) -> Option<L>
where
L: Clone,
{
self.with_current(L::clone).ok()
}
pub fn with_current<T>(&self, f: impl FnOnce(&L) -> T) -> Result<T, Error> {
let inner = self.inner.upgrade().ok_or(Error {
kind: ErrorKind::SubscriberGone,
})?;
let inner = try_lock!(inner.read(), else return Err(Error::poisoned()));
Ok(f(&*inner))
}
}
impl<L, S> Clone for Handle<L, S> {
fn clone(&self) -> Self {
Handle {
inner: self.inner.clone(),
_s: PhantomData,
}
}
}
impl Error {
fn poisoned() -> Self {
Self {
kind: ErrorKind::Poisoned,
}
}
pub fn is_poisoned(&self) -> bool {
matches!(self.kind, ErrorKind::Poisoned)
}
pub fn is_dropped(&self) -> bool {
matches!(self.kind, ErrorKind::SubscriberGone)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self.kind {
ErrorKind::SubscriberGone => "subscriber no longer exists",
ErrorKind::Poisoned => "lock poisoned",
};
f.pad(msg)
}
}
impl error::Error for Error {}