Log on tty (serial and vga)

This commit is contained in:
2026-03-22 16:00:41 +01:00
parent 15ecefb5fb
commit f966a1239e
9 changed files with 129 additions and 96 deletions

View File

@@ -2,8 +2,12 @@
//!
//! Provides a lightweight logger implementation routing to UART and helper
//! macros for printing from kernel code.
use crate::println;
use crate::print;
use crate::tty::{TTY_INITIALIZED, TTY0};
use alloc::format;
use io::Write;
use log::{Level, Metadata, Record};
use log::{LevelFilter, SetLoggerError};
@@ -14,11 +18,15 @@ use crate::uart::write_uart;
/// Accepts any type that implements `AsRef<str>` to avoid unnecessary
/// allocations at call sites.
pub(crate) fn print<T: AsRef<str>>(content: T) {
write_uart(content);
if TTY_INITIALIZED.load(core::sync::atomic::Ordering::Relaxed) {
unsafe { TTY0.new_node().write(content.as_ref().as_bytes()).unwrap() };
} else {
write_uart(&content);
}
}
/// Logger implementation that routes kernel log records to the UART-based console.
struct Logger;
pub struct Logger;
impl log::Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
@@ -27,24 +35,25 @@ impl log::Log for Logger {
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
if let Some((file, line)) = record.file().zip(record.line()) {
println!(
"[{}] at {}:{} - {}",
let to_print = if let Some((file, line)) = record.file().zip(record.line()) {
format!(
"[{}] at {}:{} - {}\n",
record.level(),
file,
line,
record.args()
);
)
} else {
println!("[{}] - {}", record.level(), record.args());
}
format!("[{}] - {}\n", record.level(), record.args())
};
print!("{to_print}");
}
}
fn flush(&self) {}
}
static LOGGER: Logger = Logger;
pub static LOGGER: Logger = Logger;
/// Initialize the kernel logger and set the default level.
///
@@ -62,10 +71,9 @@ macro_rules! print {
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n\r")
$crate::print!("\n")
};
($($args:expr),*) => {{
$crate::print!($($args),*);
$crate::println!();
$crate::print!("{}\n", alloc::format!($($args),*));
}};
}