Inject the logger for Asterinas

This commit is contained in:
Chen Chengjun 2024-11-15 16:58:06 +08:00 committed by Tate, Hongliang Tian
parent 7865469998
commit 0cb2ea562e
18 changed files with 149 additions and 5 deletions

22
Cargo.lock generated
View File

@ -81,6 +81,7 @@ name = "aster-block"
version = "0.1.0"
dependencies = [
"align_ext",
"aster-logger",
"aster-util",
"bitflags 1.3.2",
"bitvec",
@ -108,6 +109,7 @@ dependencies = [
name = "aster-framebuffer"
version = "0.1.0"
dependencies = [
"aster-logger",
"component",
"font8x8",
"log",
@ -120,6 +122,7 @@ name = "aster-input"
version = "0.1.0"
dependencies = [
"ascii",
"aster-logger",
"aster-rights",
"aster-util",
"bitflags 1.3.2",
@ -130,12 +133,26 @@ dependencies = [
"spin 0.9.8",
]
[[package]]
name = "aster-logger"
version = "0.1.0"
dependencies = [
"aster-console",
"cfg-if",
"component",
"log",
"ostd",
"owo-colors 3.5.0",
"spin 0.9.8",
]
[[package]]
name = "aster-network"
version = "0.1.0"
dependencies = [
"align_ext",
"aster-bigtcp",
"aster-logger",
"aster-rights",
"aster-util",
"bitflags 1.3.2",
@ -158,6 +175,7 @@ dependencies = [
"aster-console",
"aster-framebuffer",
"aster-input",
"aster-logger",
"aster-network",
"aster-rights",
"aster-rights-proc",
@ -223,6 +241,7 @@ dependencies = [
name = "aster-softirq"
version = "0.1.0"
dependencies = [
"aster-logger",
"component",
"intrusive-collections",
"ostd",
@ -233,6 +252,7 @@ dependencies = [
name = "aster-time"
version = "0.1.0"
dependencies = [
"aster-logger",
"aster-util",
"chrono",
"component",
@ -261,6 +281,7 @@ dependencies = [
"aster-block",
"aster-console",
"aster-input",
"aster-logger",
"aster-network",
"aster-rights",
"aster-util",
@ -1153,7 +1174,6 @@ dependencies = [
"ostd-macros",
"ostd-pod",
"ostd-test",
"owo-colors 3.5.0",
"riscv",
"sbi-rt",
"smallvec",

View File

@ -17,6 +17,7 @@ members = [
"kernel/comps/input",
"kernel/comps/network",
"kernel/comps/softirq",
"kernel/comps/logger",
"kernel/comps/time",
"kernel/comps/virtio",
"kernel/libs/cpio-decoder",

View File

@ -6,6 +6,7 @@ input = { name = "aster-input" }
block = { name = "aster-block" }
console = { name = "aster-console" }
softirq = { name = "aster-softirq" }
logger = { name = "aster-logger" }
time = { name = "aster-time" }
framebuffer = { name = "aster-framebuffer" }
network = { name = "aster-network" }

View File

@ -145,6 +145,7 @@ OSDK_CRATES := \
kernel/comps/input \
kernel/comps/network \
kernel/comps/softirq \
kernel/comps/logger \
kernel/comps/time \
kernel/comps/virtio \
kernel/libs/aster-util \

View File

@ -13,6 +13,7 @@ aster-network = { path = "comps/network" }
aster-console = { path = "comps/console" }
aster-framebuffer = { path = "comps/framebuffer" }
aster-softirq = { path = "comps/softirq" }
aster-logger = { path = "comps/logger" }
aster-time = { path = "comps/time" }
aster-virtio = { path = "comps/virtio" }
aster-rights = { path = "libs/aster-rights" }

View File

@ -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"

View File

@ -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 = [

View File

@ -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"

View 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"]

View 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);
}

View File

@ -27,7 +27,7 @@ pub fn _print(args: Arguments) {
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::console::_print(format_args!($($arg)*));
$crate::_print(format_args!($($arg)*));
}};
}
@ -38,6 +38,6 @@ macro_rules! println {
$crate::print!("\n")
};
($($arg:tt)*) => {{
$crate::console::_print(format_args_nl!($($arg)*));
$crate::_print(format_args_nl!($($arg)*));
}};
}

View 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(())
}

View File

@ -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" }

View File

@ -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"

View File

@ -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"

View File

@ -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" }

View File

@ -51,7 +51,6 @@ extern crate controlled;
extern crate getset;
pub mod arch;
pub mod console;
pub mod context;
pub mod cpu;
pub mod device;

View File

@ -47,11 +47,12 @@ macro_rules! current_thread {
};
}
pub(crate) use aster_logger::{print, println};
pub(crate) use crate::{
context::{Context, CurrentUserSpace, ReadCString},
current, current_thread,
error::{Errno, Error},
print, println,
process::signal::Pause,
time::{wait::WaitTimeout, Clock},
};