Skip to content

Commit

Permalink
userlib: Add print! and println! support
Browse files Browse the repository at this point in the history
Implement console access and provide the the print! and println!
macros. Use the new macros to print a message in the panic handler.

Signed-off-by: Joerg Roedel <[email protected]>
  • Loading branch information
joergroedel committed Dec 11, 2024
1 parent a0b39e6 commit 7b6bcde
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
45 changes: 45 additions & 0 deletions user/lib/src/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 SUSE LLC
//
// Author: Joerg Roedel <[email protected]>

use crate::SpinLock;
use core::fmt;
use syscall::write_console;

#[derive(Debug, Default)]
struct ConsoleWriter {}

impl ConsoleWriter {
const fn new() -> Self {
Self {}
}
}

impl fmt::Write for ConsoleWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
// Ignore any errors from console writing.
let _ = write_console(s.as_bytes());
Ok(())
}
}

static CONSOLE_WRITER: SpinLock<ConsoleWriter> = SpinLock::new(ConsoleWriter::new());

#[doc(hidden)]
pub fn console_print(args: fmt::Arguments<'_>) {
use core::fmt::Write;
CONSOLE_WRITER.lock().write_fmt(args).unwrap()
}

#[macro_export]
macro_rules! print {
($($arg:tt)*) => (console_print(format_args!($($arg)*)))
}

#[macro_export]
macro_rules! println {
() => (print!("\n"));
($($arg:tt)*) => (print!("{}\n", format_args!($($arg)*)));
}
5 changes: 4 additions & 1 deletion user/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#![no_std]

pub mod console;
pub mod locking;

pub use console::*;
use core::panic::PanicInfo;
pub use locking::*;
pub use syscall::*;
Expand All @@ -27,6 +29,7 @@ macro_rules! declare_main {
}

#[panic_handler]
fn panic(_info: &PanicInfo<'_>) -> ! {
fn panic(info: &PanicInfo<'_>) -> ! {
println!("Panic: {}", info);
exit(!0);
}

0 comments on commit 7b6bcde

Please sign in to comment.