Unnamed repository; edit this file 'description' to name the repository.
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
//! Logging support for `hx`.

use std::io::Write;
use std::path::Path;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

/// Minimal `log::Log` implementation: a level filter plus a single line-buffered sink.
struct Logger {
    level: log::LevelFilter,
    sink: Mutex<Box<dyn Write + Send>>,
}

impl log::Log for Logger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        metadata.level() <= self.level
    }

    fn log(&self, record: &log::Record) {
        if !self.enabled(record.metadata()) {
            return;
        }
        if let Ok(mut sink) = self.sink.lock() {
            let _ = writeln!(
                sink,
                "{} {} [{}] {}",
                log_timestamp(),
                record.target(),
                record.level(),
                record.args()
            );
        }
    }

    fn flush(&self) {
        if let Ok(mut sink) = self.sink.lock() {
            let _ = sink.flush();
        }
    }
}

fn install(
    level: log::LevelFilter,
    sink: Box<dyn Write + Send>,
) -> Result<(), log::SetLoggerError> {
    log::set_boxed_logger(Box::new(Logger {
        level,
        sink: Mutex::new(sink),
    }))?;
    log::set_max_level(level);
    Ok(())
}

/// Install the global logger writing to `path` (created if absent, appended to).
pub fn init_file(level: log::LevelFilter, path: &Path) -> std::io::Result<()> {
    let file = std::fs::OpenOptions::new()
        .append(true)
        .create(true)
        .open(path)?;
    install(level, Box::new(file)).map_err(std::io::Error::other)
}

/// Install the global logger writing to stdout (used by integration tests).
#[cfg(feature = "integration")]
pub fn init_stdout(level: log::LevelFilter) {
    let _ = install(level, Box::new(std::io::stdout()));
}

/// RFC3339-style UTC timestamp for a log line: `YYYY-MM-DDTHH:MM:SS.mmm`.
pub fn log_timestamp() -> String {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    format_timestamp(now.as_secs(), now.subsec_millis())
}

fn format_timestamp(secs: u64, millis: u32) -> String {
    let days = (secs / 86_400) as i64;
    let tod = secs % 86_400;
    let (hour, min, sec) = (tod / 3600, (tod % 3600) / 60, tod % 60);
    let (year, month, day) = civil_from_days(days);

    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{min:02}:{sec:02}.{millis:03}")
}

/// Howard Hinnant's `civil_from_days`: days since the Unix epoch (1970-01-01,
/// UTC) to `(year, month, day)`. Exact for the whole proleptic Gregorian range.
fn civil_from_days(z: i64) -> (i64, u32, u32) {
    let z = z + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097; // [0, 146096]
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
    let year = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
    let mp = (5 * doy + 2) / 153; // [0, 11]
    let day = (doy - (153 * mp + 2) / 5 + 1) as u32; // [1, 31]
    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32; // [1, 12]
    (if month <= 2 { year + 1 } else { year }, month, day)
}

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

    #[test]
    fn timestamp_matches_known_instants() {
        // epoch
        assert_eq!(format_timestamp(0, 0), "1970-01-01T00:00:00.000");
        // next day, and time-of-day + millis
        assert_eq!(
            format_timestamp(86_400 + 3661, 7),
            "1970-01-02T01:01:01.007"
        );
        // a leap day: 2024-02-29T12:30:45.123 UTC == 1709209845 s
        assert_eq!(
            format_timestamp(1_709_209_845, 123),
            "2024-02-29T12:30:45.123"
        );
        // 2000-03-01 (the algorithm's era boundary)
        assert_eq!(format_timestamp(951_868_800, 0), "2000-03-01T00:00:00.000");
    }
}