reduct-base 1.21.1

Base crate for ReductStore
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
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use chrono::prelude::{DateTime, Utc};
use log::{Level, Log, Metadata, Record};
use std::collections::BTreeMap;
use std::sync::{Arc, LazyLock, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};
use thread_id;

static LOGGER: Logger = Logger {
    sink: RwLock::new(None),
};

/// A registered log sink: receives an owned [`LogSnapshot`] for every record
/// that reaches the logger. It must be non-blocking and must not panic.
pub type LogSink = Arc<dyn Fn(LogSnapshot) + Send + Sync>;

pub struct Logger {
    /// Optional capture sink, installed via [`Logger::init_with_sink`]. Held as
    /// interior-mutable state on the single static logger rather than as a
    /// separate global.
    sink: RwLock<Option<LogSink>>,
}

static PATHS: LazyLock<RwLock<BTreeMap<String, Level>>> =
    LazyLock::new(|| RwLock::new(BTreeMap::new()));

/// An owned, `Send + 'static` snapshot of a single log record.
///
/// `log::Record` borrows its fields and is neither `Send` nor `'static`, so a
/// sink that wants to process records off-thread must copy what it needs
/// synchronously. This struct uses only types available to `reduct_base`
/// (`log::Level` is from the `log` crate), keeping the hook fully decoupled from
/// any consumer.
#[derive(Clone, Debug)]
pub struct LogSnapshot {
    /// Microseconds since the Unix epoch, captured when the record was logged.
    pub timestamp: u64,
    pub level: Level,
    pub target: String,
    pub file: Option<String>,
    pub line: Option<u32>,
    pub message: String,
}

impl From<&Record<'_>> for LogSnapshot {
    fn from(record: &Record) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_micros() as u64)
            .unwrap_or(0);
        LogSnapshot {
            timestamp,
            level: record.level(),
            target: record.target().to_string(),
            file: record.file().map(|file| file.to_string()),
            line: record.line(),
            message: format!("{}", record.args()),
        }
    }
}

/// Parse a single log level name (case-insensitive) into a [`Level`].
///
/// Returns `None` for unset / `OFF` / invalid input, which callers treat as
/// "disabled". Unlike [`Logger::init`] this parses exactly one level and does
/// not mutate any global logger state.
pub fn parse_log_level(level: &str) -> Option<Level> {
    match level.trim().to_uppercase().as_str() {
        "ERROR" => Some(Level::Error),
        "WARN" => Some(Level::Warn),
        "INFO" => Some(Level::Info),
        "DEBUG" => Some(Level::Debug),
        "TRACE" => Some(Level::Trace),
        _ => None,
    }
}
impl Log for Logger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        let paths = PATHS.read().unwrap();
        // Check paths in reverse order (most specific first)
        for (path, level) in paths.iter().rev() {
            if path.is_empty() {
                return metadata.level() <= *level;
            }
            if metadata.target().replace("::", "/").starts_with(path) {
                return metadata.level() <= *level;
            }
        }
        false
    }

    fn log(&self, record: &Record) {
        // Console output is ALWAYS produced here (subject only to the existing
        // level filter); it is never gated by the capture hook below. This
        // guarantees a log emitted *during* a capture write still reaches stdout.
        if self.enabled(record.metadata()) {
            let now: DateTime<Utc> = Utc::now();

            let file = if let Some(file) = record.file() {
                // Absolute path to crate, remove path to registry
                match file.split_once("src/") {
                    Some((_, file)) => file,
                    None => file,
                }
            } else {
                "(unknown)"
            };

            let package_name = if let Some(package_name) = record.target().split_once(':') {
                package_name.0
            } else {
                record.target()
            };

            let formatted = format!(
                "{} ({:>5}) [{}] -- {:}/{:}:{:} {:?}",
                now.format("%Y-%m-%d %H:%M:%S.%3f"),
                thread_id::get() % 100000,
                record.level(),
                package_name,
                file,
                record.line().unwrap(),
                record.args(),
            );
            println!("{}", formatted);
            #[cfg(test)]
            test_support::record_console(formatted);
        }

        // Optional capture hook (e.g. persisting logs to storage). It runs
        // AFTER console output and is fully abstract: this crate knows nothing
        // about what the sink does. Any reentrancy guarding lives inside the
        // registered sink, never here, so console output is never suppressed.
        let sink = self.sink.read().unwrap().clone();
        if let Some(sink) = sink {
            sink(LogSnapshot::from(record));
        }
    }

    fn flush(&self) {}
}

impl Logger {
    /// Initialize the logger with no capture sink (clearing any previously
    /// installed one).
    ///
    /// # Arguments
    ///
    /// * `level` - The log level to use. Can be one of TRACE, DEBUG, INFO, WARN, ERROR.
    pub fn init(levels: &str) {
        Self::setup(levels);
        *LOGGER.sink.write().unwrap() = None;
    }

    /// Initialize the logger and install a capture [`LogSink`]. The sink is
    /// invoked for every record after console output (see [`Logger::log`]).
    pub fn init_with_sink(levels: &str, sink: LogSink) {
        Self::setup(levels);
        *LOGGER.sink.write().unwrap() = Some(sink);
    }

