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
949
950
951
952
953
954
955
956
957
#![cfg(not(miri))]
#![allow(missing_docs)]

//! Tests specifically targeting uncovered lines identified by cargo-llvm-cov.

#[cfg(test)]
mod tests {
    // =========================================================================
    // tracing.rs coverage: DEBUG/TRACE events, record_str, record, record_follows_from
    // =========================================================================

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_debug_and_trace_events() {
        use rlg::tracing::RlgSubscriber;
        use tracing::{debug, trace};
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        // Set filter to ALL to let DEBUG and TRACE through
        rlg::engine::ENGINE.set_filter(rlg::LogLevel::ALL.to_numeric());

        dispatcher::with_default(&dispatch, || {
            debug!("debug event for coverage");
            trace!("trace event for coverage");
            debug!(key = 42u64, "debug with field");
            trace!(key = true, "trace with field");
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_record_and_follows_from() {
        use rlg::tracing::RlgSubscriber;
        use tracing::{Level, span};
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            let span1 = span!(
                Level::INFO,
                "span1",
                field1 = tracing::field::Empty
            );
            let span2 = span!(Level::INFO, "span2");

            // Trigger Subscriber::record()
            span1.record("field1", "recorded_value");

            // Trigger Subscriber::record_follows_from()
            span2.follows_from(&span1);
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_record_str_visitor() {
        use rlg::tracing::RlgSubscriber;
        use tracing::info;
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            // Pass &str fields to trigger record_str visitor method
            let s: &str = "string_value";
            info!(str_field = s, "test record_str");

            // Also pass a String via Display to cover other paths
            let owned = String::from("owned_string");
            info!(owned_field = owned.as_str(), "test owned str");

            // Test message field through record_str (using % display hint)
            info!(msg_field = %"display_str", "test display");

            // Trigger record_str with field named "message" (the special branch)
            // In tracing, passing message = "value" explicitly as a &str field
            // should dispatch through record_str
            info!(message = "explicit message via record_str");
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_warn_event() {
        use rlg::tracing::RlgSubscriber;
        use tracing::warn;
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            warn!(key = "val", "warn event with field for coverage");
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_span_enter_exit() {
        use rlg::tracing::RlgSubscriber;
        use tracing::{Level, span};
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            let span = span!(Level::INFO, "enter_exit_span");
            // enter() triggers Subscriber::enter, drop of guard triggers exit
            let _guard = span.enter();
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_record_error_visitor() {
        use rlg::tracing::RlgSubscriber;
        use tracing::info;
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            let err = std::io::Error::other("test error for coverage");
            let err_ref: &(dyn std::error::Error + 'static) = &err;
            info!(error = err_ref, "event with error field");
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_record_u128_i128() {
        use rlg::tracing::RlgSubscriber;
        use tracing::info;
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            info!(big_u = 42u128, big_i = -42i128, "u128/i128 fields");
        });
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_tracing_all_numeric_types() {
        use rlg::tracing::RlgSubscriber;
        use tracing::info;
        use tracing_core::dispatcher::{self, Dispatch};

        let subscriber = RlgSubscriber::new();
        let dispatch = Dispatch::new(subscriber);

        dispatcher::with_default(&dispatch, || {
            info!(
                u64_val = 100u64,
                i64_val = -100i64,
                bool_val = true,
                f64_val = 1.5_f64,
                "all numeric field types"
            );
        });
    }

    // =========================================================================
    // utils.rs coverage: generate_trace_id
    // =========================================================================

    #[test]
    fn test_generate_trace_id() {
        let trace_id = rlg::utils::generate_trace_id();
        assert_eq!(
            trace_id.len(),
            32,
            "Trace ID should be 32 hex chars"
        );
        assert!(
            trace_id.chars().all(|c| c.is_ascii_hexdigit()),
            "Trace ID should be valid hex"
        );

        // Two consecutive IDs should differ
        let trace_id2 = rlg::utils::generate_trace_id();
        // They might be the same if RNG is identical, but likely different
        let _ = trace_id2;
    }

    #[test]
    fn test_generate_span_id() {
        let span_id = rlg::utils::generate_span_id();
        assert_eq!(span_id.len(), 16, "Span ID should be 16 hex chars");
        assert!(
            span_id.chars().all(|c| c.is_ascii_hexdigit()),
            "Span ID should be valid hex"
        );
    }

    // =========================================================================
    // log_level.rs coverage: as_str_lowercase for ALL, NONE, DISABLED, VERBOSE, CRITICAL
    // =========================================================================

    #[test]
    fn test_log_level_as_str_lowercase_all_variants() {
        use rlg::log_level::LogLevel;

        assert_eq!(LogLevel::ALL.as_str_lowercase(), "all");
        assert_eq!(LogLevel::NONE.as_str_lowercase(), "none");
        assert_eq!(LogLevel::DISABLED.as_str_lowercase(), "disabled");
        assert_eq!(LogLevel::VERBOSE.as_str_lowercase(), "verbose");
        assert_eq!(LogLevel::CRITICAL.as_str_lowercase(), "critical");
        // Already covered but verify for completeness
        assert_eq!(LogLevel::TRACE.as_str_lowercase(), "trace");
        assert_eq!(LogLevel::DEBUG.as_str_lowercase(), "debug");
        assert_eq!(LogLevel::INFO.as_str_lowercase(), "info");
        assert_eq!(LogLevel::WARN.as_str_lowercase(), "warn");
        assert_eq!(LogLevel::ERROR.as_str_lowercase(), "error");
        assert_eq!(LogLevel::FATAL.as_str_lowercase(), "fatal");
    }

    // =========================================================================
    // log_format.rs coverage: ELF/ApacheAccessLog validate, JSON format_log error
    // =========================================================================

    #[test]
    fn test_log_format_validate_elf_and_apache() {
        use rlg::log_format::LogFormat;

        // ELF and ApacheAccessLog always return true for non-empty strings
        assert!(LogFormat::ELF.validate("any string here"));
        assert!(LogFormat::ApacheAccessLog.validate("any string here"));

        // But empty string should return false (handled by the early check)
        assert!(!LogFormat::ELF.validate(""));
        assert!(!LogFormat::ApacheAccessLog.validate(""));
    }

    // =========================================================================
    // config.rs coverage: VersionError, ensure_paths parent dir creation
    // =========================================================================

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn test_config_load_async_version_mismatch() {
        use rlg::config::Config;

        let temp_dir = tempfile::tempdir().unwrap();
        let config_content = r#"
            version = "99.99"
            log_file_path = "RLG.log"
            log_format = "%level - %message"
        "#;

        let config_file_path = temp_dir.path().join("config.toml");
        tokio::fs::write(&config_file_path, config_content)
            .await
            .unwrap();

        let result = Config::load_async(Some(&config_file_path)).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_msg = format!("{err}");
        assert!(
            err_msg.contains("version"),
            "Error should mention version: {err_msg}"
        );
    }

    #[test]
    fn test_config_ensure_paths_with_subdirectory() {
        use rlg::config::{Config, LoggingDestination};

        let temp_dir = tempfile::tempdir().unwrap();
        let nested_path =
            temp_dir.path().join("sub").join("dir").join("test.log");

        let config = Config {
            logging_destinations: vec![LoggingDestination::File(
                nested_path.clone(),
            )],
            ..Config::default()
        };

        // Should create parent directories and succeed
        assert!(config.ensure_paths().is_ok());
        assert!(nested_path.parent().unwrap().exists());
    }

    #[test]
    fn test_config_ensure_paths_no_file_destination() {
        use rlg::config::{Config, LoggingDestination};

        let config = Config {
            logging_destinations: vec![LoggingDestination::Stdout],
            ..Config::default()
        };

        // Should succeed without creating any files
        assert!(config.ensure_paths().is_ok());
    }

    // =========================================================================
    // log.rs coverage: write_json_str escaping for special characters
    // =========================================================================

    #[test]
    fn test_log_json_str_escaping() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;
        use rlg::log_level::LogLevel;

        // Create a log with description containing special chars that need escaping
        let log = Log::build(LogLevel::INFO, "desc with \"quotes\" and \\backslash and \nnewline and \rcarriage and \ttab")
            .session_id(1)
            .time("ts")
            .component("comp")
            .format(LogFormat::JSON);

        let output = format!("{log}");

        // Verify JSON escaping works
        assert!(output.contains("\\\""), "Should escape double quotes");
        assert!(output.contains("\\\\"), "Should escape backslashes");
        assert!(output.contains("\\n"), "Should escape newlines");
        assert!(
            output.contains("\\r"),
            "Should escape carriage returns"
        );
        assert!(output.contains("\\t"), "Should escape tabs");

        // Verify it's valid JSON
        let parsed: serde_json::Value = serde_json::from_str(&output)
            .unwrap_or_else(|e| {
                panic!(
                    "Output should be valid JSON: {e}\nOutput: {output}"
                )
            });
        assert!(parsed.is_object());
    }

    #[test]
    fn test_log_json_str_escaping_in_component() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;
        use rlg::log_level::LogLevel;

        // Test escaping in the component field too
        let log = Log::build(LogLevel::INFO, "normal desc")
            .session_id(1)
            .time("ts")
            .component("comp\twith\ttabs")
            .format(LogFormat::JSON);

        let output = format!("{log}");
        assert!(
            output.contains("\\t"),
            "Should escape tabs in component"
        );

        // Also test session_id field (now u64, no escaping needed)
        let log2 = Log::build(LogLevel::INFO, "desc")
            .session_id(42)
            .time("ts")
            .component("comp")
            .format(LogFormat::JSON);
        let output2 = format!("{log2}");
        assert!(
            output2.contains("\"SessionID\":42"),
            "Should contain numeric session_id"
        );
    }

    #[test]
    fn test_log_json_str_escaping_via_fluent_api() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;

        // Test with special chars via fluent API
        let log = Log::info("test\nwith\nnewlines")
            .format(LogFormat::GELF)
            .component("comp\\with\\backslash");

        let output = format!("{log}");
        assert!(
            output.contains("\\n"),
            "Should escape newlines in GELF format"
        );
    }

    // =========================================================================
    // sink.rs coverage: journald env var fallback path
    // =========================================================================

    #[test]
    #[allow(unsafe_code)]
    fn test_platform_sink_journald_with_env_fallback() {
        // Test the journald emit path where RLG_FALLBACK_STDOUT is set
        // This covers the env var check inside the Journald emit branch
        // SAFETY: Test-only; no other threads depend on this env var.
        unsafe { std::env::set_var("RLG_FALLBACK_STDOUT", "1") };

        #[cfg(unix)]
        {
            use rlg::sink::PlatformSink;
            use std::os::unix::net::UnixDatagram;
            if let Ok(socket) = UnixDatagram::unbound() {
                let mut sink = PlatformSink::Journald(Some(socket));
                sink.emit("INFO", b"test with fallback env");
            }
        }

        // SAFETY: Test-only cleanup.
        unsafe { std::env::remove_var("RLG_FALLBACK_STDOUT") };
    }

    // =========================================================================
    // engine.rs coverage: filter level return, queue management
    // =========================================================================

    #[test]
    fn test_engine_filter_drops_below_level() {
        use rlg::LogLevel;
        use rlg::engine::{LockFreeEngine, LogEvent};

        let engine = LockFreeEngine::new(10);
        // Set filter to ERROR level
        engine.set_filter(LogLevel::ERROR.to_numeric());

        // This event should be filtered out (level < filter)
        let event = LogEvent {
            level: LogLevel::DEBUG,
            level_num: LogLevel::DEBUG.to_numeric(),
            log: rlg::log::Log::debug("should be dropped"),
        };
        engine.ingest(event);

        engine.shutdown();
    }

    // =========================================================================
    // log.rs: write_logfmt attribute branch that produces "?" for other values
    // =========================================================================

    #[test]
    fn test_log_write_logfmt_quote_in_description() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;
        use rlg::log_level::LogLevel;

        let log = Log::build(LogLevel::INFO, "desc with \"quotes\"")
            .session_id(1)
            .time("ts")
            .component("comp")
            .format(LogFormat::Logfmt);
        let output = format!("{log}");
        assert!(output.contains(r#"msg="desc with \"quotes\"""#));
    }

    // =========================================================================
    // config.rs: validate with env vars that have a valid key but empty value
    // (line 439 is the closing brace after error return - need a non-empty
    // key with non-empty value to pass validation successfully in the loop)
    // =========================================================================

    #[test]
    fn test_config_validate_env_vars_valid_pair() {
        use rlg::config::Config;
        use std::collections::HashMap;

        // This exercises the loop body where both key and value are non-empty
        let mut env_vars = HashMap::new();
        env_vars
            .insert("VALID_KEY".to_string(), "valid_value".to_string());
        let config = Config {
            env_vars,
            ..Config::default()
        };
        assert!(config.validate().is_ok());
    }

    // =========================================================================
    // config.rs: hot_reload_async trigger modify event
    // =========================================================================

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn test_config_hot_reload_modify_event() {
        use parking_lot::RwLock;
        use rlg::config::Config;
        use std::sync::Arc;
        use tokio::time::{Duration, sleep};

        let temp_dir = tempfile::tempdir().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        // Write initial valid config
        let initial = r#"
            version = "1.0"
            profile = "default"
            log_file_path = "RLG.log"
            log_format = "%level - %message"
        "#;
        tokio::fs::write(&config_path, initial).await.unwrap();

        let shared_config = Arc::new(RwLock::new(Config::default()));
        let stop_tx = Config::hot_reload_async(
            config_path.to_str().unwrap(),
            &shared_config,
        )
        .unwrap();

        // Modify the config file to trigger a Modify event
        let modified = r#"
            version = "1.0"
            profile = "hot_reloaded"
            log_file_path = "RLG.log"
            log_format = "%level - %message"
        "#;
        tokio::fs::write(&config_path, modified).await.unwrap();

        // Give the watcher time to detect and process the change
        sleep(Duration::from_millis(500)).await;

        let _ = stop_tx.send(()).await;
    }

    // =========================================================================
    // log_format.rs: MCP/OTLP/Logfmt from_str
    // =========================================================================

    #[test]
    fn test_log_format_from_str_remaining_variants() {
        use rlg::log_format::LogFormat;
        use std::str::FromStr;

        assert_eq!(LogFormat::from_str("mcp").unwrap(), LogFormat::MCP);
        assert_eq!(
            LogFormat::from_str("otlp").unwrap(),
            LogFormat::OTLP
        );
        assert_eq!(
            LogFormat::from_str("logfmt").unwrap(),
            LogFormat::Logfmt
        );
        assert_eq!(LogFormat::from_str("ecs").unwrap(), LogFormat::ECS);
        assert_eq!(
            LogFormat::from_str("apache").unwrap(),
            LogFormat::ApacheAccessLog
        );
    }

    // =========================================================================
    // log.rs: Display for formats with special characters
    // =========================================================================

    #[test]
    fn test_log_display_all_json_formats_with_escaping() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;

        let special_desc = "line1\nline2\ttab\\backslash\"quote";

        // Test each JSON format
        for format in [
            LogFormat::JSON,
            LogFormat::GELF,
            LogFormat::Logstash,
            LogFormat::NDJSON,
            LogFormat::MCP,
            LogFormat::ECS,
        ] {
            let log = Log::info(special_desc)
                .format(format)
                .component("test");
            let output = format!("{log}");
            // All JSON formats should produce valid JSON with proper escaping
            assert!(
                serde_json::from_str::<serde_json::Value>(&output)
                    .is_ok(),
                "Format {:?} should produce valid JSON. Got: {}",
                format,
                output
            );
        }

        // OTLP format (has specific structure)
        let log = Log::info(special_desc)
            .format(LogFormat::OTLP)
            .component("test");
        let output = format!("{log}");
        // OTLP has extra }} at end, verify it still contains escaped chars
        assert!(output.contains("\\n"));
    }

    // =========================================================================
    // log.rs: write_json_map with empty map (already covered by default attributes)
    // and with content
    // =========================================================================

    #[test]
    fn test_log_json_map_with_multiple_attributes() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;

        let log = Log::info("test attributes")
            .format(LogFormat::JSON)
            .with("key1", "value1")
            .with("key2", 42)
            .with("key3", true);

        let output = format!("{log}");
        let parsed: serde_json::Value =
            serde_json::from_str(&output).unwrap();
        let attrs = parsed.get("Attributes").unwrap();
        assert_eq!(attrs.get("key1").unwrap(), "value1");
        assert_eq!(attrs.get("key2").unwrap(), 42);
        assert_eq!(attrs.get("key3").unwrap(), true);
    }

    // =========================================================================
    // engine.rs: queue full path (reliable fill without flusher race)
    // =========================================================================

    #[test]
    fn test_engine_queue_full_retry() {
        use rlg::LogLevel;
        use rlg::engine::{LockFreeEngine, LogEvent};

        // Create a tiny queue
        let engine = LockFreeEngine::new(1);

        // Rapidly fill beyond capacity to trigger the retry loop
        for _ in 0..5 {
            let event = LogEvent {
                level: LogLevel::INFO,
                level_num: LogLevel::INFO.to_numeric(),
                log: rlg::log::Log::info("fill"),
            };
            engine.ingest(event);
        }

        engine.shutdown();
    }

    // =========================================================================
    // sink.rs: native() without env vars (covers the cfg(test) Journald path)
    // =========================================================================

    #[test]
    fn test_platform_sink_native_without_env() {
        use rlg::sink::PlatformSink;

        // Call native() directly - in test cfg on Linux, this hits the
        // #[cfg(all(target_os = "linux", test))] block returning Journald(None)
        // unless RLG_FALLBACK_STDOUT or GITHUB_ACTIONS is set
        let _sink = PlatformSink::native();
    }

    // =========================================================================
    // config.rs: hot_reload_async with invalid path (watcher error)
    // =========================================================================

    #[cfg(feature = "tokio")]
    #[test]
    fn test_config_hot_reload_invalid_path() {
        use parking_lot::RwLock;
        use rlg::config::Config;
        use std::sync::Arc;

        let shared_config = Arc::new(RwLock::new(Config::default()));
        let result = Config::hot_reload_async(
            "/nonexistent/path/that/does/not/exist.toml",
            &shared_config,
        );
        // The watcher.watch() call should fail for non-existent paths
        assert!(result.is_err());
    }

    // =========================================================================
    // config.rs: ensure_paths with root path (parent() returns None)
    // =========================================================================

    #[test]
    fn test_config_ensure_paths_root_path_no_parent() {
        use rlg::config::{Config, LoggingDestination};
        use std::path::PathBuf;

        // PathBuf::from("/") has parent() == None on Unix.
        // This exercises the implicit else branch of `if let Some(parent_dir)`.
        let config = Config {
            logging_destinations: vec![LoggingDestination::File(
                PathBuf::from("/"),
            )],
            ..Config::default()
        };

        // Opening "/" for append fails (it's a directory), but that's OK —
        // we're testing that the parent-dir-creation block is skipped.
        let result = config.ensure_paths();
        assert!(result.is_err());
    }

    // =========================================================================
    // config.rs: hot_reload_async spawn completes cleanly
    // =========================================================================

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn test_config_hot_reload_spawn_completes() {
        use parking_lot::RwLock;
        use rlg::config::Config;
        use std::sync::Arc;
        use tokio::time::{Duration, sleep};

        let temp_dir = tempfile::tempdir().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let initial = r#"
            version = "1.0"
            profile = "default"
            log_file_path = "RLG.log"
            log_format = "%level - %message"
        "#;
        tokio::fs::write(&config_path, initial).await.unwrap();

        let shared_config = Arc::new(RwLock::new(Config::default()));
        let stop_tx = Config::hot_reload_async(
            config_path.to_str().unwrap(),
            &shared_config,
        )
        .unwrap();

        // Send stop signal and wait for the spawned task to complete
        let _ = stop_tx.send(()).await;
        sleep(Duration::from_millis(100)).await;
    }

    // =========================================================================
    // log.rs: write_logfmt with attributes (all value branches)
    // =========================================================================

    #[test]
    fn test_log_write_logfmt_all_attribute_types() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;
        use rlg::log_level::LogLevel;

        // Test logfmt with all attribute value types to cover every branch
        let log = Log::build(LogLevel::INFO, "logfmt test")
            .session_id(1)
            .time("ts")
            .component("comp")
            .with("simple_str", "nospaces") // unquoted string
            .with("spaced_str", "has spaces") // quoted string (contains space)
            .with("quoted_str", "has\"quotes") // quoted string (contains quote)
            .with("empty_str", "") // quoted string (empty)
            .with("number", 42) // non-string value
            .with("flag", true) // non-string value
            .format(LogFormat::Logfmt);

        let output = format!("{log}");

        // Verify base fields
        assert!(
            output.contains("level=info"),
            "Should contain level=info"
        );
        assert!(
            output.contains("msg=\"logfmt test\""),
            "Should contain quoted msg"
        );
        assert!(
            output.contains("session_id=1"),
            "Should contain session_id"
        );
        assert!(
            output.contains("component=\"comp\""),
            "Should contain quoted component"
        );

        // Verify attribute formatting
        assert!(
            output.contains("simple_str=nospaces"),
            "Simple string unquoted"
        );
        assert!(
            output.contains("spaced_str=\"has spaces\""),
            "Spaced string quoted"
        );
        assert!(output.contains("number=42"), "Number without quotes");
        assert!(output.contains("flag=true"), "Bool without quotes");
    }

    // =========================================================================
    // log_format.rs: format_log for all JSON-based formats (success path)
    // =========================================================================

    #[test]
    fn test_log_format_format_log_json_success() {
        use rlg::log_format::LogFormat;

        let json_input = r#"{"key": "value", "count": 42}"#;
        for format in [
            LogFormat::JSON,
            LogFormat::Logstash,
            LogFormat::NDJSON,
            LogFormat::GELF,
            LogFormat::MCP,
            LogFormat::OTLP,
            LogFormat::ECS,
        ] {
            let result = format.format_log(json_input);
            assert!(
                result.is_ok(),
                "format_log should succeed for {:?}: {:?}",
                format,
                result
            );
            let formatted = result.unwrap();
            assert!(
                formatted.contains("key"),
                "Formatted output should contain key for {:?}",
                format
            );
        }
    }

    // =========================================================================
    // tui.rs: get_terminal_height_fd with invalid fd (covers fallback path)
    // and with a real PTY (covers success path)
    // =========================================================================

    #[test]
    #[cfg(not(windows))]
    fn test_get_terminal_height_of_non_terminal() {
        // A regular file is not a terminal, so terminal_size_of returns None → fallback 24
        let file = std::fs::File::open("/dev/null").unwrap();
        let height = rlg::tui::get_terminal_height_of(&file);
        assert_eq!(
            height, 24,
            "Non-terminal fd should return fallback height"
        );
    }

    #[test]
    #[cfg(not(windows))]
    fn test_get_terminal_height_of_with_pty() {
        // Open a PTY master to get a real terminal fd
        let result = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open("/dev/ptmx");
        if let Ok(ptmx) = result {
            let height = rlg::tui::get_terminal_height_of(&ptmx);
            // PTY might report 0 rows (no real terminal size),
            // but terminal_size_of should succeed, covering the success branch.
            let _ = height;
        }
        // If /dev/ptmx is not available, skip silently
    }

    // sink.rs: native() coverage is tested in src/sink.rs inline tests
    // using #[serial] to prevent env var race conditions.

    // =========================================================================
    // engine.rs: queue full retry with merged break condition
    // =========================================================================

    #[test]
    fn test_engine_queue_full_merged_break() {
        use rlg::LogLevel;
        use rlg::engine::{LockFreeEngine, LogEvent};

        // Create the smallest possible queue
        let engine = LockFreeEngine::new(1);

        // Shut down the flusher thread first, then wait for it to exit.
        // This guarantees no one is draining the queue.
        engine.shutdown();
        std::thread::sleep(std::time::Duration::from_millis(10));

        // Now fill the queue with guaranteed overflow.
        // First event fills the capacity-1 queue; second triggers the while loop.
        let event1 = LogEvent {
            level: LogLevel::INFO,
            level_num: LogLevel::INFO.to_numeric(),
            log: rlg::log::Log::info("fill1"),
        };
        engine.ingest(event1); // succeeds, queue now full

        let event2 = LogEvent {
            level: LogLevel::INFO,
            level_num: LogLevel::INFO.to_numeric(),
            log: rlg::log::Log::info("overflow"),
        };
        engine.ingest(event2); // queue full → enters while loop → break
    }

    // =========================================================================
    // log.rs: Display for Logfmt format (exercises write_logfmt success path)
    // =========================================================================

    #[test]
    fn test_log_display_logfmt_basic() {
        use rlg::log::Log;
        use rlg::log_format::LogFormat;
        use rlg::log_level::LogLevel;

        let log = Log::build(LogLevel::WARN, "simple warning")
            .session_id(1)
            .time("2025-01-01")
            .component("api")
            .format(LogFormat::Logfmt);

        let output = format!("{log}");
        assert!(output.starts_with("level=warn "));
        assert!(output.contains("msg=\"simple warning\""));
        assert!(output.contains("session_id=1"));
        assert!(output.contains("component=\"api\""));
    }

    // =========================================================================
    // log_format.rs: format_log for non-JSON formats (CLF, Logfmt, etc.)
    // =========================================================================

    #[test]
    fn test_log_format_format_log_non_json() {
        use rlg::log_format::LogFormat;

        let plain_input =
            "127.0.0.1 - user [2025-01-01] \"GET / HTTP/1.1\" 200 1234";

        for format in [
            LogFormat::CLF,
            LogFormat::ApacheAccessLog,
            LogFormat::CEF,
            LogFormat::ELF,
            LogFormat::W3C,
            LogFormat::Log4jXML,
            LogFormat::Logfmt,
        ] {
            let result = format.format_log(plain_input);
            assert!(
                result.is_ok(),
                "format_log should succeed for {:?}",
                format
            );
        }
    }
}