-
Notifications
You must be signed in to change notification settings - Fork 2
/
archive.rs
173 lines (154 loc) · 5.46 KB
/
archive.rs
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
type SmartString = smartstring::SmartString<smartstring::LazyCompact>;
use chrono::prelude::*;
use indexmap::IndexMap;
use replace_with::replace_with_or_abort;
use std::sync::Arc;
/// A record of a tracing [event](https://docs.rs/tracing/0.1/tracing/index.html#events).
#[derive(Debug, Clone)]
pub struct Event {
pub(crate) meta: &'static tracing::Metadata<'static>,
pub(crate) timestamp: NaiveDateTime,
pub(crate) fields: FieldMap,
pub(crate) span: Option<Arc<Span>>,
}
/// A record of a tracing [span](https://docs.rs/tracing/0.1/tracing/index.html#spans).
#[derive(Debug, Clone)]
pub struct Span {
pub(crate) meta: &'static tracing::Metadata<'static>,
pub(crate) fields: FieldMap,
pub(crate) parent: Option<Arc<Span>>,
}
type FieldMap = IndexMap<&'static str, Field, ahash::RandomState>;
/// A field recorded on some tracing event/span.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Field {
I64(i64),
U64(u64),
Bool(bool),
Str(SmartString),
Error(SmartString),
Debug(SmartString),
Multiple(Vec<Field>),
}
impl Event {
/// The [`tracing::Metadata`] describing this event.
pub fn meta(&self) -> &'static tracing::Metadata<'static> {
self.meta
}
/// The time at which this event was fired.
pub fn timestamp(&self) -> NaiveDateTime {
self.timestamp
}
/// A recorded field on this event.
pub fn field(&self, name: &str) -> Option<&Field> {
self.fields.get(name)
}
/// All recorded fields on this event.
pub fn fields(&self) -> impl Iterator<Item = (&'static str, &Field)> + '_ {
self.fields.iter().map(|(&name, field)| (name, field))
}
/// The containing span, if any.
pub fn span(&self) -> Option<&Span> {
self.span.as_deref()
}
pub(crate) fn record_field(
&mut self,
field: &tracing::field::Field,
value: impl Fn() -> Field,
) {
self.fields
.entry(field.name())
.and_modify(|entry| {
replace_with_or_abort(entry, |field| match field {
Field::Multiple(mut fields) => {
fields.push(value());
Field::Multiple(fields)
}
field => Field::Multiple(vec![field, value()]),
})
})
.or_insert_with(value);
}
}
impl Span {
/// The [`tracing::Metadata`] describing this span.
pub fn meta(&self) -> &'static tracing::Metadata<'static> {
self.meta
}
/// A recorded field on this span.
pub fn field(&self, name: &str) -> Option<&Field> {
self.fields.get(name)
}
/// All recorded fields on this span.
pub fn fields(&self) -> impl Iterator<Item = (&'static str, &Field)> + '_ {
self.fields.iter().map(|(&name, field)| (name, field))
}
/// The containing span, if any.
pub fn parent(&self) -> Option<&Span> {
self.parent.as_deref()
}
pub(crate) fn record_field(
&mut self,
field: &tracing::field::Field,
value: impl Fn() -> Field,
) {
self.fields
.entry(field.name())
.and_modify(|entry| {
replace_with_or_abort(entry, |field| match field {
Field::Multiple(mut fields) => {
fields.push(value());
Field::Multiple(fields)
}
field => Field::Multiple(vec![field, value()]),
})
})
.or_insert_with(value);
}
}
impl Field {
/// The field, as would be presented to [`tracing::field::Visit::record_debug`].
///
/// If the field was recorded multiple times, `record_debug` is called multiple times.
pub fn with_debug<'a, R>(
&'a self,
record_debug: impl 'a + FnMut(&dyn std::fmt::Debug) -> R,
) -> impl Iterator<Item = R> + 'a {
struct WithDebug<'a, F>(&'a [Field], Vec<&'a [Field]>, F);
impl<F, R> Iterator for WithDebug<'_, F>
where
F: FnMut(&dyn std::fmt::Debug) -> R,
{
type Item = R;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
self.0 = self.1.pop().unwrap_or(&[]);
}
match self.0 {
[] => None,
[head, tail @ ..] => {
let res = match head {
Field::I64(value) => self.2(value),
Field::U64(value) => self.2(value),
Field::Bool(value) => self.2(value),
Field::Str(value) => self.2(&&**value as &&str),
Field::Error(value) => self.2(&format_args!("{}", value)),
Field::Debug(value) => self.2(&format_args!("{}", value)),
Field::Multiple(values) => {
if tail.is_empty() {
self.0 = &**values;
} else if !values.is_empty() {
self.1.push(&**values);
}
return self.next();
}
};
self.0 = tail;
Some(res)
}
}
}
}
WithDebug(std::slice::from_ref(self), vec![], record_debug)
}
}