    fn setup(levels: &str) {
        let mut max_level = Level::Trace;
        {
            let mut paths = PATHS.write().unwrap();
            paths.clear();
            paths.insert("".to_string(), Level::Info); // default level
        }
        for level in levels.split(',') {
            let mut parts = level.splitn(2, '=');
            let mut path = parts.next().unwrap().trim();
            let level = if let Some(lvl) = parts.next() {
                lvl.trim()
            } else {
                // for case INFO,path=DEBUG
                let lvl = path;
                path = ""; // root
                lvl
            };

            let level = match level.to_uppercase().as_str() {
                "TRACE" => Level::Trace,
                "DEBUG" => Level::Debug,
                "INFO" => Level::Info,
                "WARN" => Level::Warn,
                "ERROR" => Level::Error,
                _ => {
                    eprintln!("Invalid log level: {}, defaulting to INFO", level);
                    Level::Info
                }
            };

            max_level = std::cmp::max(max_level, level);
            PATHS.write().unwrap().insert(path.to_string(), level);
        }

        log::set_logger(&LOGGER).ok();
        log::set_max_level(max_level.to_level_filter());
    }
}

/// Test-only capture of console output, so tests can assert that lines actually
/// reached the console branch without trying to redirect real stdout. Not
/// compiled into the library when used as a dependency.
#[cfg(test)]
mod test_support {
    use std::sync::{LazyLock, Mutex};

    static CONSOLE: LazyLock<Mutex<Vec<String>>> = LazyLock::new(|| Mutex::new(Vec::new()));

    pub(super) fn record_console(line: String) {
        CONSOLE.lock().unwrap().push(line);
    }

    pub(super) fn clear_console() {
        CONSOLE.lock().unwrap().clear();
    }

    pub(super) fn captured_console() -> Vec<String> {
        CONSOLE.lock().unwrap().clone()
    }
}

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

    #[test]
    #[serial]
    fn it_works() {
        Logger::init("INFO");
        log::info!("Hello, world!");
    }

    #[test]
    fn parse_log_level_parses_known_levels_and_rejects_others() {
        assert_eq!(parse_log_level("warn"), Some(Level::Warn));
        assert_eq!(parse_log_level(" ERROR "), Some(Level::Error));
        assert_eq!(parse_log_level("Trace"), Some(Level::Trace));
        assert_eq!(parse_log_level("OFF"), None);
        assert_eq!(parse_log_level(""), None);
        assert_eq!(parse_log_level("bogus"), None);
    }

    #[test]
    #[serial]
    fn sink_receives_owned_snapshot() {
        use std::sync::Mutex;
        test_support::clear_console();

        let captured: Arc<Mutex<Vec<LogSnapshot>>> = Arc::new(Mutex::new(Vec::new()));
        let sink_captured = Arc::clone(&captured);
        Logger::init_with_sink(
            "TRACE",
            Arc::new(move |snapshot| {
                sink_captured.lock().unwrap().push(snapshot);
            }),
        );

        log::warn!("hello sink");
        Logger::init("INFO"); // clears the sink

        let snapshots = captured.lock().unwrap();
        assert!(snapshots
            .iter()
            .any(|snap| snap.message == "hello sink" && snap.level == Level::Warn));
    }

    /// Constraint B: a log emitted *during* the capture hook still reaches the
    /// console, because the console `println!` is unconditional and runs before
    /// the (abstract) sink call.
    #[test]
    #[serial]
    fn console_prints_even_when_sink_reenters() {
        use std::sync::atomic::{AtomicBool, Ordering};
        test_support::clear_console();

        // The sink simulates "a log happens during a capture write": the first
        // time it sees the outer message it logs again (once, to stay bounded).
        let reentered = Arc::new(AtomicBool::new(false));
        let guard = Arc::clone(&reentered);
        Logger::init_with_sink(
            "TRACE",
            Arc::new(move |snapshot: LogSnapshot| {
                if snapshot.message == "outer-message" && !guard.swap(true, Ordering::SeqCst) {
                    log::error!("inner-during-capture");
                }
            }),
        );

        log::warn!("outer-message");
        let console = test_support::captured_console();
        Logger::init("INFO"); // clears the sink

        assert!(
            console.iter().any(|line| line.contains("outer-message")),
            "outer log must reach console"
        );
        assert!(
            console
                .iter()
                .any(|line| line.contains("inner-during-capture")),
            "a log emitted during a capture write must still reach console (Constraint B)"
        );
    }

    #[test]
    #[serial]
    fn test_log_levels() {
        Logger::init("DEBUG,path=TRACE,crate/module=ERROR");
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Info)
                    .target("crate")
                    .build()
            ),
            true
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Debug)
                    .target("crate")
                    .build()
            ),
            true
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Trace)
                    .target("crate")
                    .build()
            ),
            false
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Error)
                    .target("crate/module")
                    .build()
            ),
            true
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Warn)
                    .target("crate/module")
                    .build()
            ),
            false
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Info)
                    .target("other")
                    .build()
            ),
            true
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Debug)
                    .target("other")
                    .build()
            ),
            true
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Trace)
                    .target("other")
                    .build()
            ),
            false
        );
    }

    #[test]
    #[serial]
    fn test_log_wrong_level() {
        Logger::init("WRONG");
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Info)
                    .target("crate")
                    .build()
            ),
            true,
            "Default level is INFO"
        );
        assert_eq!(
            LOGGER.enabled(
                &Metadata::builder()
                    .level(Level::Debug)
                    .target("crate")
                    .build()
            ),
            false
        );
    }
}