2018-04-04 20:58:23 +04:00
|
|
|
use core::fmt;
|
2018-04-17 15:42:58 +04:00
|
|
|
use arch::driver::serial::COM1;
|
2018-04-04 20:58:23 +04:00
|
|
|
|
|
|
|
mod vga_writer;
|
|
|
|
|
2018-05-19 14:42:08 +04:00
|
|
|
pub fn init() {
|
|
|
|
static LOGGER: SimpleLogger = SimpleLogger;
|
|
|
|
log::set_logger(&LOGGER).unwrap();
|
2018-05-21 21:11:37 +04:00
|
|
|
log::set_max_level(match option_env!("LOG") {
|
|
|
|
Some("off") => LevelFilter::Off,
|
|
|
|
Some("error") => LevelFilter::Error,
|
|
|
|
Some("warn") => LevelFilter::Warn,
|
|
|
|
Some("info") => LevelFilter::Info,
|
|
|
|
Some("trace") => LevelFilter::Trace,
|
|
|
|
Some("debug") | _ => LevelFilter::Debug,
|
|
|
|
});
|
2018-04-07 16:57:14 +04:00
|
|
|
}
|
|
|
|
|
2018-04-04 20:58:23 +04:00
|
|
|
macro_rules! print {
|
|
|
|
($($arg:tt)*) => ({
|
|
|
|
$crate::io::print(format_args!($($arg)*));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! println {
|
|
|
|
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
|
|
|
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
|
|
|
}
|
|
|
|
|
2018-05-19 14:42:08 +04:00
|
|
|
/// Add escape sequence to print with color in Linux console
|
|
|
|
macro_rules! with_color {
|
|
|
|
($args: ident, $color: ident) => {{
|
|
|
|
let (show, code) = color_to_console_code($color);
|
|
|
|
format_args!("{}[{};{}m{}{}[0m", 27 as char, show.clone(), code + 30, $args, 27 as char)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-04-07 16:57:14 +04:00
|
|
|
use arch::driver::vga::Color;
|
|
|
|
|
|
|
|
fn print_in_color(args: fmt::Arguments, color: Color) {
|
2018-04-04 20:58:23 +04:00
|
|
|
use core::fmt::Write;
|
2018-04-18 11:22:06 +04:00
|
|
|
use arch::driver::vga::*;
|
2018-04-25 21:00:32 +04:00
|
|
|
// {
|
|
|
|
// let mut writer = vga_writer::VGA_WRITER.lock();
|
|
|
|
// writer.set_color(color);
|
|
|
|
// writer.write_fmt(args).unwrap();
|
|
|
|
// }
|
2018-04-27 17:49:01 +04:00
|
|
|
// TODO: 解决死锁问题
|
|
|
|
// 若进程在持有锁时被中断,中断处理程序请求输出,就会死锁
|
|
|
|
unsafe{ COM1.force_unlock(); }
|
2018-05-19 14:42:08 +04:00
|
|
|
COM1.lock().write_fmt(with_color!(args, color)).unwrap();
|
2018-04-07 16:57:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print(args: fmt::Arguments) {
|
2018-05-19 14:42:08 +04:00
|
|
|
use core::fmt::Write;
|
|
|
|
unsafe { COM1.force_unlock(); }
|
|
|
|
COM1.lock().write_fmt(args).unwrap();
|
2018-05-12 23:41:41 +04:00
|
|
|
}
|
|
|
|
|
2018-05-19 14:42:08 +04:00
|
|
|
use log;
|
|
|
|
use log::{Record, Level, Metadata, Log, SetLoggerError, LevelFilter};
|
|
|
|
|
|
|
|
struct SimpleLogger;
|
|
|
|
|
|
|
|
impl Log for SimpleLogger {
|
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
|
|
true
|
|
|
|
// metadata.level() <= Level::Info
|
|
|
|
}
|
|
|
|
fn log(&self, record: &Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
print_in_color(format_args!("{}\n", record.args()), Color::from(record.level()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Level> for Color {
|
|
|
|
fn from(level: Level) -> Self {
|
|
|
|
match level {
|
|
|
|
Level::Error => Color::Red,
|
|
|
|
Level::Warn => Color::Yellow,
|
|
|
|
Level::Info => Color::Blue,
|
|
|
|
Level::Debug => Color::LightRed,
|
|
|
|
Level::Trace => Color::DarkGray,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_to_console_code(color: Color) -> (u8, u8) {
|
|
|
|
match color {
|
|
|
|
Color::Black => (0, 0),
|
|
|
|
Color::Blue => (0, 4),
|
|
|
|
Color::Green => (0, 2),
|
|
|
|
Color::Cyan => (0, 6),
|
|
|
|
Color::Red => (0, 1),
|
|
|
|
Color::Magenta => (0, 5),
|
|
|
|
Color::Brown => (0, 3),
|
|
|
|
Color::LightGray => (1, 7),
|
|
|
|
Color::DarkGray => (0, 7),
|
|
|
|
Color::LightBlue => (1, 4),
|
|
|
|
Color::LightGreen => (1, 2),
|
|
|
|
Color::LightCyan => (1, 6),
|
|
|
|
Color::LightRed => (1, 1),
|
|
|
|
Color::Pink => (1, 5),
|
|
|
|
Color::Yellow => (1, 3),
|
|
|
|
Color::White => (1, 0),
|
|
|
|
}
|
2018-04-04 20:58:23 +04:00
|
|
|
}
|