kerf 0.1.2

Simple tokio-based trace event collector
Documentation
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use tracing::field::Field;

use std::fmt::Write as _;

pub use tracing::Level as TracingLevel;

use crate::Level;

pub type TraceEventId = u64;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Event {
    pub id: TraceEventId,
    pub timestamp: chrono::DateTime<chrono::Local>,
    pub level: Level,
    pub target: String,
    pub name: String,
    pub module_path: Option<String>,
    pub file: Option<String>,
    pub line: Option<u32>,
    pub message: String,
    pub fields: HashMap<String, String>,
    pub span_name: Option<String>,
    pub span_hierarchy: Option<String>,
}

pub type ArcEvent = Arc<Event>;

impl Event {
    pub fn new(id: u64, tracing_event: &tracing::Event<'_>) -> Self {
        let meta = tracing_event.metadata();

        // Enhanced visitor to capture both formatted event and fields
        let mut visitor = EventMessageVisitor::default();
        tracing_event.record(&mut visitor);

        Event {
            id,
            timestamp: chrono::Local::now(),
            level: Level::from(*meta.level()),
            target: meta.target().to_string(),
            name: meta.name().to_string(),
            module_path: meta.module_path().map(|s| s.to_string()),
            file: meta.file().map(|s| s.to_string()),
            line: meta.line(),
            message: visitor.message,
            fields: visitor.fields,
            span_name: None,      // Will be set by subscriber
            span_hierarchy: None, // Will be set by subscriber
        }
    }

    pub fn into_shared(self) -> Arc<Self> {
        Arc::new(self)
    }

    pub fn ref_count(self: &Arc<Self>) -> usize {
        Arc::strong_count(self)
    }

    pub fn ptr_eq(self: &Arc<Self>, other: &Arc<Self>) -> bool {
        Arc::ptr_eq(self, other)
    }

    pub fn format(&self) -> String {
        let span_info = if let Some(span_name) = &self.span_name {
            format!("[span:{span_name}] ")
        } else {
            String::new()
        };

        format!(
            "{}  {}  {}  {} {}",
            self.timestamp.format("%Y-%m-%d %H:%M:%S"),
            self.level,
            self.message,
            if let Some(module) = &self.module_path {
                format!("[{module}] ")
            } else {
                String::new()
            },
            span_info,
        )
    }

    pub fn format_with_file(&self) -> String {
        let mut formatted = self.format();
        formatted.push_str(&format!(
            " {}:{}",
            self.file.as_deref().unwrap_or("unknown"),
            self.line.unwrap_or(0)
        ));
        formatted
    }

    pub fn format_with_span_hierarchy(&self) -> String {
        let mut formatted = self.format();

        if let Some(hierarchy) = &self.span_hierarchy {
            formatted.push_str(&format!(" [Span Hierarchy: {hierarchy}]"));
        }

        formatted
    }

    pub fn format_with_fields(&self) -> String {
        let mut formatted = self.format();

        if !self.fields.is_empty() {
            formatted.push_str(" {");
            let fields_str = self
                .fields
                .iter()
                .map(|(k, v)| format!("{k}={v}"))
                .collect::<Vec<_>>()
                .join(", ");
            formatted.push_str(&fields_str);
            formatted.push('}');
        }

        formatted
    }

    pub fn format_full(&self) -> String {
        let mut formatted = self.format_with_file();

        if let Some(hierarchy) = &self.span_hierarchy {
            formatted.push_str(&format!(" [Span Hierarchy: {hierarchy}]"));
        }

        if !self.fields.is_empty() {
            formatted.push_str(" {");
            let fields_str = self
                .fields
                .iter()
                .map(|(k, v)| format!("{k}={v}"))
                .collect::<Vec<_>>()
                .join(", ");
            formatted.push_str(&fields_str);
            formatted.push('}');
        }

        formatted
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.format())
    }
}

#[derive(Default)]
pub struct EventMessageVisitor {
    pub fields: HashMap<String, String>,
    pub message: String,
}

const MESSAGE_META: &str = "message";

impl tracing::field::Visit for EventMessageVisitor {
    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        // Special handling for the "message" field which contains the formatted event
        if field.name() == MESSAGE_META {
            if self.message.is_empty() {
                let _ = write!(self.message, "{value:?}");
            }
        } else {
            let mut buffer = String::new();
            let _ = write!(buffer, "{value:?}");
            self.fields.insert(field.name().to_string(), buffer);
        }
    }

    fn record_str(&mut self, field: &Field, value: &str) {
        if field.name() == MESSAGE_META {
            if self.message.is_empty() {
                self.message = value.to_string();
            }
        } else {
            self.fields
                .insert(field.name().to_string(), value.to_string());
        }
    }

    fn record_i64(&mut self, field: &Field, value: i64) {
        self.fields
            .insert(field.name().to_string(), value.to_string());
    }

    fn record_u64(&mut self, field: &Field, value: u64) {
        self.fields
            .insert(field.name().to_string(), value.to_string());
    }

    fn record_bool(&mut self, field: &Field, value: bool) {
        self.fields
            .insert(field.name().to_string(), value.to_string());
    }

    fn record_f64(&mut self, field: &Field, value: f64) {
        self.fields
            .insert(field.name().to_string(), value.to_string());
    }
}

