console.rs 1.8 KB
Newer Older
1 2
use alloc::collections::vec_deque::VecDeque;
use alloc::sync::Arc;
Y
Yifan Wu 已提交
3
use core::fmt::{self, Write};
4
use spin::mutex::Mutex;
5

6 7 8 9
pub const STDIN: usize = 0;
pub const STDOUT: usize = 1;

const CONSOLE_BUFFER_SIZE: usize = 256 * 10;
10 11

use super::{read, write};
12 13 14 15 16 17 18 19 20 21
use lazy_static::*;

struct ConsoleBuffer(VecDeque<u8>);

lazy_static! {
    static ref CONSOLE_BUFFER: Arc<Mutex<ConsoleBuffer>> = {
        let buffer = VecDeque::<u8>::with_capacity(CONSOLE_BUFFER_SIZE);
        Arc::new(Mutex::new(ConsoleBuffer(buffer)))
    };
}
Y
Yifan Wu 已提交
22

23 24 25 26 27 28 29 30
impl ConsoleBuffer {
    fn flush(&mut self) -> isize {
        let s: &[u8] = self.0.make_contiguous();
        let ret = write(STDOUT, s);
        self.0.clear();
        ret
    }
}
Y
Yifan Wu 已提交
31

32
impl Write for ConsoleBuffer {
Y
Yifan Wu 已提交
33
    fn write_str(&mut self, s: &str) -> fmt::Result {
34 35 36 37 38 39
        for c in s.as_bytes().iter() {
            self.0.push_back(*c);
            if (*c == b'\n' || self.0.len() == CONSOLE_BUFFER_SIZE) && -1 == self.flush() {
                return Err(fmt::Error);
            }
        }
Y
Yifan Wu 已提交
40 41 42 43
        Ok(())
    }
}

44
#[allow(unused)]
Y
Yifan Wu 已提交
45
pub fn print(args: fmt::Arguments) {
46 47 48 49
    let mut buf = CONSOLE_BUFFER.lock();
    // buf.write_fmt(args).unwrap();
    // BUG FIX: 关闭 stdout 后,本函数不能触发 panic,否则会造成死锁
    buf.write_fmt(args);
Y
Yifan Wu 已提交
50 51 52 53 54 55 56 57 58 59 60
}

#[macro_export]
macro_rules! print {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!($fmt $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! println {
61 62 63
    () => {
        $crate::console::print(format_args!("\n"));
    };
Y
Yifan Wu 已提交
64 65 66
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
    }
67 68 69 70 71 72 73
}

pub fn getchar() -> u8 {
    let mut c = [0u8; 1];
    read(STDIN, &mut c);
    c[0]
}
74 75 76 77 78

pub fn flush() {
    let mut buf = CONSOLE_BUFFER.lock();
    buf.flush();
}