From 93acb481dd90d05eb0e4c8c9ba1d7a2c43dbd195 Mon Sep 17 00:00:00 2001 From: Akira Moroo Date: Fri, 5 Jul 2024 23:32:26 +0000 Subject: [PATCH] build: Add logger Signed-off-by: Akira Moroo --- src/logger.rs | 23 +++++++++++++++++++++++ src/main.rs | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 src/logger.rs diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 00000000..7b9d4903 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (C) 2024 Akira Moroo + +pub struct Logger; + +impl log::Log for Logger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + metadata.level() <= log::Level::Info + } + + fn log(&self, record: &log::Record) { + if self.enabled(record.metadata()) { + log!("[{}] {}", record.level(), record.args()); + } + } + + fn flush(&self) {} +} + +pub fn init() { + log::set_logger(&Logger).expect("Failed to set logger"); + log::set_max_level(log::LevelFilter::Info); +} diff --git a/src/main.rs b/src/main.rs index e97c68c1..f91c6e39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,7 @@ mod fdt; mod integration; mod layout; mod loader; +mod logger; mod mem; mod part; mod pci; @@ -183,6 +184,7 @@ fn boot_from_device( #[no_mangle] pub extern "C" fn rust64_start(#[cfg(not(feature = "coreboot"))] pvh_info: &pvh::StartInfo) -> ! { serial::PORT.borrow_mut().init(); + logger::init(); arch::x86_64::sse::enable_sse(); arch::x86_64::paging::setup(); @@ -204,6 +206,7 @@ pub extern "C" fn rust64_start(x0: *const u8) -> ! { // Use atomic operation before MMU enabled may cause exception, see https://www.ipshop.xyz/5909.html serial::PORT.borrow_mut().init(); + logger::init(); let info = fdt::StartInfo::new( x0, @@ -226,6 +229,7 @@ pub extern "C" fn rust64_start(a0: u64, a1: *const u8) -> ! { use crate::bootinfo::{EntryType, Info, MemoryEntry}; serial::PORT.borrow_mut().init(); + logger::init(); log!("Starting on RV64 0x{:x} 0x{:x}", a0, a1 as u64,);