Reimplement print in std

This commit is contained in:
Yuke Peng 2023-11-07 22:53:16 -08:00 committed by Tate, Hongliang Tian
parent 01e485b96e
commit 34e66a51d9
4 changed files with 45 additions and 2 deletions

View File

@ -57,7 +57,8 @@ pub fn init() {
#[cfg(feature = "intel_tdx")]
println!(
"td gpaw: {}, td attributes: {:?}\nTDX guest is initialized",
td_info.gpaw, td_info.attributes
td_info.gpaw,
td_info.attributes
);
vm::heap_allocator::init();
boot::init();

View File

@ -0,0 +1,40 @@
//! `print` and `println` macros
//!
//! FIXME: It will print to all `virtio-console` devices, which is not a good choice.
//!
use core::fmt::{Arguments, Write};
struct VirtioConsolesPrinter;
impl Write for VirtioConsolesPrinter {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for (_, device) in jinux_console::all_devices() {
device.send(s.as_bytes());
}
Ok(())
}
}
pub fn _print(args: Arguments) {
VirtioConsolesPrinter.write_fmt(args).unwrap();
}
/// Copy from Rust std: https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::console::_print(format_args!($($arg)*));
}};
}
/// Copy from Rust std: https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {{
$crate::console::_print(format_args_nl!($($arg)*));
}};
}

View File

@ -18,6 +18,7 @@
#![feature(trait_alias)]
#![feature(register_tool)]
#![feature(trait_upcasting)]
#![feature(format_args_nl)]
#![register_tool(component_access_control)]
use crate::{
@ -40,6 +41,7 @@ extern crate controlled;
#[macro_use]
extern crate ktest;
pub mod console;
pub mod device;
pub mod driver;
pub mod error;

View File

@ -20,7 +20,6 @@ pub(crate) use int_to_c_enum::TryFromInt;
pub(crate) use jinux_frame::config::PAGE_SIZE;
pub(crate) use jinux_frame::sync::{Mutex, MutexGuard, RwLock, SpinLock, SpinLockGuard};
pub(crate) use jinux_frame::vm::Vaddr;
pub(crate) use jinux_frame::{print, println};
pub(crate) use log::{debug, error, info, trace, warn};
pub(crate) use pod::Pod;
@ -43,6 +42,7 @@ macro_rules! current_thread {
pub(crate) use crate::current;
pub(crate) use crate::current_thread;
pub(crate) use crate::error::{Errno, Error};
pub(crate) use crate::{print, println};
pub(crate) use lazy_static::lazy_static;
pub(crate) type Result<T> = core::result::Result<T, Error>;
pub(crate) use crate::{return_errno, return_errno_with_message};