-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathlogging.rs
More file actions
161 lines (136 loc) · 3.95 KB
/
Copy pathlogging.rs
File metadata and controls
161 lines (136 loc) · 3.95 KB
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
use core::fmt;
use core::sync::atomic::{AtomicBool, Ordering};
use anstyle::AnsiColor;
use log::{Level, LevelFilter, Metadata, Record};
pub static KERNEL_LOGGER: KernelLogger = KernelLogger::new();
/// Data structure to filter kernel messages
pub struct KernelLogger {
time: AtomicBool,
}
impl KernelLogger {
pub const fn new() -> Self {
Self {
time: AtomicBool::new(false),
}
}
pub fn time(&self) -> bool {
self.time.load(Ordering::Relaxed)
}
pub fn set_time(&self, time: bool) {
self.time.store(time, Ordering::Relaxed);
}
}
impl log::Log for KernelLogger {
fn enabled(&self, _: &Metadata<'_>) -> bool {
true
}
fn flush(&self) {
// nothing to do
}
fn log(&self, record: &Record<'_>) {
if !self.enabled(record.metadata()) {
return;
}
// FIXME: Use `super let` once stable
let time;
let format_time = if self.time() {
time = Microseconds(crate::processor::get_timer_ticks());
format_args!("[{time}]")
} else {
format_args!("[ ]")
};
let core_id = crate::arch::core_local::core_id();
let level = ColorLevel(record.level());
let target = record.target();
let (crate_, modules) = target.split_once("::").unwrap_or((target, ""));
let (_modules, module) = modules.rsplit_once("::").unwrap_or(("", modules));
let target = if !module.is_empty() && crate_ == "hermit" {
module
} else {
crate_
};
let format_target = format_args!(" {target:<10}");
let args = record.args();
println!("{format_time}[{core_id}][{level}{format_target}] {args}");
}
}
struct Microseconds(u64);
impl fmt::Display for Microseconds {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let seconds = self.0 / 1_000_000;
let microseconds = self.0 % 1_000_000;
write!(f, "{seconds:5}.{microseconds:06}")
}
}
struct ColorLevel(Level);
impl fmt::Display for ColorLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let level = self.0;
if no_color() {
write!(f, "{level:<5}")
} else {
let color = match level {
Level::Trace => AnsiColor::Magenta,
Level::Debug => AnsiColor::Blue,
Level::Info => AnsiColor::Green,
Level::Warn => AnsiColor::Yellow,
Level::Error => AnsiColor::Red,
};
let style = anstyle::Style::new().fg_color(Some(color.into()));
write!(f, "{style}{level:<5}{style:#}")
}
}
}
fn no_color() -> bool {
hermit_var!("NO_COLOR").is_some_and(|val| !val.is_empty())
}
pub unsafe fn init() {
log::set_logger(&KERNEL_LOGGER).expect("Can't initialize logger");
// Determines LevelFilter at compile time
let log_level = hermit_var!("HERMIT_LOG_LEVEL_FILTER");
let mut max_level = LevelFilter::Info;
if let Some(log_level) = log_level {
max_level = if log_level.eq_ignore_ascii_case("off") {
LevelFilter::Off
} else if log_level.eq_ignore_ascii_case("error") {
LevelFilter::Error
} else if log_level.eq_ignore_ascii_case("warn") {
LevelFilter::Warn
} else if log_level.eq_ignore_ascii_case("info") {
LevelFilter::Info
} else if log_level.eq_ignore_ascii_case("debug") {
LevelFilter::Debug
} else if log_level.eq_ignore_ascii_case("trace") {
LevelFilter::Trace
} else {
error!("Could not parse HERMIT_LOG_LEVEL_FILTER, falling back to `info`.");
LevelFilter::Info
};
}
log::set_max_level(max_level);
}
#[cfg_attr(target_arch = "riscv64", allow(unused_macros))]
macro_rules! infoheader {
// This should work on paper, but it's currently not supported :(
// Refer to https://github.com/rust-lang/rust/issues/46569
/*($($arg:tt)+) => ({
info!("");
info!("{:=^70}", format_args!($($arg)+));
});*/
($str:expr) => {{
::log::info!("");
::log::info!("{:=^70}", $str);
}};
}
#[cfg_attr(target_arch = "riscv64", allow(unused_macros))]
#[clippy::format_args]
macro_rules! infoentry {
($str:expr, $($arg:tt)+) => (::log::info!("{:25}{}", concat!($str, ":"), format_args!($($arg)+)));
}
#[cfg_attr(target_arch = "riscv64", allow(unused_macros))]
macro_rules! infofooter {
() => {{
::log::info!("{:=^70}", '=');
::log::info!("");
}};
}