sel4_logging/
lib.rs

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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]

use core::fmt::{self, Write};

use log::{Log, Metadata, Record, SetLoggerError};

pub use log::{self, LevelFilter};

mod synchronized;

pub use synchronized::SynchronizedLogger;

pub struct Logger {
    pub level_filter: LevelFilter,
    pub filter: fn(&Metadata) -> bool,
    pub fmt: FmtRecordFn,
    pub write: fn(&str),
    pub flush: fn(),
}

pub type FmtRecordFn = fn(&Record, &mut fmt::Formatter) -> fmt::Result;

pub const FMT_RECORD_DEFAULT: FmtRecordFn = fmt_with_module;

impl Logger {
    pub const fn const_default() -> Self {
        Self {
            level_filter: LevelFilter::Warn,
            filter: |_| true,
            fmt: FMT_RECORD_DEFAULT,
            write: |_| (),
            flush: || (),
        }
    }

    pub fn level_filter(&self) -> LevelFilter {
        self.level_filter
    }

    pub fn set_max_level(&self) {
        log::set_max_level(self.level_filter());
    }

    pub fn set(&'static self) -> Result<(), SetLoggerError> {
        self.set_max_level();
        log::set_logger(self)?;
        Ok(())
    }
}

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

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            let mut writer = WriteWrapper(self.write);
            let wrapped = DisplayWrapper {
                fmt: self.fmt,
                record,
            };
            writeln!(writer, "{wrapped}").unwrap()
        }
    }

    fn flush(&self) {
        (self.flush)()
    }
}

//

struct WriteWrapper(fn(&str));

impl fmt::Write for WriteWrapper {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        (self.0)(s);
        Ok(())
    }
}

struct DisplayWrapper<'a> {
    fmt: FmtRecordFn,
    record: &'a Record<'a>,
}

impl fmt::Display for DisplayWrapper<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (self.fmt)(self.record, f)
    }
}

//

pub struct LoggerBuilder(Logger);

impl LoggerBuilder {
    pub const fn const_default() -> Self {
        Self(Logger::const_default())
    }

    pub const fn build(self) -> Logger {
        self.0
    }

    pub const fn level_filter(mut self, level_filter: LevelFilter) -> Self {
        self.0.level_filter = level_filter;
        self
    }

    pub const fn filter(mut self, filter: fn(&Metadata) -> bool) -> Self {
        self.0.filter = filter;
        self
    }

    pub const fn fmt(mut self, fmt: FmtRecordFn) -> Self {
        self.0.fmt = fmt;
        self
    }

    pub const fn write(mut self, write: fn(&str)) -> Self {
        self.0.write = write;
        self
    }

    pub const fn flush(mut self, flush: fn()) -> Self {
        self.0.flush = flush;
        self
    }
}

//

pub fn fmt_with_module(record: &Record, f: &mut fmt::Formatter) -> fmt::Result {
    let target = if !record.target().is_empty() {
        record.target()
    } else {
        record.module_path().unwrap_or_default()
    };
    write!(f, "{:<5} [{}] {}", record.level(), target, record.args())
}

pub fn fmt_with_line(record: &Record, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{:<5} [", record.level())?;
    if let Some(file) = record.file() {
        write!(f, "{file}")?;
    } else if let Some(file) = record.file_static() {
        write!(f, "{file}")?;
    } else {
        write!(f, "(?)")?;
    }
    write!(f, ":")?;
    if let Some(line) = record.line() {
        write!(f, "{line}")?;
    } else {
        write!(f, "(?)")?;
    }
    write!(f, "] {}", record.args())
}