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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use ansi_term::Color;
use std::{
fmt::{self, Write as _},
io,
};
use tracing_core::{
field::{Field, Visit},
Level,
};
pub(crate) const LINE_VERT: &str = "│";
const LINE_HORIZ: &str = "─";
pub(crate) const LINE_BRANCH: &str = "├";
pub(crate) const LINE_CLOSE: &str = "┘";
pub(crate) const LINE_OPEN: &str = "┐";
#[derive(Copy, Clone)]
pub(crate) enum SpanMode {
PreOpen,
Open { verbose: bool },
Close { verbose: bool },
PostClose,
Event,
}
#[derive(Debug)]
pub struct Config {
pub ansi: bool,
pub indent_lines: bool,
pub indent_amount: usize,
pub targets: bool,
pub render_thread_ids: bool,
pub render_thread_names: bool,
pub wraparound: usize,
pub verbose_entry: bool,
pub verbose_exit: bool,
pub bracketed_fields: bool,
}
impl Config {
pub fn with_ansi(self, ansi: bool) -> Self {
Self { ansi, ..self }
}
pub fn with_indent_lines(self, indent_lines: bool) -> Self {
Self {
indent_lines,
..self
}
}
pub fn with_targets(self, targets: bool) -> Self {
Self { targets, ..self }
}
pub fn with_thread_ids(self, render_thread_ids: bool) -> Self {
Self {
render_thread_ids,
..self
}
}
pub fn with_thread_names(self, render_thread_names: bool) -> Self {
Self {
render_thread_names,
..self
}
}
pub fn with_wraparound(self, wraparound: usize) -> Self {
Self { wraparound, ..self }
}
pub fn with_verbose_entry(self, verbose_entry: bool) -> Self {
Self {
verbose_entry,
..self
}
}
pub fn with_verbose_exit(self, verbose_exit: bool) -> Self {
Self {
verbose_exit,
..self
}
}
pub fn with_bracketed_fields(self, bracketed_fields: bool) -> Self {
Self {
bracketed_fields,
..self
}
}
pub(crate) fn prefix(&self) -> String {
let mut buf = String::new();
if self.render_thread_ids {
write!(buf, "{:?}", std::thread::current().id()).unwrap();
if buf.ends_with(')') {
buf.truncate(buf.len() - 1);
}
if buf.starts_with("ThreadId(") {
buf.drain(0.."ThreadId(".len());
}
}
if self.render_thread_names {
if let Some(name) = std::thread::current().name() {
if self.render_thread_ids {
buf.push(':');
}
buf.push_str(name);
}
}
buf
}
}
impl Default for Config {
fn default() -> Self {
Self {
ansi: true,
indent_lines: false,
indent_amount: 2,
targets: false,
render_thread_ids: false,
render_thread_names: false,
wraparound: usize::max_value(),
verbose_entry: false,
verbose_exit: false,
bracketed_fields: false,
}
}
}
#[derive(Debug)]
pub struct Buffers {
pub current_buf: String,
pub indent_buf: String,
}
impl Buffers {
pub fn new() -> Self {
Self {
current_buf: String::new(),
indent_buf: String::new(),
}
}
pub fn flush_current_buf(&mut self, mut writer: impl io::Write) {
write!(writer, "{}", &self.current_buf).unwrap();
self.current_buf.clear();
}
pub fn flush_indent_buf(&mut self) {
self.current_buf.push_str(&self.indent_buf);
self.indent_buf.clear();
}
pub(crate) fn indent_current(&mut self, indent: usize, config: &Config, style: SpanMode) {
self.current_buf.push('\n');
let prefix = config.prefix();
if config.indent_lines {
match style {
SpanMode::Close { .. } | SpanMode::PostClose => {
if indent > 0 && (indent + 1) % config.wraparound == 0 {
self.indent_buf.push_str(&prefix);
for _ in 0..(indent % config.wraparound * config.indent_amount) {
self.indent_buf.push_str(LINE_HORIZ);
}
self.indent_buf.push_str(LINE_OPEN);
self.indent_buf.push('\n');
}
}
_ => {}
}
}
indent_block(
&mut self.current_buf,
&mut self.indent_buf,
indent % config.wraparound,
config.indent_amount,
config.indent_lines,
&prefix,
style,
);
self.current_buf.clear();
self.flush_indent_buf();
if config.indent_lines {
match style {
SpanMode::PreOpen | SpanMode::Open { .. } => {
if indent > 0 && (indent + 1) % config.wraparound == 0 {
self.current_buf.push_str(&prefix);
for _ in 0..(indent % config.wraparound * config.indent_amount) {
self.current_buf.push_str(LINE_HORIZ);
}
self.current_buf.push_str(LINE_CLOSE);
self.current_buf.push('\n');
}
}
_ => {}
}
}
}
}
pub struct FmtEvent<'a> {
pub bufs: &'a mut Buffers,
pub comma: bool,
}
impl<'a> Visit for FmtEvent<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
let buf = &mut self.bufs.current_buf;
let comma = if self.comma { "," } else { "" };
match field.name() {
"message" => {
write!(buf, "{} {:?}", comma, value).unwrap();
self.comma = true;
}
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => {}
name => {
write!(buf, "{} {}={:?}", comma, name, value).unwrap();
self.comma = true;
}
}
}
}
pub struct ColorLevel<'a>(pub &'a Level);
impl<'a> fmt::Display for ColorLevel<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self.0 {
Level::TRACE => Color::Purple.bold().paint("TRACE"),
Level::DEBUG => Color::Blue.bold().paint("DEBUG"),
Level::INFO => Color::Green.bold().paint(" INFO"),
Level::WARN => Color::RGB(252, 234, 160).bold().paint(" WARN"),
Level::ERROR => Color::Red.bold().paint("ERROR"),
}
.fmt(f)
}
}
fn indent_block_with_lines(
lines: &[&str],
buf: &mut String,
indent: usize,
indent_amount: usize,
prefix: &str,
style: SpanMode,
) {
let indent_spaces = indent * indent_amount;
if lines.is_empty() {
return;
} else if indent_spaces == 0 {
for line in lines {
buf.push_str(prefix);
if indent == 0 {
match style {
SpanMode::Open { .. } => buf.push_str(LINE_OPEN),
SpanMode::Close { .. } => buf.push_str(LINE_CLOSE),
SpanMode::PreOpen | SpanMode::PostClose => {}
SpanMode::Event => {}
}
}
buf.push_str(line);
buf.push('\n');
}
return;
}
let mut s = String::with_capacity(indent_spaces + prefix.len());
s.push_str(prefix);
for i in 0..(indent_spaces - indent_amount) {
if i % indent_amount == 0 {
s.push_str(LINE_VERT);
} else {
s.push(' ');
}
}
buf.push_str(&s);
match style {
SpanMode::PreOpen => {
buf.push_str(LINE_BRANCH);
for _ in 1..(indent_amount / 2) {
buf.push_str(LINE_HORIZ);
}
buf.push_str(LINE_OPEN);
}
SpanMode::Open { verbose: false } => {
buf.push_str(LINE_BRANCH);
for _ in 1..indent_amount {
buf.push_str(LINE_HORIZ);
}
buf.push_str(LINE_OPEN);
}
SpanMode::Open { verbose: true } => {
buf.push_str(LINE_VERT);
for _ in 1..(indent_amount / 2) {
buf.push(' ');
}
if indent_amount > 1 {
buf.push('└');
}
for _ in (indent_amount / 2)..(indent_amount - 1) {
buf.push_str(LINE_HORIZ);
}
if indent_amount > 1 {
buf.push_str(LINE_OPEN);
} else {
buf.push_str(LINE_VERT);
}
}
SpanMode::Close { verbose: false } => {
buf.push_str(LINE_BRANCH);
for _ in 1..indent_amount {
buf.push_str(LINE_HORIZ);
}
buf.push_str(LINE_CLOSE);
}
SpanMode::Close { verbose: true } => {
buf.push_str(LINE_VERT);
for _ in 1..(indent_amount / 2) {
buf.push(' ');
}
if indent_amount > 1 {
buf.push('┌');
}
for _ in (indent_amount / 2)..(indent_amount - 1) {
buf.push_str(LINE_HORIZ);
}
if indent_amount > 1 {
buf.push_str(LINE_CLOSE);
} else {
buf.push_str(LINE_VERT);
}
}
SpanMode::PostClose => {
buf.push_str(LINE_BRANCH);
for _ in 1..(indent_amount / 2) {
buf.push_str(LINE_HORIZ);
}
buf.push_str(LINE_CLOSE);
}
SpanMode::Event => {
buf.push_str(LINE_BRANCH);
for _ in 0..(indent_amount - 1) {
buf.push_str(LINE_HORIZ);
}
}
}
buf.push_str(lines[0]);
buf.push('\n');
for i in 0..indent_amount {
if i % indent_amount == 0 {
s.push_str(LINE_VERT);
} else {
s.push(' ');
}
}
for line in &lines[1..] {
buf.push_str(&s);
buf.push_str(line);
buf.push('\n');
}
}
fn indent_block(
block: &mut String,
buf: &mut String,
indent: usize,
indent_amount: usize,
indent_lines: bool,
prefix: &str,
style: SpanMode,
) {
let lines: Vec<&str> = block.lines().collect();
let indent_spaces = indent * indent_amount;
buf.reserve(block.len() + (lines.len() * indent_spaces));
if indent_lines {
indent_block_with_lines(&lines, buf, indent, indent_amount, prefix, style);
} else {
let indent_str = String::from(" ").repeat(indent_spaces);
for line in lines {
buf.push_str(prefix);
buf.push_str(&indent_str);
buf.push_str(line);
buf.push('\n');
}
}
}