// Add this to your trace_event.rs file

impl Event {
    /// Format with colors using ANSI escape codes
    pub fn format_colored(&self) -> String {
        let span_info = if let Some(span_name) = &self.span_name {
            format!(
                "[span:{}] ",
                ansi_color(span_name, AnsiColor::BrightMagenta)
            )
        } else {
            String::new()
        };

        const HOUR: u8 = 120;
        const MINUTE: u8 = 150;
        const SEC: u8 = 180;

        const HOUR_FG: AnsiColor = AnsiColor::Rgb(HOUR, HOUR, HOUR);
        const MINUTE_FG: AnsiColor = AnsiColor::Rgb(MINUTE, MINUTE, MINUTE);
        const SEC_FG: AnsiColor = AnsiColor::Rgb(SEC, SEC, SEC);

        let hour = ansi_color(&self.timestamp.format("%H").to_string(), HOUR_FG);
        let min = ansi_color(&self.timestamp.format("%M").to_string(), MINUTE_FG);
        let sec = ansi_color(&self.timestamp.format("%S").to_string(), SEC_FG);
        let level = self.format_level_colored();
        let message = ansi_color(&self.message, AnsiColor::White);
        format!("{hour}{min}{sec}  {level}  {message}  {span_info}",)
    }

    /// Format with colors and file info
    pub fn format_colored_with_file(&self) -> String {
        let mut formatted = self.format_colored();
        formatted.push_str(&format!(
            " {}",
            ansi_color(
                &format!(
                    "({}:{})",
                    self.file.as_deref().unwrap_or("unknown"),
                    self.line.unwrap_or(0)
                ),
                AnsiColor::Rgb(90, 90, 90)
            )
        ));
        formatted
    }

    /// Format with colors, file info, and span hierarchy
    pub fn format_colored_with_span_hierarchy(&self) -> String {
        let mut formatted = self.format_colored_with_file();

        if let Some(hierarchy) = &self.span_hierarchy {
            formatted.push_str(&format!(
                " [Span Hierarchy: {}]",
                ansi_color(hierarchy, AnsiColor::BrightMagenta)
            ));
        }

        formatted
    }

