rlg 0.0.8

A Rust library that implements application-level logging with a simple, readable output format. Features include log rotation, network logging, and support for multiple structured formats like JSON, CEF, and more.
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
// tui.rs
// Copyright © 2024-2026 RustLogs (RLG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! Opt-in terminal dashboard for live observability metrics.
//!
//! Set `RLG_TUI=1` to spawn a background render thread that paints a
//! non-clobbering sparkline dashboard at ~60 FPS. Enable the `tui` feature
//! for automatic terminal size detection; otherwise falls back to 80x24.

use std::io::Write;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

/// Number of throughput samples in the sparkline ring buffer (one per tick).
const SPARKLINE_RING_SIZE: usize = 60;

/// TUI render interval in milliseconds (~60 FPS).
const TUI_TICK_INTERVAL_MS: u64 = 16;

/// Default terminal height when detection fails.
const DEFAULT_TERMINAL_HEIGHT: u16 = 24;

/// Default terminal width when detection fails.
const DEFAULT_TERMINAL_WIDTH: u16 = 80;

/// Width of level-distribution bar charts (in block characters).
const LEVEL_BAR_WIDTH: usize = 10;

#[cfg(all(not(windows), feature = "tui"))]
/// Returns the terminal height for the given handle, or 24 as fallback.
///
/// # Panics
///
/// This function does not panic.
#[must_use]
pub fn get_terminal_height_of(handle: &impl std::os::fd::AsFd) -> u16 {
    terminal_size::terminal_size_of(handle).map_or(
        DEFAULT_TERMINAL_HEIGHT,
        |(_, terminal_size::Height(h))| h,
    )
}

#[cfg(feature = "tui")]
fn get_terminal_height() -> u16 {
    terminal_size::terminal_size().map_or(
        DEFAULT_TERMINAL_HEIGHT,
        |(_, terminal_size::Height(h))| h,
    )
}

#[cfg(not(feature = "tui"))]
fn get_terminal_height() -> u16 {
    DEFAULT_TERMINAL_HEIGHT
}

/// Atomic counters for the TUI dashboard. Cache-line aligned (`repr(align(64))`).
#[repr(align(64))]
#[derive(Debug, Default)]
pub struct TuiMetrics {
    /// Total number of log events ingested.
    pub total_events: AtomicUsize,
    /// Number of error/fatal events.
    pub error_count: AtomicUsize,
    /// Number of active spans (OpenTelemetry style).
    pub active_spans: AtomicUsize,
    /// Calculated events per second.
    pub throughput: AtomicUsize,
    /// Peak throughput (events per second).
    pub peak_throughput: AtomicUsize,
    /// Engine start time (epoch seconds).
    pub start_epoch_secs: AtomicUsize,

    // Per-level counters
    /// TRACE-level event count.
    pub level_trace: AtomicUsize,
    /// DEBUG-level event count.
    pub level_debug: AtomicUsize,
    /// INFO-level event count.
    pub level_info: AtomicUsize,
    /// WARN-level event count.
    pub level_warn: AtomicUsize,
    /// ERROR-level event count.
    pub level_error: AtomicUsize,
    /// FATAL-level event count.
    pub level_fatal: AtomicUsize,
    /// CRITICAL-level event count.
    pub level_critical: AtomicUsize,
    /// Number of events dropped due to full ring buffer.
    pub dropped_events: AtomicUsize,

    // Per-format counters
    /// CLF format count.
    pub fmt_clf: AtomicUsize,
    /// JSON format count.
    pub fmt_json: AtomicUsize,
    /// CEF format count.
    pub fmt_cef: AtomicUsize,
    /// ELF format count.
    pub fmt_elf: AtomicUsize,
    /// W3C format count.
    pub fmt_w3c: AtomicUsize,
    /// GELF format count.
    pub fmt_gelf: AtomicUsize,
    /// Apache Access Log format count.
    pub fmt_apache: AtomicUsize,
    /// Logstash format count.
    pub fmt_logstash: AtomicUsize,
    /// Log4j XML format count.
    pub fmt_log4j: AtomicUsize,
    /// NDJSON format count.
    pub fmt_ndjson: AtomicUsize,
    /// MCP format count.
    pub fmt_mcp: AtomicUsize,
    /// OTLP format count.
    pub fmt_otlp: AtomicUsize,
    /// Logfmt format count.
    pub fmt_logfmt: AtomicUsize,
    /// ECS format count.
    pub fmt_ecs: AtomicUsize,
}

