-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a default logger implementation for log crate. Add a new feature "log".
- Loading branch information
1 parent
da5a078
commit 4619af6
Showing
7 changed files
with
86 additions
and
5 deletions.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,66 @@ | ||
extern crate alloc; | ||
|
||
use crate::syscalls; | ||
use alloc::format; | ||
use log::{Level, Metadata, Record}; | ||
use log::{LevelFilter, SetLoggerError}; | ||
|
||
struct SimpleLogger; | ||
|
||
impl log::Log for SimpleLogger { | ||
fn enabled(&self, _metadata: &Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
let metadata = record.metadata(); | ||
if self.enabled(metadata) { | ||
let level = metadata.level(); | ||
match level { | ||
Level::Error => syscalls::debug(format!( | ||
"[\x1b[31m{}\x1b[0m {}:{}] {}", | ||
record.level(), | ||
record.file().unwrap_or("???"), | ||
record.line().unwrap_or(0), | ||
record.args() | ||
)), | ||
Level::Warn => syscalls::debug(format!( | ||
"[\x1b[33m{}\x1b[0m {}:{}] {}", | ||
record.level(), | ||
record.file().unwrap_or("???"), | ||
record.line().unwrap_or(0), | ||
record.args() | ||
)), | ||
Level::Info => syscalls::debug(format!( | ||
"[\x1b[32m{}\x1b[0m {}:{}] {}", | ||
record.level(), | ||
record.file().unwrap_or("???"), | ||
record.line().unwrap_or(0), | ||
record.args() | ||
)), | ||
Level::Debug => syscalls::debug(format!( | ||
"[\x1b[34m{}\x1b[0m {}:{}] {}", | ||
record.level(), | ||
record.file().unwrap_or("???"), | ||
record.line().unwrap_or(0), | ||
record.args() | ||
)), | ||
Level::Trace => syscalls::debug(format!( | ||
"[\x1b[36m{}\x1b[0m {}:{}] {}", | ||
record.level(), | ||
record.file().unwrap_or("???"), | ||
record.line().unwrap_or(0), | ||
record.args() | ||
)), | ||
} | ||
} | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
static LOGGER: SimpleLogger = SimpleLogger; | ||
|
||
pub fn init() -> Result<(), SetLoggerError> { | ||
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace)) | ||
} |