memfaultd 1.26.1

Memfault daemon for embedded Linux systems. Observability, logging, crash reporting, and updating all in one service. Learn more at https://docs.memfault.com/
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
//
// Copyright (c) Memfault, Inc.
// See License.txt for details
//! Collect logs into log files and save them as MAR entries.
//!
use std::path::PathBuf;
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::{fs, sync::atomic::AtomicUsize};
use std::{io::Cursor, sync::Arc};
use std::{num::NonZeroU32, sync::atomic::Ordering};

use chrono::{DateTime, Utc};
use eyre::{eyre, Context, Result};
use flate2::Compression;
use log::warn;
use serde::{Deserialize, Serialize};
use ssf::{Handler, MsgMailbox, Service};
use tiny_http::{Header, Method, Request, Response, ResponseBox, StatusCode};

use crate::config::{Config, DeviceConfig, DeviceConfigUpdateMessage, LogFilterConfig, Resolution};
use crate::http_server::HttpHandlerResult;
use crate::{config::LogToMetricRule, logs::completed_log::CompletedLog};
use crate::{config::StorageConfig, http_server::ConvenientHeader};
use crate::{
    http_server::{parse_query_params, HttpHandler},
    logs::log_file::{LogFile, LogFileControl, LogFileControlImpl},
};
use crate::{logs::headroom::HeadroomCheck, util::circular_queue::CircularQueue};
use crate::{metrics::MetricsMBox, util::rate_limiter::RateLimiter};

pub const CRASH_LOGS_URL: &str = "/api/v1/crash-logs";
pub const CRASH_LOGS_CRASH_TS_PARAM: &str = "time_of_crash";

use super::log_filter::LogFilter;
use super::log_level_mapper::LogLevelMapper;
use super::messages::GetLatestLogTimestampMsg;

use crate::config::LevelMappingConfig;

use super::{
    log_entry::LogEntry,
    messages::{FlushLogsMsg, GetQueuedLogsMsg, LogEntryMsg, RecoverLogsMsg, RotateIfNeededMsg},
};

pub struct LogCollector<H: HeadroomCheck + Send + 'static> {
    inner: Option<Inner<H>>,
}

impl<H: HeadroomCheck + Send + 'static> LogCollector<H> {
    /// This value is used to clamp the number of lines captured in a coredump.
    ///
    /// This is done to prevent the coredump from becoming too large. The value was chosen
    /// arbitrarily to be large enough to capture a reasonable amount of logs, but small enough
    /// to prevent the coredump from becoming too large. The current default is 100 lines.
    const MAX_IN_MEMORY_LINES: usize = 500;

    /// Create a new log collector and open a new log file for writing.
    /// The on_log_completion callback will be called when a log file is completed.
    /// This callback must move (or delete) the log file!
    pub fn open<R: FnMut(CompletedLog) -> Result<()> + Send + 'static>(
        log_config: LogCollectorConfig,
        on_log_completion: R,
        headroom_limiter: H,
        metrics_mbox: MetricsMBox,
        device_config: Arc<DeviceConfig>,
    ) -> Result<Self> {
        fs::create_dir_all(&log_config.log_tmp_path).wrap_err_with(|| {
            format!(
                "Unable to create directory to store in-progress logs: {}",
                log_config.log_tmp_path.display()
            )
        })?;

        // Collect any leftover logfiles in the tmp folder
        let level_mapper = if log_config.level_mapping_config.enable {
            Some(LogLevelMapper::try_from(&log_config.level_mapping_config)?)
        } else {
            None
        };
        let in_memory_lines = if log_config.in_memory_lines > Self::MAX_IN_MEMORY_LINES {
            warn!(
                "Too many lines captured in coredump ({}), clamping to {}",
                log_config.in_memory_lines,
                Self::MAX_IN_MEMORY_LINES
            );
            Self::MAX_IN_MEMORY_LINES
        } else {
            log_config.in_memory_lines
        };

        Ok(Self {
            inner: Some(Inner {
                log_file_control: LogFileControlImpl::open(
                    log_config.log_tmp_path,
                    log_config.log_max_size,
                    log_config.log_max_duration,
                    log_config.log_compression_level,
                    on_log_completion,
                )?,
                rate_limiter: RateLimiter::new(log_config.max_lines_per_minute),
                headroom_limiter,
                log_filter: LogFilter::new(
                    log_config.log_filter_config.rules,
                    log_config.log_to_metrics_rules,
                    log_config.log_filter_config.default_action,
                    metrics_mbox,
                ),
                log_queue: CircularQueue::new(in_memory_lines),
                storage_config: log_config.storage_config,
                level_mapper,
                device_config,
            }),
        })
    }

    /// Try to get the inner log_collector or return an error
    fn with_mut_inner<T, F: FnOnce(&mut Inner<H>) -> Result<T>>(&mut self, fun: F) -> Result<T> {
        let mut inner_opt = &mut self.inner;

        match &mut inner_opt {
            Some(inner) => fun(inner),
            None => Err(eyre!("Log collector has already shutdown.")),
        }
    }

    /// Close and dispose of the inner log collector.
    /// This is not public because it does not consume self (to be compatible with drop()).
    fn close_internal(&mut self) -> Result<()> {
        match self.inner.take() {
            Some(inner) => inner.log_file_control.close(),
            None => {
                // Already closed.
                Ok(())
            }
        }
    }
}