impl TuiMetrics {
    /// Increments the total event count.
    pub fn inc_events(&self) {
        self.total_events.fetch_add(1, Ordering::Relaxed);
    }

    /// Increments the error count.
    pub fn inc_errors(&self) {
        self.error_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Increments active spans.
    pub fn inc_spans(&self) {
        self.active_spans.fetch_add(1, Ordering::Relaxed);
    }

    /// Decrements active spans.
    pub fn dec_spans(&self) {
        self.active_spans.fetch_sub(1, Ordering::Relaxed);
    }

    /// Increments the dropped event count.
    pub fn inc_dropped(&self) {
        self.dropped_events.fetch_add(1, Ordering::Relaxed);
    }

    /// Increments the counter for the given log level.
    pub fn inc_level(&self, level: crate::log_level::LogLevel) {
        use crate::log_level::LogLevel;
        match level {
            LogLevel::TRACE => {
                self.level_trace.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::DEBUG => {
                self.level_debug.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::INFO => {
                self.level_info.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::WARN => {
                self.level_warn.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::ERROR => {
                self.level_error.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::FATAL => {
                self.level_fatal.fetch_add(1, Ordering::Relaxed);
            }
            LogLevel::CRITICAL => {
                self.level_critical.fetch_add(1, Ordering::Relaxed);
            }
            _ => {}
        }
    }

    /// Increments the counter for the given log format.
    pub fn inc_format(&self, format: crate::log_format::LogFormat) {
        use crate::log_format::LogFormat;
        match format {
            LogFormat::CLF => {
                self.fmt_clf.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::JSON => {
                self.fmt_json.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::CEF => {
                self.fmt_cef.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::ELF => {
                self.fmt_elf.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::W3C => {
                self.fmt_w3c.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::GELF => {
                self.fmt_gelf.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::ApacheAccessLog => {
                self.fmt_apache.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::Logstash => {
                self.fmt_logstash.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::Log4jXML => {
                self.fmt_log4j.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::NDJSON => {
                self.fmt_ndjson.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::MCP => {
                self.fmt_mcp.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::OTLP => {
                self.fmt_otlp.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::Logfmt => {
                self.fmt_logfmt.fetch_add(1, Ordering::Relaxed);
            }
            LogFormat::ECS => {
                self.fmt_ecs.fetch_add(1, Ordering::Relaxed);
            }
        }
    }
}

/// Returns the terminal width, or `DEFAULT_TERMINAL_WIDTH` as fallback.
#[cfg(feature = "tui")]
fn get_terminal_width() -> u16 {
    terminal_size::terminal_size().map_or(
        DEFAULT_TERMINAL_WIDTH,
        |(terminal_size::Width(w), _)| w,
    )
}

/// Returns the default terminal width when the `tui` feature is disabled.
#[cfg(not(feature = "tui"))]
fn get_terminal_width() -> u16 {
    DEFAULT_TERMINAL_WIDTH
}

/// Sparkline characters indexed by intensity (0..=7).
const SPARK_CHARS: [char; 8] = [
    '\u{2581}', '\u{2582}', '\u{2583}', '\u{2584}', '\u{2585}',
    '\u{2586}', '\u{2587}', '\u{2588}',
];

/// Renders a sparkline string from a circular buffer of throughput samples.
fn render_sparkline(
    ring: &[usize; SPARKLINE_RING_SIZE],
    cursor: usize,
) -> String {
    let max_val = ring.iter().copied().max().unwrap_or(1).max(1);
    let mut out = String::with_capacity(SPARKLINE_RING_SIZE);
    for i in 0..SPARKLINE_RING_SIZE {
        let idx = (cursor + i) % SPARKLINE_RING_SIZE;
        let scaled = (ring[idx] * 7) / max_val;
        out.push(SPARK_CHARS[scaled.min(7)]);
    }
    out
}

/// Renders a level bar: filled blocks + empty blocks, `LEVEL_BAR_WIDTH` chars wide.
fn render_level_bar(count: usize, total: usize) -> String {
    if total == 0 {
        return "\u{2591}".repeat(LEVEL_BAR_WIDTH);
    }
    let filled =
        ((count * LEVEL_BAR_WIDTH) / total).min(LEVEL_BAR_WIDTH);
    let mut bar = String::with_capacity(LEVEL_BAR_WIDTH * 3);
    for _ in 0..filled {
        bar.push('\u{2588}');
    }
    for _ in filled..LEVEL_BAR_WIDTH {
        bar.push('\u{2591}');
    }
    bar
}

/// Formats an uptime duration as HH:MM:SS.
fn format_uptime(secs: u64) -> String {
    let h = secs / 3600;
    let m = (secs % 3600) / 60;
    let s = secs % 60;
    format!("{h:02}:{m:02}:{s:02}")
}

/// Builds the format counts line from metrics.
pub fn build_fmt_line(metrics: &TuiMetrics) -> String {
    let fmt_counts: Vec<(&str, usize)> = [
        ("CLF", metrics.fmt_clf.load(Ordering::Relaxed)),
        ("JSON", metrics.fmt_json.load(Ordering::Relaxed)),
        ("CEF", metrics.fmt_cef.load(Ordering::Relaxed)),
        ("ELF", metrics.fmt_elf.load(Ordering::Relaxed)),
        ("W3C", metrics.fmt_w3c.load(Ordering::Relaxed)),
        ("GELF", metrics.fmt_gelf.load(Ordering::Relaxed)),
        ("Apache", metrics.fmt_apache.load(Ordering::Relaxed)),
        ("Logstash", metrics.fmt_logstash.load(Ordering::Relaxed)),
        ("Log4j", metrics.fmt_log4j.load(Ordering::Relaxed)),
        ("NDJSON", metrics.fmt_ndjson.load(Ordering::Relaxed)),
        ("MCP", metrics.fmt_mcp.load(Ordering::Relaxed)),
        ("OTLP", metrics.fmt_otlp.load(Ordering::Relaxed)),
        ("Logfmt", metrics.fmt_logfmt.load(Ordering::Relaxed)),
        ("ECS", metrics.fmt_ecs.load(Ordering::Relaxed)),
    ]
    .into_iter()
    .filter(|(_, c)| *c > 0)
    .collect();

    let mut fmt_line = String::new();
    for (i, (name, count)) in fmt_counts.iter().enumerate() {
        use std::fmt::Write as _;
        if i > 0 {
            fmt_line.push_str(" | ");
        }
        let _ = write!(fmt_line, "{name}: {count}");
    }
    if fmt_line.is_empty() {
        fmt_line.push_str("(none)");
    }
    fmt_line
}

/// Computes level bar rendering from metrics.
///
/// Returns `(info_bar, info_pct, error_bar, error_pct)`.
pub fn compute_level_bars(
    metrics: &TuiMetrics,
) -> (String, usize, String, usize) {
    let info_c = metrics.level_info.load(Ordering::Relaxed);
    let warn_c = metrics.level_warn.load(Ordering::Relaxed);
    let error_c = metrics.level_error.load(Ordering::Relaxed);
    let debug_c = metrics.level_debug.load(Ordering::Relaxed);
    let trace_c = metrics.level_trace.load(Ordering::Relaxed);

    let level_total = info_c + warn_c + error_c + debug_c + trace_c;

    let info_bar = render_level_bar(info_c, level_total.max(1));
    let info_pct = if level_total > 0 {
        (info_c * 100) / level_total
    } else {
        0
    };
    let error_bar = render_level_bar(error_c, level_total.max(1));
    let error_pct = if level_total > 0 {
        (error_c * 100) / level_total
    } else {
        0
    };

    (info_bar, info_pct, error_bar, error_pct)
}

/// Performs one tick of the TUI dashboard, returning the ANSI-formatted frame.
///
/// This function is extracted from the render loop for testability.
#[allow(clippy::cast_possible_truncation)]
pub fn render_tick(
    metrics: &TuiMetrics,
    last_total: &mut usize,
    sparkline_ring: &mut [usize; SPARKLINE_RING_SIZE],
    spark_cursor: &mut usize,
) -> String {
    let total = metrics.total_events.load(Ordering::Relaxed);
    let errors = metrics.error_count.load(Ordering::Relaxed);
    let spans = metrics.active_spans.load(Ordering::Relaxed);
    let dropped = metrics.dropped_events.load(Ordering::Relaxed);

    // Calculate throughput (events per ~16ms tick -> scale to second)
    let diff = total.saturating_sub(*last_total);
    *last_total = total;
    let tps = diff * 60;
    metrics.throughput.store(tps, Ordering::Relaxed);

    // Track peak throughput
    let _ = metrics.peak_throughput.fetch_max(tps, Ordering::Relaxed);
    let peak = metrics.peak_throughput.load(Ordering::Relaxed);

    // Update sparkline ring buffer
    sparkline_ring[*spark_cursor % SPARKLINE_RING_SIZE] = tps;
    *spark_cursor = spark_cursor.wrapping_add(1);

    // Uptime
    let now_secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as usize;
    let uptime_secs = now_secs.saturating_sub(
        metrics.start_epoch_secs.load(Ordering::Relaxed),
    );
    let uptime = format_uptime(uptime_secs as u64);

    // Level bars
    let (info_bar, info_pct, error_bar, error_pct) =
        compute_level_bars(metrics);

    // Format counts line
    let fmt_line = build_fmt_line(metrics);

    let sparkline = render_sparkline(
        sparkline_ring,
        *spark_cursor % SPARKLINE_RING_SIZE,
    );

    let total_fmt = format_with_commas(total);

    let width = get_terminal_width() as usize;
    let separator: String =
        "\u{2500}".repeat(width.min(SPARKLINE_RING_SIZE));

    let height = get_terminal_height();

    format!(
        "\x1b7\x1b[{height};1H\x1b[7A\x1b[J\
\x1b[38;5;33m[ \x1b[1;37mRLG Liquid Glass Dashboard \x1b[0;38;5;33m]\x1b[0m\n\
\x1b[1mErrors:\x1b[0m {errors} | \x1b[1mDropped:\x1b[0m {dropped} | \x1b[1mActive Spans:\x1b[0m {spans} | \x1b[1mThroughput:\x1b[0m {tps} ev/s\n\
\x1b[1mPeak:\x1b[0m {peak} ev/s | \x1b[1mUptime:\x1b[0m {uptime} | \x1b[1mTotal:\x1b[0m {total_fmt}\n\
\x1b[1mThroughput\x1b[0m {sparkline}\n\
\x1b[1mLevels:\x1b[0m {info_bar} {info_pct}% INFO  {error_bar} {error_pct}% ERROR\n\
\x1b[1mFormats:\x1b[0m {fmt_line}\n\
\x1b[38;5;239m{separator}\x1b[0m\x1b8"
    )
}

/// Spawns the background TUI renderer thread.
///
/// # Panics
///
/// This function panics if the TUI background thread fails to spawn.
pub fn spawn_tui_thread(
    metrics: Arc<TuiMetrics>,
    shutdown_flag: Arc<AtomicBool>,
) {
    // Record engine start time
    #[allow(clippy::cast_possible_truncation)]
    let start = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as usize;
    metrics.start_epoch_secs.store(start, Ordering::Relaxed);

    thread::Builder::new()
        .name("rlg-tui".into())
        .spawn(move || {
            let mut last_total: usize = 0;
            let mut sparkline_ring = [0_usize; SPARKLINE_RING_SIZE];
            let mut spark_cursor: usize = 0;

            loop {
                if shutdown_flag.load(Ordering::Relaxed) {
                    break;
                }

                thread::sleep(Duration::from_millis(
                    TUI_TICK_INTERVAL_MS,
                ));

                let frame = render_tick(
                    &metrics,
                    &mut last_total,
                    &mut sparkline_ring,
                    &mut spark_cursor,
                );

                let mut stdout = std::io::stdout();
                let _ = write!(stdout, "{frame}");
                let _ = stdout.flush();
            }

            // Clean up terminal state on exit
            let _ = write!(std::io::stdout(), "\x1b[r\x1b[J");
        })
        .expect("Failed to spawn TUI thread");
}

/// Formats an integer with comma separators.
fn format_with_commas(n: usize) -> String {
    let s = n.to_string();
    let mut result = String::with_capacity(s.len() + s.len() / 3);
    for (i, ch) in s.chars().enumerate() {
        if i > 0 && (s.len() - i).is_multiple_of(3) {
            result.push(',');
        }
        result.push(ch);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_with_commas_zero() {
        assert_eq!(format_with_commas(0), "0");
    }

    #[test]
    fn test_format_with_commas_small() {
        assert_eq!(format_with_commas(42), "42");
        assert_eq!(format_with_commas(999), "999");
    }

    #[test]
    fn test_format_with_commas_thousands() {
        assert_eq!(format_with_commas(1000), "1,000");
        assert_eq!(format_with_commas(1_234), "1,234");
        assert_eq!(format_with_commas(999_999), "999,999");
    }

    #[test]
    fn test_format_with_commas_millions() {
        assert_eq!(format_with_commas(1_000_000), "1,000,000");
        assert_eq!(format_with_commas(1_234_567), "1,234,567");
    }

    #[test]
    fn test_format_uptime_zero() {
        assert_eq!(format_uptime(0), "00:00:00");
    }

    #[test]
    fn test_format_uptime_seconds() {
        assert_eq!(format_uptime(45), "00:00:45");
    }

    #[test]
    fn test_format_uptime_minutes() {
        assert_eq!(format_uptime(125), "00:02:05");
    }

    #[test]
    fn test_format_uptime_hours() {
        assert_eq!(format_uptime(3661), "01:01:01");
        assert_eq!(format_uptime(86399), "23:59:59");
    }

    #[test]
    fn test_render_level_bar_zero_total() {
        let bar = render_level_bar(0, 0);
        assert_eq!(bar.chars().count(), 10);
        // All empty blocks
        assert!(bar.chars().all(|c| c == '\u{2591}'));
    }

    #[test]
    fn test_render_level_bar_full() {
        let bar = render_level_bar(100, 100);
        assert_eq!(bar.chars().count(), 10);
        assert!(bar.chars().all(|c| c == '\u{2588}'));
    }

    #[test]
    fn test_render_level_bar_half() {
        let bar = render_level_bar(50, 100);
        assert_eq!(bar.chars().count(), 10);
        let filled = bar.chars().filter(|&c| c == '\u{2588}').count();
        assert_eq!(filled, 5);
    }

    #[test]
    fn test_render_level_bar_empty() {
        let bar = render_level_bar(0, 100);
        assert_eq!(bar.chars().count(), 10);
        assert!(bar.chars().all(|c| c == '\u{2591}'));
    }

    #[test]
    fn test_render_sparkline_empty() {
        let ring = [0_usize; SPARKLINE_RING_SIZE];
        let sparkline = render_sparkline(&ring, 0);
        assert_eq!(sparkline.chars().count(), SPARKLINE_RING_SIZE);
        // All minimum bars since all values are 0
        assert!(sparkline.chars().all(|c| c == '\u{2581}'));
    }

    #[test]
    fn test_render_sparkline_uniform() {
        let ring = [100_usize; SPARKLINE_RING_SIZE];
        let sparkline = render_sparkline(&ring, 0);
        assert_eq!(sparkline.chars().count(), SPARKLINE_RING_SIZE);
    }

    #[test]
    fn test_render_sparkline_varied() {
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        ring[0] = 100;
        ring[30] = 50;
        ring[59] = 25;
        let sparkline = render_sparkline(&ring, 0);
        assert_eq!(sparkline.chars().count(), SPARKLINE_RING_SIZE);
    }

    #[test]
    fn test_render_sparkline_cursor_wrap() {
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        ring[55] = 10;
        ring[5] = 20;
        let sparkline = render_sparkline(&ring, 50);
        assert_eq!(sparkline.chars().count(), SPARKLINE_RING_SIZE);
    }

    #[test]
    fn test_get_terminal_width() {
        // In test/CI environments this typically returns 80 fallback
        let w = get_terminal_width();
        assert!(w > 0);
    }

    #[test]
    fn test_tui_metrics_inc_level_all_variants() {
        let m = TuiMetrics::default();

        m.inc_level(crate::log_level::LogLevel::TRACE);
        assert_eq!(m.level_trace.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::DEBUG);
        assert_eq!(m.level_debug.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::INFO);
        assert_eq!(m.level_info.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::WARN);
        assert_eq!(m.level_warn.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::ERROR);
        assert_eq!(m.level_error.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::FATAL);
        assert_eq!(m.level_fatal.load(Ordering::Relaxed), 1);

        m.inc_level(crate::log_level::LogLevel::CRITICAL);
        assert_eq!(m.level_critical.load(Ordering::Relaxed), 1);

        // Non-tracked levels should not panic
        m.inc_level(crate::log_level::LogLevel::ALL);
        m.inc_level(crate::log_level::LogLevel::NONE);
    }

    #[test]
    fn test_tui_metrics_inc_format_all_variants() {
        let m = TuiMetrics::default();

        m.inc_format(crate::log_format::LogFormat::CLF);
        assert_eq!(m.fmt_clf.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::JSON);
        assert_eq!(m.fmt_json.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::CEF);
        assert_eq!(m.fmt_cef.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::ELF);
        assert_eq!(m.fmt_elf.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::W3C);
        assert_eq!(m.fmt_w3c.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::GELF);
        assert_eq!(m.fmt_gelf.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::ApacheAccessLog);
        assert_eq!(m.fmt_apache.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::Logstash);
        assert_eq!(m.fmt_logstash.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::Log4jXML);
        assert_eq!(m.fmt_log4j.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::NDJSON);
        assert_eq!(m.fmt_ndjson.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::MCP);
        assert_eq!(m.fmt_mcp.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::OTLP);
        assert_eq!(m.fmt_otlp.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::Logfmt);
        assert_eq!(m.fmt_logfmt.load(Ordering::Relaxed), 1);

        m.inc_format(crate::log_format::LogFormat::ECS);
        assert_eq!(m.fmt_ecs.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn test_tui_metrics_peak_throughput() {
        let m = TuiMetrics::default();
        m.peak_throughput.store(100, Ordering::Relaxed);
        assert_eq!(m.peak_throughput.load(Ordering::Relaxed), 100);
    }

    #[test]
    fn test_tui_metrics_start_epoch() {
        let m = TuiMetrics::default();
        m.start_epoch_secs.store(1_234_567_890, Ordering::Relaxed);
        assert_eq!(
            m.start_epoch_secs.load(Ordering::Relaxed),
            1_234_567_890
        );
    }

    #[test]
    fn test_spark_chars_length() {
        assert_eq!(SPARK_CHARS.len(), 8);
    }

    #[test]
    fn test_build_fmt_line_empty() {
        let m = TuiMetrics::default();
        let line = build_fmt_line(&m);
        assert_eq!(line, "(none)");
    }

    #[test]
    fn test_build_fmt_line_single() {
        let m = TuiMetrics::default();
        m.fmt_json.store(42, Ordering::Relaxed);
        let line = build_fmt_line(&m);
        assert_eq!(line, "JSON: 42");
    }

    #[test]
    fn test_build_fmt_line_multiple() {
        let m = TuiMetrics::default();
        m.fmt_json.store(10, Ordering::Relaxed);
        m.fmt_mcp.store(20, Ordering::Relaxed);
        m.fmt_otlp.store(5, Ordering::Relaxed);
        let line = build_fmt_line(&m);
        assert!(line.contains("JSON: 10"));
        assert!(line.contains("MCP: 20"));
        assert!(line.contains("OTLP: 5"));
        assert!(line.contains(" | "));
    }

    #[test]
    fn test_build_fmt_line_all_formats() {
        let m = TuiMetrics::default();
        m.fmt_clf.store(1, Ordering::Relaxed);
        m.fmt_json.store(2, Ordering::Relaxed);
        m.fmt_cef.store(3, Ordering::Relaxed);
        m.fmt_elf.store(4, Ordering::Relaxed);
        m.fmt_w3c.store(5, Ordering::Relaxed);
        m.fmt_gelf.store(6, Ordering::Relaxed);
        m.fmt_apache.store(7, Ordering::Relaxed);
        m.fmt_logstash.store(8, Ordering::Relaxed);
        m.fmt_log4j.store(9, Ordering::Relaxed);
        m.fmt_ndjson.store(10, Ordering::Relaxed);
        m.fmt_mcp.store(11, Ordering::Relaxed);
        m.fmt_otlp.store(12, Ordering::Relaxed);
        m.fmt_logfmt.store(13, Ordering::Relaxed);
        m.fmt_ecs.store(14, Ordering::Relaxed);
        let line = build_fmt_line(&m);
        assert!(line.contains("CLF: 1"));
        assert!(line.contains("ECS: 14"));
    }

    #[test]
    fn test_compute_level_bars_empty() {
        let m = TuiMetrics::default();
        let (info_bar, info_pct, error_bar, error_pct) =
            compute_level_bars(&m);
        assert_eq!(info_pct, 0);
        assert_eq!(error_pct, 0);
        assert_eq!(info_bar.chars().count(), 10);
        assert_eq!(error_bar.chars().count(), 10);
    }

    #[test]
    fn test_compute_level_bars_with_data() {
        let m = TuiMetrics::default();
        m.level_info.store(80, Ordering::Relaxed);
        m.level_error.store(20, Ordering::Relaxed);
        let (info_bar, info_pct, error_bar, error_pct) =
            compute_level_bars(&m);
        assert_eq!(info_pct, 80);
        assert_eq!(error_pct, 20);
        // info_bar should have 8 filled blocks
        let filled =
            info_bar.chars().filter(|&c| c == '\u{2588}').count();
        assert_eq!(filled, 8);
        // error_bar should have 2 filled blocks
        let filled =
            error_bar.chars().filter(|&c| c == '\u{2588}').count();
        assert_eq!(filled, 2);
    }

    #[test]
    fn test_compute_level_bars_all_levels() {
        let m = TuiMetrics::default();
        m.level_info.store(50, Ordering::Relaxed);
        m.level_warn.store(20, Ordering::Relaxed);
        m.level_error.store(10, Ordering::Relaxed);
        m.level_debug.store(15, Ordering::Relaxed);
        m.level_trace.store(5, Ordering::Relaxed);
        let (_info_bar, info_pct, _error_bar, error_pct) =
            compute_level_bars(&m);
        assert_eq!(info_pct, 50);
        assert_eq!(error_pct, 10);
    }

    #[test]
    fn test_render_tick_basic() {
        let m = TuiMetrics::default();
        m.total_events.store(100, Ordering::Relaxed);
        m.error_count.store(5, Ordering::Relaxed);
        m.active_spans.store(2, Ordering::Relaxed);
        m.level_info.store(80, Ordering::Relaxed);
        m.level_error.store(20, Ordering::Relaxed);
        m.fmt_json.store(50, Ordering::Relaxed);
        m.fmt_mcp.store(30, Ordering::Relaxed);

        #[allow(clippy::cast_possible_truncation)]
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as usize;
        m.start_epoch_secs.store(now, Ordering::Relaxed);

        let mut last_total = 0_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        let frame =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        assert!(frame.contains("RLG Liquid Glass Dashboard"));
        assert!(frame.contains("Errors:"));
        assert!(frame.contains("Active Spans:"));
        assert!(frame.contains("Throughput"));
        assert!(frame.contains("Peak:"));
        assert!(frame.contains("Uptime:"));
        assert!(frame.contains("Levels:"));
        assert!(frame.contains("Formats:"));
        assert!(frame.contains("JSON: 50"));
        assert!(frame.contains("MCP: 30"));
    }

    #[test]
    fn test_render_tick_updates_state() {
        let m = TuiMetrics::default();
        m.total_events.store(100, Ordering::Relaxed);
        m.start_epoch_secs.store(0, Ordering::Relaxed);

        let mut last_total = 0_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        let _ =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);

        // last_total should be updated
        assert_eq!(last_total, 100);
        // cursor should be incremented
        assert_eq!(cursor, 1);
        // ring[0] should have the throughput value
        assert_eq!(ring[0], 100 * 60); // diff * 60
        // throughput metric should be stored
        assert_eq!(m.throughput.load(Ordering::Relaxed), 100 * 60);
    }

    #[test]
    fn test_render_tick_no_diff() {
        let m = TuiMetrics::default();
        m.total_events.store(50, Ordering::Relaxed);
        m.start_epoch_secs.store(0, Ordering::Relaxed);

        let mut last_total = 50_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        let _ =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        assert_eq!(m.throughput.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn test_render_tick_peak_tracking() {
        let m = TuiMetrics::default();
        m.start_epoch_secs.store(0, Ordering::Relaxed);

        let mut last_total = 0_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        // First tick: 100 events => tps = 6000
        m.total_events.store(100, Ordering::Relaxed);
        let _ =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        assert_eq!(m.peak_throughput.load(Ordering::Relaxed), 6000);

        // Second tick: 50 more events => tps = 3000
        m.total_events.store(150, Ordering::Relaxed);
        let _ =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        // Peak should still be 6000
        assert_eq!(m.peak_throughput.load(Ordering::Relaxed), 6000);
    }

    #[test]
    fn test_render_tick_no_formats() {
        let m = TuiMetrics::default();
        m.start_epoch_secs.store(0, Ordering::Relaxed);

        let mut last_total = 0_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        let frame =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        assert!(frame.contains("(none)"));
    }

    #[test]
    fn test_get_terminal_height() {
        let h = get_terminal_height();
        assert!(h > 0);
    }

    #[test]
    fn test_tui_metrics_dropped_events() {
        let m = TuiMetrics::default();
        assert_eq!(m.dropped_events.load(Ordering::Relaxed), 0);
        m.inc_dropped();
        m.inc_dropped();
        assert_eq!(m.dropped_events.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn test_render_tick_shows_dropped() {
        let m = TuiMetrics::default();
        m.start_epoch_secs.store(0, Ordering::Relaxed);
        m.dropped_events.store(42, Ordering::Relaxed);

        let mut last_total = 0_usize;
        let mut ring = [0_usize; SPARKLINE_RING_SIZE];
        let mut cursor = 0_usize;

        let frame =
            render_tick(&m, &mut last_total, &mut ring, &mut cursor);
        assert!(frame.contains("Dropped:"));
        assert!(frame.contains("42"));
    }
}