mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-20 04:56:32 +00:00
Inject the logger for Asterinas
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
7865469998
commit
0cb2ea562e
@ -11,6 +11,7 @@ spin = "0.9.4"
|
||||
ostd = { path = "../../../ostd" }
|
||||
align_ext = { path = "../../../ostd/libs/align_ext" }
|
||||
aster-util = { path = "../../libs/aster-util" }
|
||||
aster-logger = { path = "../logger" }
|
||||
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
log = "0.4"
|
||||
|
@ -8,6 +8,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
ostd = { path = "../../../ostd" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
aster-logger = { path = "../logger" }
|
||||
log = "0.4"
|
||||
spin = "0.9.4"
|
||||
font8x8 = { version = "0.2.5", default-features = false, features = [
|
||||
|
@ -11,6 +11,7 @@ spin = "0.9.4"
|
||||
ostd = { path = "../../../ostd" }
|
||||
aster-util = { path = "../../libs/aster-util" }
|
||||
aster-rights = { path = "../../libs/aster-rights" }
|
||||
aster-logger = { path = "../logger" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
||||
log = "0.4"
|
||||
|
19
kernel/comps/logger/Cargo.toml
Normal file
19
kernel/comps/logger/Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "aster-logger"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
aster-console = { path = "../console" }
|
||||
log = "0.4"
|
||||
ostd = { path = "../../../ostd" }
|
||||
spin = "0.9.4"
|
||||
owo-colors = { version = "3", optional = true }
|
||||
cfg-if = "1.0"
|
||||
|
||||
[features]
|
||||
default = ["log_color"]
|
||||
log_color = ["dep:owo-colors"]
|
64
kernel/comps/logger/src/aster_logger.rs
Normal file
64
kernel/comps/logger/src/aster_logger.rs
Normal file
@ -0,0 +1,64 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use log::{Metadata, Record};
|
||||
use ostd::timer::Jiffies;
|
||||
|
||||
/// The logger used for Asterinas.
|
||||
struct AsterLogger;
|
||||
|
||||
static LOGGER: AsterLogger = AsterLogger;
|
||||
|
||||
impl log::Log for AsterLogger {
|
||||
fn enabled(&self, _metadata: &Metadata) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
let timestamp = Jiffies::elapsed().as_duration().as_secs_f64();
|
||||
|
||||
// Use a global lock to prevent interleaving of log messages.
|
||||
use ostd::sync::SpinLock;
|
||||
static RECORD_LOCK: SpinLock<()> = SpinLock::new(());
|
||||
let _lock = RECORD_LOCK.disable_irq().lock();
|
||||
|
||||
print_logs(record, timestamp);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "log_color")]
|
||||
fn print_logs(record: &Record, timestamp: f64) {
|
||||
use owo_colors::Style;
|
||||
|
||||
let timestamp_style = Style::new().green();
|
||||
let record_style = Style::new().default_color();
|
||||
let level_style = match record.level() {
|
||||
log::Level::Error => Style::new().red(),
|
||||
log::Level::Warn => Style::new().bright_yellow(),
|
||||
log::Level::Info => Style::new().blue(),
|
||||
log::Level::Debug => Style::new().bright_green(),
|
||||
log::Level::Trace => Style::new().bright_black(),
|
||||
};
|
||||
|
||||
super::_print(format_args!(
|
||||
"{} {:<5}: {}\n",
|
||||
timestamp_style.style(format_args!("[{:>10.3}]", timestamp)),
|
||||
level_style.style(record.level()),
|
||||
record_style.style(record.args())
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "log_color"))]
|
||||
fn print_logs(record: &Record, timestamp: f64) {
|
||||
super::_print(format_args!(
|
||||
"{} {:<5}: {}\n",
|
||||
format_args!("[{:>10.3}]", timestamp),
|
||||
record.level(),
|
||||
record.args()
|
||||
));
|
||||
}
|
||||
|
||||
pub(super) fn init() {
|
||||
ostd::logger::inject_logger(&LOGGER);
|
||||
}
|
43
kernel/comps/logger/src/console.rs
Normal file
43
kernel/comps/logger/src/console.rs
Normal file
@ -0,0 +1,43 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! `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 aster_console::all_devices() {
|
||||
device.send(s.as_bytes());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Prints the formatted arguments to the standard output.
|
||||
pub fn _print(args: Arguments) {
|
||||
VirtioConsolesPrinter.write_fmt(args).unwrap();
|
||||
}
|
||||
|
||||
/// Copied from Rust std: <https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs>
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => {{
|
||||
$crate::_print(format_args!($($arg)*));
|
||||
}};
|
||||
}
|
||||
|
||||
/// Copied 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::_print(format_args_nl!($($arg)*));
|
||||
}};
|
||||
}
|
30
kernel/comps/logger/src/lib.rs
Normal file
30
kernel/comps/logger/src/lib.rs
Normal file
@ -0,0 +1,30 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! The logger implementation for Asterinas.
|
||||
//!
|
||||
//! This logger now has the most basic logging functionality, controls the output
|
||||
//! based on the globally set log level. Different log levels will be represented
|
||||
//! with different colors if enabling `log_color` feature.
|
||||
//!
|
||||
//! This logger guarantees _atomicity_ under concurrency: messages are always
|
||||
//! printed in their entirety without being mixed with messages generated
|
||||
//! concurrently on other cores.
|
||||
//!
|
||||
//! IRQs are disabled while printing. So do not print long log messages.
|
||||
#![no_std]
|
||||
#![deny(unsafe_code)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use component::{init_component, ComponentInitError};
|
||||
|
||||
mod aster_logger;
|
||||
mod console;
|
||||
|
||||
pub use console::_print;
|
||||
|
||||
#[init_component]
|
||||
fn init() -> Result<(), ComponentInitError> {
|
||||
aster_logger::init();
|
||||
Ok(())
|
||||
}
|
@ -10,6 +10,7 @@ align_ext = { path = "../../../ostd/libs/align_ext" }
|
||||
aster-util = { path = "../../libs/aster-util" }
|
||||
aster-rights = { path = "../../libs/aster-rights" }
|
||||
aster-bigtcp = { path = "../../libs/aster-bigtcp" }
|
||||
aster-logger = { path = "../logger" }
|
||||
bitflags = "1.3"
|
||||
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
|
@ -8,6 +8,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
ostd = { path = "../../../ostd" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
aster-logger = { path = "../logger" }
|
||||
intrusive-collections = "0.9.5"
|
||||
spin = "0.9.4"
|
||||
|
||||
|
@ -9,6 +9,7 @@ edition = "2021"
|
||||
ostd = { path = "../../../ostd" }
|
||||
aster-util = { path = "../../libs/aster-util" }
|
||||
component = { path = "../../libs/comp-sys/component" }
|
||||
aster-logger = { path = "../logger" }
|
||||
log = "0.4"
|
||||
spin = "0.9.4"
|
||||
|
||||
|
@ -12,6 +12,7 @@ bytes = { version = "1.4.0", default-features = false }
|
||||
align_ext = { path = "../../../ostd/libs/align_ext" }
|
||||
aster-input = { path = "../input" }
|
||||
aster-block = { path = "../block" }
|
||||
aster-logger = { path = "../logger" }
|
||||
aster-network = { path = "../network" }
|
||||
aster-console = { path = "../console" }
|
||||
aster-util = { path = "../../libs/aster-util" }
|
||||
|
Reference in New Issue
Block a user