| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
pub(crate) struct CapturedEvent { |
| 7 |
|
| 8 |
pub(crate) message: Option<String>, |
| 9 |
|
| 10 |
pub(crate) fields: Vec<(String, String)>, |
| 11 |
} |
| 12 |
|
| 13 |
impl CapturedEvent { |
| 14 |
|
| 15 |
pub(crate) fn field(&self, name: &str) -> Option<&str> { |
| 16 |
self.fields |
| 17 |
.iter() |
| 18 |
.find(|(k, _)| k == name) |
| 19 |
.map(|(_, v)| v.as_str()) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#[derive(Default)] |
| 29 |
struct EventVisitor { |
| 30 |
message: Option<String>, |
| 31 |
fields: Vec<(String, String)>, |
| 32 |
} |
| 33 |
|
| 34 |
impl EventVisitor { |
| 35 |
fn put(&mut self, name: &str, value: String) { |
| 36 |
if name == "message" { |
| 37 |
self.message = Some(value); |
| 38 |
} else { |
| 39 |
self.fields.push((name.to_string(), value)); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl tracing::field::Visit for EventVisitor { |
| 45 |
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { |
| 46 |
self.put(field.name(), format!("{value:?}")); |
| 47 |
} |
| 48 |
|
| 49 |
fn record_str(&mut self, field: &tracing::field::Field, value: &str) { |
| 50 |
self.put(field.name(), value.to_string()); |
| 51 |
} |
| 52 |
|
| 53 |
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) { |
| 54 |
self.put(field.name(), value.to_string()); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
struct Sink { |
| 60 |
thread: std::thread::ThreadId, |
| 61 |
events: Vec<CapturedEvent>, |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static SINK: std::sync::Mutex<Option<Sink>> = std::sync::Mutex::new(None); |
| 66 |
|
| 67 |
|
| 68 |
static CAPTURING: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct Recorder; |
| 84 |
|
| 85 |
impl tracing::Subscriber for Recorder { |
| 86 |
fn register_callsite( |
| 87 |
&self, |
| 88 |
_: &'static tracing::Metadata<'static>, |
| 89 |
) -> tracing::subscriber::Interest { |
| 90 |
|
| 91 |
|
| 92 |
tracing::subscriber::Interest::sometimes() |
| 93 |
} |
| 94 |
|
| 95 |
fn enabled(&self, _: &tracing::Metadata<'_>) -> bool { |
| 96 |
true |
| 97 |
} |
| 98 |
|
| 99 |
fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id { |
| 100 |
tracing::span::Id::from_u64(1) |
| 101 |
} |
| 102 |
|
| 103 |
fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {} |
| 104 |
|
| 105 |
fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {} |
| 106 |
|
| 107 |
fn event(&self, event: &tracing::Event<'_>) { |
| 108 |
let mut sink = SINK |
| 109 |
.lock() |
| 110 |
.unwrap_or_else(std::sync::PoisonError::into_inner); |
| 111 |
let Some(sink) = sink.as_mut() else { return }; |
| 112 |
if sink.thread != std::thread::current().id() { |
| 113 |
return; |
| 114 |
} |
| 115 |
let mut visitor = EventVisitor::default(); |
| 116 |
event.record(&mut visitor); |
| 117 |
sink.events.push(CapturedEvent { |
| 118 |
message: visitor.message, |
| 119 |
fields: visitor.fields, |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
fn enter(&self, _: &tracing::span::Id) {} |
| 124 |
|
| 125 |
fn exit(&self, _: &tracing::span::Id) {} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
pub(crate) fn events_from(f: impl FnOnce()) -> Vec<CapturedEvent> { |
| 133 |
static INSTALL: std::sync::Once = std::sync::Once::new(); |
| 134 |
INSTALL.call_once(|| { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
let _ = tracing::subscriber::set_global_default(Recorder); |
| 139 |
}); |
| 140 |
|
| 141 |
let _capturing = CAPTURING |
| 142 |
.lock() |
| 143 |
.unwrap_or_else(std::sync::PoisonError::into_inner); |
| 144 |
*SINK |
| 145 |
.lock() |
| 146 |
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Sink { |
| 147 |
thread: std::thread::current().id(), |
| 148 |
events: Vec::new(), |
| 149 |
}); |
| 150 |
f(); |
| 151 |
SINK.lock() |
| 152 |
.unwrap_or_else(std::sync::PoisonError::into_inner) |
| 153 |
.take() |
| 154 |
.expect("the sink this call installed") |
| 155 |
.events |
| 156 |
} |
| 157 |
|