    /// Format with colors and fields
    pub fn format_colored_with_fields(&self) -> String {
        let mut formatted = self.format_colored();

        if !self.fields.is_empty() {
            formatted.push_str(" {");
            let fields_str = self
                .fields
                .iter()
                .map(|(k, v)| {
                    format!(
                        "{}={}",
                        ansi_color(k, AnsiColor::Cyan),
                        ansi_color(v, AnsiColor::BrightWhite)
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            formatted.push_str(&fields_str);
            formatted.push('}');
        }

        formatted
    }

    /// Full colored format with all information
    pub fn format_colored_full(&self) -> String {
        let mut formatted = self.format_colored_with_file();

        if let Some(hierarchy) = &self.span_hierarchy {
            formatted.push_str(&format!(
                " [Span Hierarchy: {}]",
                ansi_color(hierarchy, AnsiColor::BrightMagenta)
            ));
        }

        if !self.fields.is_empty() {
            formatted.push_str(" {");
            let fields_str = self
                .fields
                .iter()
                .map(|(k, v)| {
                    format!(
                        "{}={}",
                        ansi_color(k, AnsiColor::Cyan),
                        ansi_color(v, AnsiColor::BrightWhite)
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            formatted.push_str(&fields_str);
            formatted.push('}');
        }

        formatted
    }

    /// Format just the level with appropriate colors
    fn format_level_colored(&self) -> String {
        let level_str = match self.level.0 {
            tracing::Level::WARN | tracing::Level::INFO => format!(" {}", self.level),
            _ => self.level.to_string(),
        };

        let color = match self.level.0 {
            tracing::Level::INFO => AnsiColor::Green,
            tracing::Level::DEBUG => AnsiColor::Cyan,
            tracing::Level::WARN => AnsiColor::Yellow,
            tracing::Level::ERROR => AnsiColor::Red,
            tracing::Level::TRACE => AnsiColor::Rgb(128, 128, 128),
        };

        ansi_color(&level_str, color)
    }

    /// Advanced colored format with multiline support
    pub fn format_colored_multiline(&self) -> Vec<String> {
        let mut result = Vec::new();
        let message_parts: Vec<&str> = self.message.split('\n').collect();

        // Create timestamp with colors matching your theme
        let timestamp_colored = format!(
            "{}{}{}",
            ansi_color(
                &self.timestamp.format("%H").to_string(),
                AnsiColor::Rgb(120, 120, 120)
            ),
            ansi_color(
                &self.timestamp.format("%M").to_string(),
                AnsiColor::Rgb(150, 150, 150)
            ),
            ansi_color(
                &self.timestamp.format("%S").to_string(),
                AnsiColor::Rgb(180, 180, 180)
            )
        );

        // Create level with padding and colors
        let level_colored = self.format_level_colored();

        // Generate file/line info if available
        let file_line_info = self.file.as_ref().and_then(|file| {
            self.line
                .as_ref()
                .map(|line| ansi_color(&format!(" ({file}:{line})"), AnsiColor::Rgb(90, 90, 90)))
        });

        // Create the header prefix
        let header_prefix = format!("{timestamp_colored} {level_colored} ");

        if message_parts.len() == 1 {
            // Single line message
            let mut line = format!(
                "{}{}",
                header_prefix,
                ansi_color(message_parts[0], AnsiColor::White)
            );
            if let Some(file_info) = file_line_info {
                line.push_str(&file_info);
            }
            result.push(line);
        } else {
            // Multiline message
            const INDENT_SIZE: usize = 13;
            let indent = " ".repeat(INDENT_SIZE);

            // First line
            result.push(format!(
                "{}{}",
                header_prefix,
                ansi_color(message_parts[0], AnsiColor::White)
            ));

            // Middle lines
            for &line in message_parts.iter().skip(1).take(message_parts.len() - 2) {
                result.push(format!("{}{}", indent, ansi_color(line, AnsiColor::White)));
            }

            // Last line with file info
            if let Some(last_part) = message_parts.last().filter(|_| message_parts.len() > 1) {
                let mut last_line =
                    format!("{}{}", indent, ansi_color(last_part, AnsiColor::White));
                if let Some(file_info) = file_line_info {
                    last_line.push_str(&file_info);
                }
                result.push(last_line);
            }
        }

        // Add span hierarchy if present
        if let Some(hierarchy) = &self.span_hierarchy {
            if let Some(last_line) = result.last_mut() {
                last_line.push_str(&format!(
                    " [Span Hierarchy: {}]",
                    ansi_color(hierarchy, AnsiColor::BrightMagenta)
                ));
            }
        }

        // Add fields if present
        if !self.fields.is_empty() {
            if let Some(last_line) = result.last_mut() {
                last_line.push_str(" {");
                let fields_str = self
                    .fields
                    .iter()
                    .map(|(k, v)| {
                        format!(
                            "{}={}",
                            ansi_color(k, AnsiColor::Cyan),
                            ansi_color(v, AnsiColor::BrightWhite)
                        )
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                last_line.push_str(&fields_str);
                last_line.push('}');
            }
        }

        result
    }
}

// ANSI color support - add this to the same file or create a separate module

#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
enum AnsiColor {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    BrightBlack,
    BrightRed,
    BrightGreen,
    BrightYellow,
    BrightBlue,
    BrightMagenta,
    BrightCyan,
    BrightWhite,
    Rgb(u8, u8, u8),
}

impl AnsiColor {
    fn to_ansi_code(self) -> String {
        match self {
            AnsiColor::Black => "30".to_string(),
            AnsiColor::Red => "31".to_string(),
            AnsiColor::Green => "32".to_string(),
            AnsiColor::Yellow => "33".to_string(),
            AnsiColor::Blue => "34".to_string(),
            AnsiColor::Magenta => "35".to_string(),
            AnsiColor::Cyan => "36".to_string(),
            AnsiColor::White => "37".to_string(),
            AnsiColor::BrightBlack => "90".to_string(),
            AnsiColor::BrightRed => "91".to_string(),
            AnsiColor::BrightGreen => "92".to_string(),
            AnsiColor::BrightYellow => "93".to_string(),
            AnsiColor::BrightBlue => "94".to_string(),
            AnsiColor::BrightMagenta => "95".to_string(),
            AnsiColor::BrightCyan => "96".to_string(),
            AnsiColor::BrightWhite => "97".to_string(),
            AnsiColor::Rgb(r, g, b) => format!("38;2;{r};{g};{b}"),
        }
    }
}

fn ansi_color(text: &str, color: AnsiColor) -> String {
    let code = color.to_ansi_code();
    format!("\x1b[{code}m{text}\x1b[0m")
}