-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
userlib: Add print! and println! support
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
1 parent
a0b39e6
commit 7b6bcde
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)*))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters