2018-06-03 19:05:43 +04:00
|
|
|
use core::fmt;
|
2018-07-16 05:48:58 +04:00
|
|
|
use log::{self, Level, LevelFilter, Log, Metadata, Record};
|
2018-11-26 12:58:45 +04:00
|
|
|
use crate::sync::SpinNoIrqLock as Mutex;
|
2018-11-19 13:21:06 +04:00
|
|
|
use lazy_static::lazy_static;
|
2018-10-16 20:21:18 +04:00
|
|
|
|
2018-10-17 15:37:53 +04:00
|
|
|
lazy_static! {
|
|
|
|
static ref log_mutex: Mutex<()> = Mutex::new(());
|
|
|
|
}
|
2018-04-04 20:58:23 +04:00
|
|
|
|
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,
|
2018-07-16 13:28:15 +04:00
|
|
|
Some("debug") => LevelFilter::Debug,
|
2018-05-21 21:11:37 +04:00
|
|
|
Some("trace") => LevelFilter::Trace,
|
2018-07-16 13:28:15 +04:00
|
|
|
_ => LevelFilter::Warn,
|
2018-05-21 21:11:37 +04:00
|
|
|
});
|
2018-04-07 16:57:14 +04:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:08:10 +04:00
|
|
|
#[macro_export]
|
2018-04-04 20:58:23 +04:00
|
|
|
macro_rules! print {
|
|
|
|
($($arg:tt)*) => ({
|
2018-07-12 19:31:54 +04:00
|
|
|
$crate::logging::print(format_args!($($arg)*));
|
2018-04-04 20:58:23 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:08:10 +04:00
|
|
|
#[macro_export]
|
2018-04-04 20:58:23 +04:00
|
|
|
macro_rules! println {
|
2018-12-27 11:56:20 +04:00
|
|
|
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
|
|
|
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
2018-04-04 20:58:23 +04:00
|
|
|
}
|
|
|
|
|
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);
|
2018-12-21 15:02:50 +04:00
|
|
|
format_args!("\u{1B}[{};{}m{}\u{1B}[0m", show.clone(), code + 30, $args)
|
2018-05-19 14:42:08 +04:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-04-07 16:57:14 +04:00
|
|
|
fn print_in_color(args: fmt::Arguments, color: Color) {
|
2018-11-19 13:21:06 +04:00
|
|
|
use crate::arch::io;
|
2018-12-18 09:31:31 +04:00
|
|
|
let _guard = log_mutex.lock();
|
2018-07-16 05:48:58 +04:00
|
|
|
io::putfmt(with_color!(args, color));
|
2018-04-07 16:57:14 +04:00
|
|
|
}
|
|
|
|
|
2018-07-12 16:45:22 +04:00
|
|
|
pub fn print(args: fmt::Arguments) {
|
2018-11-19 13:21:06 +04:00
|
|
|
use crate::arch::io;
|
2018-12-18 09:31:31 +04:00
|
|
|
let _guard = log_mutex.lock();
|
2018-07-16 05:48:58 +04:00
|
|
|
io::putfmt(args);
|
2018-07-12 16:45:22 +04:00
|
|
|
}
|
|
|
|
|
2018-05-19 14:42:08 +04:00
|
|
|
struct SimpleLogger;
|
|
|
|
|
|
|
|
impl Log for SimpleLogger {
|
2018-12-18 09:31:31 +04:00
|
|
|
fn enabled(&self, _metadata: &Metadata) -> bool {
|
|
|
|
true
|
2018-05-19 14:42:08 +04:00
|
|
|
}
|
|
|
|
fn log(&self, record: &Record) {
|
2018-06-01 07:46:32 +04:00
|
|
|
static DISABLED_TARGET: &[&str] = &[
|
|
|
|
];
|
|
|
|
if self.enabled(record.metadata()) && !DISABLED_TARGET.contains(&record.target()) {
|
2018-07-12 19:31:54 +04:00
|
|
|
// let target = record.target();
|
|
|
|
// let begin = target.as_bytes().iter().rposition(|&c| c == b':').map(|i| i + 1).unwrap_or(0);
|
2018-12-27 11:56:20 +04:00
|
|
|
print_in_color(format_args!("[{:>5}] {}\n", record.level(), record.args()), Color::from(record.level()));
|
2018-05-19 14:42:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2018-11-11 19:08:59 +04:00
|
|
|
Level::Debug => Color::Green,
|
2018-05-19 14:42:08 +04:00
|
|
|
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-07-06 18:33:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum Color {
|
|
|
|
Black = 0,
|
|
|
|
Blue = 1,
|
|
|
|
Green = 2,
|
|
|
|
Cyan = 3,
|
|
|
|
Red = 4,
|
|
|
|
Magenta = 5,
|
|
|
|
Brown = 6,
|
|
|
|
LightGray = 7,
|
|
|
|
DarkGray = 8,
|
|
|
|
LightBlue = 9,
|
|
|
|
LightGreen = 10,
|
|
|
|
LightCyan = 11,
|
|
|
|
LightRed = 12,
|
|
|
|
Pink = 13,
|
|
|
|
Yellow = 14,
|
|
|
|
White = 15,
|
2018-04-04 20:58:23 +04:00
|
|
|
}
|