Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use log::{Level, LevelFilter, Metadata, Record};
9
10pub fn init(lvl: LevelFilter) {
11 static LOGGER: Logger = Logger;
12
13 log::set_logger(&LOGGER).unwrap();
14 log::set_max_level(lvl);
15}
16
17struct Logger;
18
19impl log::Log for Logger {
20 fn enabled(&self, _metadata: &Metadata) -> bool {
21 true
22 }
23
24 fn log(&self, record: &Record) {
25 if self.enabled(record.metadata()) {
26 let color = match record.level() {
27 Level::Trace => "\x1b[36m",
28 Level::Debug => "\x1b[34m",
29 Level::Info => "\x1b[32m",
30 Level::Warn => "\x1b[33m",
31 Level::Error => "\x1b[31;1m",
32 };
33
34 print(format_args!(
35 "[{color}{:<5}\x1b[0m {}] {}\n",
36 record.level(),
37 record.module_path_static().unwrap_or_default(),
38 record.args()
39 ));
40 }
41 }
42
43 fn flush(&self) {}
44}
45
46fn print(args: core::fmt::Arguments) {
47 cfg_if::cfg_if! {
48 if #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))] {
49 riscv::hio::_print(args);
50 } else {
51 compile_error!("unsupported target architecture");
52 }
53 }
54}