impl<H: HeadroomCheck + Send> Drop for LogCollector<H> {
    fn drop(&mut self) {
        if let Err(e) = self.close_internal() {
            warn!("Error closing log collector: {}", e);
        }
    }
}

impl<H: HeadroomCheck + Send> Service for LogCollector<H> {
    fn name(&self) -> &str {
        "LogCollector"
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<FlushLogsMsg> for LogCollector<H> {
    fn deliver(&mut self, _m: FlushLogsMsg) -> <FlushLogsMsg as ssf::Message>::Reply {
        self.with_mut_inner(|inner| inner.log_file_control.rotate_unless_empty().map(|_| ()))
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<GetQueuedLogsMsg> for LogCollector<H> {
    fn deliver(&mut self, _m: GetQueuedLogsMsg) -> <GetQueuedLogsMsg as ssf::Message>::Reply {
        let logs = self.with_mut_inner(|inner| inner.get_log_queue())?;

        Ok(logs)
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<GetLatestLogTimestampMsg> for LogCollector<H> {
    fn deliver(
        &mut self,
        _m: GetLatestLogTimestampMsg,
    ) -> <GetLatestLogTimestampMsg as ssf::Message>::Reply {
        let log_ts = self.with_mut_inner(|inner| {
            inner
                .get_latest_log_timestamp()
                .ok_or(eyre!("Couldn't get latest log timestamp"))
        })?;

        Ok(log_ts)
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<RotateIfNeededMsg> for LogCollector<H> {
    fn deliver(&mut self, _m: RotateIfNeededMsg) -> <RotateIfNeededMsg as ssf::Message>::Reply {
        self.with_mut_inner(|inner| inner.rotate_if_needed())
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<LogEntryMsg> for LogCollector<H> {
    fn deliver(&mut self, m: LogEntryMsg) -> <LogEntryMsg as ssf::Message>::Reply {
        if m.dropped_msg_count > 0 {
            warn!("Dropped {} log messages", m.dropped_msg_count);
        }
        self.with_mut_inner(|inner| inner.process_log_record(m.entry))
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<RecoverLogsMsg> for LogCollector<H> {
    fn deliver(&mut self, _m: RecoverLogsMsg) -> <RecoverLogsMsg as ssf::Message>::Reply {
        self.with_mut_inner(|inner| inner.log_file_control.recover_logs())
    }
}

impl<H: HeadroomCheck + Send + 'static> Handler<DeviceConfigUpdateMessage> for LogCollector<H> {
    fn deliver(
        &mut self,
        m: DeviceConfigUpdateMessage,
    ) -> <DeviceConfigUpdateMessage as ssf::Message>::Reply {
        let _ = self.with_mut_inner(|inner| {
            inner.device_config = m.config;
            Ok(())
        });
    }
}

/// The log collector keeps one Inner struct behind a Arc<Mutex<>> so it can be
/// shared by multiple threads.
struct Inner<H: HeadroomCheck> {
    rate_limiter: RateLimiter<DateTime<Utc>>,
    log_file_control: LogFileControlImpl,
    headroom_limiter: H,
    log_filter: LogFilter,
    log_queue: CircularQueue<LogEntry>,
    storage_config: StorageConfig,
    level_mapper: Option<LogLevelMapper>,
    device_config: Arc<DeviceConfig>,
}

impl<H: HeadroomCheck> Inner<H> {
    // Process one log record - To call this, the caller must have acquired a
    // mutex on the Inner object.
    // Be careful to not try to acquire other mutexes here to avoid a
    // dead-lock. Everything we need should be in Inner.
    fn process_log_record(&mut self, mut log: LogEntry) -> Result<()> {
        if let Some(level_mapper) = &self.level_mapper.as_mut() {
            level_mapper.map_log(&mut log)?;
        }

        if let Some(log) = self
            .log_filter
            .apply_rules(log, self.device_config.logging.as_ref())
        {
            if !self
                .headroom_limiter
                .check(&log.ts, &mut self.log_file_control)?
            {
                return Ok(());
            }
            self.log_queue.push(log.clone());

            // Return early and do not write a log message to file if not persisting
            if !self.should_persist() {
                return Ok(());
            }

            // Rotate before writing (in case log file is now too old)
            self.log_file_control.rotate_if_needed()?;

            let logfile = self.log_file_control.current_log()?;
            self.rate_limiter
                .run_within_limits(log.ts, |rate_limited_calls| {
                    // Print a message if some previous calls were rate limited.
                    if let Some(limited) = rate_limited_calls {
                        logfile.write_log(
                            limited.latest_call,
                            "WARN",
                            format!("Memfaultd rate limited {} messages.", limited.count),
                        )?;
                    }
                    logfile.write_json_line(log)?;
                    Ok(())
                })?;

            // Rotate after writing (in case log file is now too large)
            self.log_file_control.rotate_if_needed()?;
        };
        Ok(())
    }

    fn should_persist(&mut self) -> bool {
        matches!(self.storage_config, StorageConfig::Persist)
            || matches!(self.logging_resolution(), Resolution::Normal)
    }

    pub fn get_log_queue(&mut self) -> Result<Vec<String>> {
        let logs = self
            .log_queue
            .iter()
            .map(serde_json::to_string)
            .collect::<Result<Vec<String>, _>>()?;

        Ok(logs)
    }

    fn get_latest_log_timestamp(&self) -> Option<DateTime<Utc>> {
        self.log_queue.back().map(|log_entry| log_entry.ts)
    }

    fn rotate_if_needed(&mut self) -> Result<bool> {
        self.log_file_control.rotate_if_needed()
    }

    fn logging_resolution(&self) -> Resolution {
        self.device_config.sampling.logging_resolution
    }
}

pub struct LogCollectorConfig {
    /// Folder where to store logfiles while they are being written
    pub log_tmp_path: PathBuf,

    /// Files will be rotated when they reach this size (so they may be slightly larger)
    log_max_size: usize,

    /// MAR entry will be rotated when they get this old.
    log_max_duration: Duration,

    /// Compression level to use for compressing the logs.
    log_compression_level: Compression,

    /// Maximum number of lines written per second continuously
    max_lines_per_minute: NonZeroU32,

    /// Rules to convert logs to metrics
    log_to_metrics_rules: Vec<LogToMetricRule>,

    /// Maximum number of lines to keep in memory
    in_memory_lines: usize,

    /// Whether or not to persist log lines
    storage_config: StorageConfig,

    level_mapping_config: LevelMappingConfig,

    log_filter_config: LogFilterConfig,
}

impl From<&Config> for LogCollectorConfig {
    fn from(config: &Config) -> Self {
        Self {
            log_tmp_path: config.logs_path(),
            log_max_size: config.config_file.logs.rotate_size,
            log_max_duration: config.config_file.logs.rotate_after,
            log_compression_level: config.config_file.logs.compression_level,
            max_lines_per_minute: config.config_file.logs.max_lines_per_minute,
            log_to_metrics_rules: config
                .config_file
                .logs
                .log_to_metrics
                .as_ref()
                .map(|c| c.rules.clone())
                .unwrap_or_default(),
            in_memory_lines: config.config_file.coredump.log_lines,
            storage_config: config.config_file.logs.storage,
            level_mapping_config: config.config_file.logs.level_mapping.clone(),
            log_filter_config: config
                .config_file
                .logs
                .filtering
                .as_ref()
                .cloned()
                .unwrap_or_default(),
        }
    }
}

#[derive(Clone)]
pub struct LogEntrySender {
    sender: MsgMailbox<LogEntryMsg>,
    dropped_msg_count: Arc<AtomicUsize>,
}

impl LogEntrySender {
    pub fn new(sender: MsgMailbox<LogEntryMsg>) -> Self {
        Self {
            sender,
            dropped_msg_count: Arc::new(AtomicUsize::new(0)),
        }
    }

    pub fn send_entry(&self, entry: LogEntry) -> Result<()> {
        let log_entry_msg = LogEntryMsg::new(entry, self.dropped_msg_count.load(Ordering::Relaxed));

        match self.sender.send_and_forget(log_entry_msg) {
            Ok(_) => self.dropped_msg_count.store(0, Ordering::Relaxed),
            Err(e) => match e {
                ssf::MailboxError::SendChannelClosed => {
                    return Err(eyre!("Journald channel dropped: {}", e));
                }
                ssf::MailboxError::NoResponse => {
                    return Err(eyre!("Unexpected service response"));
                }
                ssf::MailboxError::SendChannelFull => {
                    self.dropped_msg_count.fetch_add(1, Ordering::Relaxed);
                }
            },
        }

        Ok(())
    }
}

#[derive(Debug, Serialize, Deserialize)]
/// A list of crash logs.
///
/// This structure is passed to the client when they request the crash logs.
pub struct CrashLogs {
    pub logs: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LatestLogTimestamp {
    pub ts: DateTime<Utc>,
}

/// A handler for the /api/v1/crash-logs endpoint.
pub struct CrashLogHandler {
    get_queued_logs_mbox: MsgMailbox<GetQueuedLogsMsg>,
    get_latest_log_ts_mbox: MsgMailbox<GetLatestLogTimestampMsg>,
}

impl CrashLogHandler {
    /// Timeout for delay on waiting for log_collector to "catch up" to the
    /// time of the crash before returning logs in the /api/v1/crash-logs
    /// endpoint
    pub const CRASH_LOGS_DELAY_TIMEOUT: Duration = Duration::from_millis(250);

    pub fn new(
        get_queued_logs_mbox: MsgMailbox<GetQueuedLogsMsg>,
        get_latest_log_ts_mbox: MsgMailbox<GetLatestLogTimestampMsg>,
    ) -> Self {
        Self {
            get_queued_logs_mbox,
            get_latest_log_ts_mbox,
        }
    }

    /// Handle a GET request to /api/v1/crash-logs
    ///
    /// Will take a snapshot of the current circular queue and return it as a JSON array.
    fn handle_get_crash_logs(
        &self,
        time_of_crash: DateTime<Utc>,
        crash_logs_delay_timeout: Duration,
    ) -> Result<ResponseBox> {
        let mut latest_log_timestamp = self
            .get_latest_log_ts_mbox
            .send_and_wait_for_reply(GetLatestLogTimestampMsg)?;
        let crash_logs_delay_start = Instant::now();

        // Try to wait for the log queue in log_collector to "catch up" to the time
        // of the crash to ensure all logs leading up to crash are captured
        while latest_log_timestamp.is_ok_and(|log_ts| log_ts < time_of_crash)
            && crash_logs_delay_start.elapsed() < crash_logs_delay_timeout
        {
            latest_log_timestamp = self
                .get_latest_log_ts_mbox
                .send_and_wait_for_reply(GetLatestLogTimestampMsg)?;

            // Sleep to avoid busy-waiting
            sleep(Duration::from_millis(50));
        }

        let logs = self
            .get_queued_logs_mbox
            .send_and_wait_for_reply(GetQueuedLogsMsg)??;

        let crash_logs = CrashLogs { logs };

        let serialized_logs = serde_json::to_string(&crash_logs)?;
        let logs_len = serialized_logs.len();
        Ok(Response::new(
            StatusCode(200),
            vec![Header::from_strings("Content-Type", "application/json")?],
            Cursor::new(serialized_logs),
            Some(logs_len),
            None,
        )
        .boxed())
    }
}
impl HttpHandler for CrashLogHandler {
    fn handle_request(&self, request: &mut Request) -> HttpHandlerResult {
        let url = request.url();
        let base_url = url.split('?').next().unwrap_or(url);

        if base_url != CRASH_LOGS_URL {
            return HttpHandlerResult::NotHandled;
        }

        if *request.method() != Method::Get {
            return HttpHandlerResult::Response(Response::empty(405).boxed());
        }

        let query_params = parse_query_params(url);

        let time_of_crash = match query_params.get(CRASH_LOGS_CRASH_TS_PARAM) {
            Some(crash_timestamp_str) => crash_timestamp_str
                .parse::<DateTime<Utc>>()
                .unwrap_or(Utc::now()),
            None => Utc::now(),
        };

        self.handle_get_crash_logs(time_of_crash, Self::CRASH_LOGS_DELAY_TIMEOUT)
            .into()
    }
}

#[cfg(test)]
mod tests {
    use std::fs::remove_file;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::mpsc::{channel, Receiver};
    use std::sync::Arc;
    use std::{cmp::min, sync::Mutex};
    use std::{io::Write, path::PathBuf, time::Duration};
    use std::{mem::replace, num::NonZeroU32};

    use crate::{
        config::LevelMappingConfig,
        logs::{
            completed_log::CompletedLog,
            log_file::{LogFile, LogFileControl},
        },
    };
    use crate::{config::Sampling, test_utils::setup_logger};
    use crate::{logs::headroom::HeadroomCheck, util::circular_queue::CircularQueue};
    use chrono::{DateTime, Duration as ChronoDuration, Utc};
    use eyre::Context;
    use flate2::Compression;
    use rstest::{fixture, rstest};
    use ssf::{ServiceMock, SharedServiceThread};
    use tempfile::{tempdir, TempDir};
    use tiny_http::{Method, TestRequest};
    use uuid::Uuid;

    use super::*;

    const IN_MEMORY_LINES: usize = 100;

    #[rstest]
    fn write_logs_to_disk(mut fixture: LogFixture) {
        fixture.write_log(test_line());
        assert_eq!(fixture.count_log_files(), 1);
        assert_eq!(fixture.on_log_completion_calls(), 0);
    }

    #[rstest]
    #[case(50)]
    #[case(100)]
    #[case(150)]
    fn circular_log_queue(#[case] mut log_count: usize, mut fixture: LogFixture) {
        let delta = ChronoDuration::seconds(1);
        let starting_time_str = "2024-09-11T12:34:56Z";
        let mut time = starting_time_str.parse::<DateTime<Utc>>().unwrap();
        for _ in 0..log_count {
            time = time.checked_add_signed(delta).unwrap();
            let log = LogEntry::new_with_message_and_ts("test", time);
            fixture.write_log(log);
        }

        let log_queue = fixture.get_log_queue();

        // Assert that the last value in the queue has the correct timestamp
        let last_val = log_queue.back().unwrap();
        assert_eq!(last_val.ts, time);

        // Clamp the log_count to the maximum size of the queue
        log_count = min(log_count, IN_MEMORY_LINES);
        assert_eq!(log_queue.len(), log_count);
    }

    #[rstest]
    fn clamp_coredump_log_count(fixture: LogFixture) {
        let config = LogCollectorConfig {
            log_tmp_path: fixture.logs_dir.path().to_owned(),
            log_max_size: 1024,
            log_max_duration: Duration::from_secs(3600),
            log_compression_level: Compression::default(),
            max_lines_per_minute: NonZeroU32::new(1_000).unwrap(),
            log_to_metrics_rules: vec![],
            log_filter_config: LogFilterConfig::default(),
            in_memory_lines: 1000,
            storage_config: StorageConfig::Persist,
            level_mapping_config: LevelMappingConfig {
                enable: false,
                regex: None,
            },
        };

        let device_config = Arc::new(DeviceConfig::default());

        let mut collector = LogCollector::open(
            config,
            |CompletedLog { path, .. }| {
                remove_file(&path)
                    .with_context(|| format!("rm {path:?}"))
                    .unwrap();
                Ok(())
            },
            StubHeadroomLimiter,
            ServiceMock::new().mbox,
            device_config,
        )
        .unwrap();

        let log_queue = collector
            .with_mut_inner(|inner| Ok(replace(&mut inner.log_queue, CircularQueue::new(1000))))
            .unwrap();

        // The log queue should be clamped to the maximum size
        assert_eq!(
            log_queue.capacity(),
            LogCollector::<StubHeadroomLimiter>::MAX_IN_MEMORY_LINES
        );
    }

    #[rstest]
    fn do_not_create_newfile_on_close(mut fixture: LogFixture) {
        fixture.write_log(test_line());
        fixture
            .collector
            .lock()
            .unwrap()
            .close_internal()
            .expect("error closing");
        // 0 because the fixture "on_log_completion" moves the file out
        assert_eq!(fixture.count_log_files(), 0);
        assert_eq!(fixture.on_log_completion_calls(), 1);
    }

    #[rstest]
    #[case(StorageConfig::Persist, 60)]
    #[case(StorageConfig::Disabled, 0)]
    fn log_persistence(
        #[case] storage_config: StorageConfig,
        #[case] expected_size: usize,
        mut fixture: LogFixture,
        _setup_logger: (),
    ) {
        fixture.set_log_config(storage_config);

        fixture.write_log(test_line());
        fixture.flush_log_writes().unwrap();

        assert_eq!(fixture.count_log_files(), 1);
        assert_eq!(fixture.read_log_len(), expected_size);
    }

    #[rstest]
    fn forced_rotation_with_nonempty_log(mut fixture: LogFixture) {
        fixture.write_log(test_line());

        fixture
            .service
            .mbox()
            .send_and_wait_for_reply(FlushLogsMsg)
            .unwrap()
            .unwrap();

        assert_eq!(fixture.count_log_files(), 0);
        assert_eq!(fixture.on_log_completion_calls(), 1);
    }

    #[rstest]
    fn delete_log_after_failed_on_completion_callback(mut fixture: LogFixture) {
        fixture
            .on_completion_should_fail
            .store(true, Ordering::Relaxed);
        fixture.write_log(test_line());

        fixture
            .service
            .mbox()
            .send_and_wait_for_reply(FlushLogsMsg)
            .unwrap()
            .unwrap();

        assert_eq!(fixture.on_log_completion_calls(), 1);

        // The old log should have been deleted, to avoid accumulating logs that fail to be moved.
        // No new file will be created without a subsequent write
        assert_eq!(fixture.count_log_files(), 0);
    }

    #[rstest]
    fn forced_rotation_with_empty_log(fixture: LogFixture) {
        fixture
            .service
            .mbox()
            .send_and_wait_for_reply(FlushLogsMsg)
            .unwrap()
            .unwrap();

        assert_eq!(fixture.count_log_files(), 0);
        assert_eq!(fixture.on_log_completion_calls(), 0);
    }

    #[rstest]
    fn forced_rotation_with_write_after_rotate(mut fixture: LogFixture) {
        fixture.write_log(test_line());
        fixture
            .service
            .mbox()
            .send_and_wait_for_reply(FlushLogsMsg)
            .unwrap()
            .unwrap();

        fixture.write_log(test_line());
        assert_eq!(fixture.count_log_files(), 1);
        assert_eq!(fixture.on_log_completion_calls(), 1);
    }

    #[rstest]
    fn recover_old_logfiles() {
        let (tmp_logs, _old_file_path) = existing_tmplogs_with_log(&(Uuid::new_v4().to_string()));
        let fixture = collector_with_logs_dir(tmp_logs);

        let mbox = fixture.service.mbox();
        mbox.send_and_wait_for_reply(RecoverLogsMsg)
            .unwrap()
            .unwrap();

        // We should have generated a MAR entry for the pre-existing logfile.
        assert_eq!(fixture.on_log_completion_calls(), 1);
    }

    #[rstest]
    fn recover_old_logfiles_on_entry() {
        let (tmp_logs, _old_file_path) = existing_tmplogs_with_log(&(Uuid::new_v4().to_string()));
        let fixture = collector_with_logs_dir(tmp_logs);

        let mbox = fixture.service.mbox();
        let entry = LogEntry::new_with_message("test");
        let entry_msg = LogEntryMsg::new(entry, 0);
        mbox.send_and_wait_for_reply(entry_msg).unwrap().unwrap();

        // We should have generated a MAR entry for the pre-existing logfile.
        assert_eq!(fixture.on_log_completion_calls(), 1);
    }

    #[rstest]
    fn delete_files_that_are_not_uuids() {
        let (tmp_logs, old_file_path) = existing_tmplogs_with_log("testfile");
        let fixture = collector_with_logs_dir(tmp_logs);

        let mbox = fixture.service.mbox();
        mbox.send_and_wait_for_reply(RecoverLogsMsg)
            .unwrap()
            .unwrap();

        // And we should have removed the bogus file
        assert!(!old_file_path.exists());

        // We should NOT have generated a MAR entry for the pre-existing bogus file.
        assert_eq!(fixture.on_log_completion_calls(), 0);
    }

    #[rstest]
    fn http_handler_log_get(mut fixture: LogFixture) {
        let date_str = "2024-09-11T12:34:56Z";
        let logs = vec![
            LogEntry::new_with_message_and_ts("xxx", date_str.parse::<DateTime<Utc>>().unwrap()),
            LogEntry::new_with_message_and_ts("yyy", date_str.parse::<DateTime<Utc>>().unwrap()),
            LogEntry::new_with_message_and_ts("zzz", date_str.parse::<DateTime<Utc>>().unwrap()),
        ];
        let log_strings = logs
            .iter()
            .map(|l| serde_json::to_string(l).unwrap())
            .collect::<Vec<_>>();

        for log in &logs {
            fixture.write_log(log.clone());
        }

        let handler =
            CrashLogHandler::new(fixture.service.mbox().into(), fixture.service.mbox().into());

        // This should timeout because the crash "happened" one second after the last log
        // message and there are no further logs and there is a delay timeout of 0 seconds
        let log_response = handler
            .handle_get_crash_logs(
                date_str.parse::<DateTime<Utc>>().unwrap() + Duration::from_secs(1),
                Duration::from_secs(0),
            )
            .unwrap();
        let mut log_response_string = String::new();
        log_response
            .into_reader()
            .read_to_string(&mut log_response_string)
            .unwrap();

        let crash_logs: CrashLogs = serde_json::from_str(&log_response_string).unwrap();
        assert_eq!(crash_logs.logs, log_strings);
    }

    #[rstest]
    #[case(Method::Post)]
    #[case(Method::Put)]
    #[case(Method::Delete)]
    #[case(Method::Patch)]
    fn http_handler_unsupported_method(fixture: LogFixture, #[case] method: Method) {
        let handler =
            CrashLogHandler::new(fixture.service.mbox().into(), fixture.service.mbox().into());

        let request = TestRequest::new()
            .with_path(CRASH_LOGS_URL)
            .with_method(method);
        let response = handler
            .handle_request(&mut request.into())
            .expect("Error handling request");
        assert_eq!(response.status_code().0, 405);
    }

    #[rstest]
    fn unhandled_url(fixture: LogFixture) {
        let handler =
            CrashLogHandler::new(fixture.service.mbox().into(), fixture.service.mbox().into());

        let request = TestRequest::new().with_path("/api/v1/other");
        let response = handler.handle_request(&mut request.into());
        assert!(matches!(response, HttpHandlerResult::NotHandled));
    }

    #[rstest]
    fn entry_sender_fail_counter_inc() {
        let mut service = ServiceMock::new_bounded(1);
        let sender = LogEntrySender::new(service.mbox.clone());
        let entry = LogEntry::new_with_message("Test");

        sender.send_entry(entry.clone()).unwrap();
        sender.send_entry(entry.clone()).unwrap();

        let messages = service.take_messages();

        // We should only have 1 entry at this point
        assert_eq!(messages.len(), 1);

        sender.send_entry(entry.clone()).unwrap();

        let messages = service.take_messages();

        // Verify that we have 1 message dropped
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].dropped_msg_count, 1);

        sender.send_entry(entry).unwrap();

        let messages = service.take_messages();

        // Verify that we now have no messages dropped
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].dropped_msg_count, 0);
    }

    fn existing_tmplogs_with_log(filename: &str) -> (TempDir, PathBuf) {
        let tmp_logs = tempdir().unwrap();
        let file_path = tmp_logs
            .path()
            .to_path_buf()
            .join(filename)
            .with_extension("log.zlib");

        let mut file = std::fs::File::create(&file_path).unwrap();
        file.write_all(b"some content in the log").unwrap();
        drop(file);
        (tmp_logs, file_path)
    }

    #[rstest]
    fn device_config_update_message_handler(_setup_logger: ()) {
        let temp_dir = tempdir().expect("Unable to create temp dir");
        let log_config = LogCollectorConfig {
            log_tmp_path: temp_dir.path().to_owned(),
            log_max_size: 1024,
            log_max_duration: Duration::from_secs(3600),
            log_compression_level: Compression::default(),
            max_lines_per_minute: NonZeroU32::new(1_000).unwrap(),
            log_to_metrics_rules: vec![],
            log_filter_config: LogFilterConfig::default(),
            in_memory_lines: IN_MEMORY_LINES,
            storage_config: StorageConfig::Disabled, // Use Disabled to test resolution logic
            level_mapping_config: LevelMappingConfig {
                enable: false,
                regex: None,
            },
        };

        // Create initial device config with Normal resolution
        let initial_config = DeviceConfig {
            revision: None,
            sampling: Sampling {
                debugging_resolution: Resolution::Normal,
                logging_resolution: Resolution::Normal,
                monitoring_resolution: Resolution::Normal,
            },
            data_upload_start_date: None,
            logging: None,
        };

        let mut collector = LogCollector::open(
            log_config,
            |CompletedLog { path, .. }| {
                remove_file(&path)
                    .with_context(|| format!("rm {path:?}"))
                    .unwrap();
                Ok(())
            },
            StubHeadroomLimiter,
            ServiceMock::new().mbox,
            Arc::new(initial_config),
        )
        .unwrap();

        // Verify initial state - should persist logs with Normal resolution
        let should_persist = collector
            .with_mut_inner(|inner| Ok(inner.should_persist()))
            .unwrap();
        assert!(should_persist, "Should persist logs with Normal resolution");

        // Create updated config with Off resolution
        let updated_config = DeviceConfig {
            revision: None,
            sampling: Sampling {
                debugging_resolution: Resolution::Off,
                logging_resolution: Resolution::Off,
                monitoring_resolution: Resolution::Off,
            },
            data_upload_start_date: None,
            logging: None,
        };

        let update_message = DeviceConfigUpdateMessage {
            config: Arc::new(updated_config),
        };

        // Send the device config update message
        use ssf::Handler;
        collector.deliver(update_message);

        // Verify the config was updated - should not persist logs with Off resolution
        let should_persist_after_update = collector
            .with_mut_inner(|inner| Ok(inner.should_persist()))
            .unwrap();
        assert!(
            !should_persist_after_update,
            "Should not persist logs with Off resolution"
        );

        // Test with High resolution
        let high_resolution_config = DeviceConfig {
            revision: None,
            sampling: Sampling {
                debugging_resolution: Resolution::High,
                logging_resolution: Resolution::High,
                monitoring_resolution: Resolution::High,
            },
            data_upload_start_date: None,
            logging: None,
        };

        let high_resolution_message = DeviceConfigUpdateMessage {
            config: Arc::new(high_resolution_config),
        };

        collector.deliver(high_resolution_message);

        // Verify High resolution does NOT enable persistence (only Normal does)
        let should_persist_high = collector
            .with_mut_inner(|inner| Ok(inner.should_persist()))
            .unwrap();
        assert!(
            !should_persist_high,
            "Should NOT persist logs with High resolution (only Normal enables persistence)"
        );

        // Test with Normal resolution to verify it does enable persistence
        let normal_resolution_config = DeviceConfig {
            revision: None,
            sampling: Sampling {
                debugging_resolution: Resolution::Normal,
                logging_resolution: Resolution::Normal,
                monitoring_resolution: Resolution::Normal,
            },
            data_upload_start_date: None,
            logging: None,
        };

        let normal_resolution_message = DeviceConfigUpdateMessage {
            config: Arc::new(normal_resolution_config),
        };

        collector.deliver(normal_resolution_message);

        // Verify Normal resolution enables persistence
        let should_persist_normal = collector
            .with_mut_inner(|inner| Ok(inner.should_persist()))
            .unwrap();
        assert!(
            should_persist_normal,
            "Should persist logs with Normal resolution"
        );

        // Verify the device config reference is shared (Arc should be the same)
        let config_arc = collector
            .with_mut_inner(|inner| Ok(inner.device_config.clone()))
            .unwrap();
        assert_eq!(
            config_arc.sampling.logging_resolution,
            Resolution::Normal,
            "Device config should be updated to Normal resolution"
        );
    }

    struct LogFixture {
        collector: Arc<Mutex<LogCollector<StubHeadroomLimiter>>>,
        service: SharedServiceThread<LogCollector<StubHeadroomLimiter>>,
        // TempDir needs to be after the collector, otherwise we fail to delete
        // the file in LogCollector::Drop because the tempdir is gone
        logs_dir: TempDir,
        on_log_completion_receiver: Receiver<(PathBuf, Uuid)>,
        on_completion_should_fail: Arc<AtomicBool>,
    }
    impl LogFixture {
        fn count_log_files(&self) -> usize {
            std::fs::read_dir(&self.logs_dir).unwrap().count()
        }

        fn write_log(&mut self, line: LogEntry) {
            self.collector
                .lock()
                .unwrap()
                .with_mut_inner(|inner| inner.process_log_record(line))
                .unwrap();
        }

        fn read_log_len(&mut self) -> usize {
            self.collector
                .lock()
                .unwrap()
                .with_mut_inner(|inner| {
                    let log = inner.log_file_control.current_log()?;
                    Ok(log.bytes_written())
                })
                .unwrap()
        }

        fn flush_log_writes(&mut self) -> Result<()> {
            self.collector
                .lock()
                .unwrap()
                .with_mut_inner(|inner| inner.log_file_control.current_log()?.flush())
        }

        fn on_log_completion_calls(&self) -> usize {
            self.on_log_completion_receiver.try_iter().count()
        }

        fn get_log_queue(&mut self) -> CircularQueue<LogEntry> {
            self.collector
                .lock()
                .unwrap()
                .with_mut_inner(|inner| Ok(replace(&mut inner.log_queue, CircularQueue::new(100))))
                .unwrap()
        }

        fn set_log_config(&mut self, storage_config: StorageConfig) {
            self.collector
                .lock()
                .unwrap()
                .with_mut_inner(|inner| {
                    inner.storage_config = storage_config;
                    Ok(())
                })
                .unwrap()
        }
    }

    #[fixture]
    fn fixture() -> LogFixture {
        collector_with_logs_dir(tempdir().unwrap())
    }

    struct StubHeadroomLimiter;

    impl HeadroomCheck for StubHeadroomLimiter {
        fn check<L: LogFile>(
            &mut self,
            _log_timestamp: &DateTime<Utc>,
            _log_file_control: &mut impl LogFileControl<L>,
        ) -> eyre::Result<bool> {
            Ok(true)
        }
    }

    fn collector_with_logs_dir(logs_dir: TempDir) -> LogFixture {
        let config = LogCollectorConfig {
            log_tmp_path: logs_dir.path().to_owned(),
            log_max_size: 1024,
            log_max_duration: Duration::from_secs(3600),
            log_compression_level: Compression::default(),
            max_lines_per_minute: NonZeroU32::new(1_000).unwrap(),
            log_to_metrics_rules: vec![],
            in_memory_lines: IN_MEMORY_LINES,
            log_filter_config: LogFilterConfig::default(),
            storage_config: StorageConfig::Persist,
            level_mapping_config: LevelMappingConfig {
                enable: false,
                regex: None,
            },
        };

        let (on_log_completion_sender, on_log_completion_receiver) = channel();

        let on_completion_should_fail = Arc::new(AtomicBool::new(false));

        let device_config = Arc::new(DeviceConfig::default());
        let collector = {
            let on_completion_should_fail = on_completion_should_fail.clone();
            let on_log_completion = move |CompletedLog { path, cid, .. }| {
                on_log_completion_sender.send((path.clone(), cid)).unwrap();
                if on_completion_should_fail.load(Ordering::Relaxed) {
                    // Don't move / unlink the log file. The LogCollector should clean up now.
                    Err(eyre::eyre!("on_log_completion failure!"))
                } else {
                    // Unlink the log file. The real implementation moves it into the MAR staging area.
                    remove_file(&path)
                        .with_context(|| format!("rm {path:?}"))
                        .unwrap();
                    Ok(())
                }
            };

            LogCollector::open(
                config,
                on_log_completion,
                StubHeadroomLimiter,
                ServiceMock::new().mbox,
                device_config,
            )
            .unwrap()
        };

        let log_collector_service = SharedServiceThread::spawn_with(collector);

        LogFixture {
            logs_dir,
            collector: log_collector_service.shared(),
            service: log_collector_service,
            on_log_completion_receiver,
            on_completion_should_fail,
        }
    }

    fn test_line() -> LogEntry {
        let date_str = "2024-09-11T12:34:56Z";
        LogEntry::new_with_message_and_ts("xxx", date_str.parse::<DateTime<Utc>>().unwrap())
    }
}