mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-18 03:56:42 +00:00
Inject the logger for Asterinas
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
7865469998
commit
0cb2ea562e
22
Cargo.lock
generated
22
Cargo.lock
generated
@ -81,6 +81,7 @@ name = "aster-block"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"align_ext",
|
"align_ext",
|
||||||
|
"aster-logger",
|
||||||
"aster-util",
|
"aster-util",
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"bitvec",
|
"bitvec",
|
||||||
@ -108,6 +109,7 @@ dependencies = [
|
|||||||
name = "aster-framebuffer"
|
name = "aster-framebuffer"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aster-logger",
|
||||||
"component",
|
"component",
|
||||||
"font8x8",
|
"font8x8",
|
||||||
"log",
|
"log",
|
||||||
@ -120,6 +122,7 @@ name = "aster-input"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ascii",
|
"ascii",
|
||||||
|
"aster-logger",
|
||||||
"aster-rights",
|
"aster-rights",
|
||||||
"aster-util",
|
"aster-util",
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
@ -130,12 +133,26 @@ dependencies = [
|
|||||||
"spin 0.9.8",
|
"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]]
|
[[package]]
|
||||||
name = "aster-network"
|
name = "aster-network"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"align_ext",
|
"align_ext",
|
||||||
"aster-bigtcp",
|
"aster-bigtcp",
|
||||||
|
"aster-logger",
|
||||||
"aster-rights",
|
"aster-rights",
|
||||||
"aster-util",
|
"aster-util",
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
@ -158,6 +175,7 @@ dependencies = [
|
|||||||
"aster-console",
|
"aster-console",
|
||||||
"aster-framebuffer",
|
"aster-framebuffer",
|
||||||
"aster-input",
|
"aster-input",
|
||||||
|
"aster-logger",
|
||||||
"aster-network",
|
"aster-network",
|
||||||
"aster-rights",
|
"aster-rights",
|
||||||
"aster-rights-proc",
|
"aster-rights-proc",
|
||||||
@ -223,6 +241,7 @@ dependencies = [
|
|||||||
name = "aster-softirq"
|
name = "aster-softirq"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aster-logger",
|
||||||
"component",
|
"component",
|
||||||
"intrusive-collections",
|
"intrusive-collections",
|
||||||
"ostd",
|
"ostd",
|
||||||
@ -233,6 +252,7 @@ dependencies = [
|
|||||||
name = "aster-time"
|
name = "aster-time"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aster-logger",
|
||||||
"aster-util",
|
"aster-util",
|
||||||
"chrono",
|
"chrono",
|
||||||
"component",
|
"component",
|
||||||
@ -261,6 +281,7 @@ dependencies = [
|
|||||||
"aster-block",
|
"aster-block",
|
||||||
"aster-console",
|
"aster-console",
|
||||||
"aster-input",
|
"aster-input",
|
||||||
|
"aster-logger",
|
||||||
"aster-network",
|
"aster-network",
|
||||||
"aster-rights",
|
"aster-rights",
|
||||||
"aster-util",
|
"aster-util",
|
||||||
@ -1153,7 +1174,6 @@ dependencies = [
|
|||||||
"ostd-macros",
|
"ostd-macros",
|
||||||
"ostd-pod",
|
"ostd-pod",
|
||||||
"ostd-test",
|
"ostd-test",
|
||||||
"owo-colors 3.5.0",
|
|
||||||
"riscv",
|
"riscv",
|
||||||
"sbi-rt",
|
"sbi-rt",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
|
@ -17,6 +17,7 @@ members = [
|
|||||||
"kernel/comps/input",
|
"kernel/comps/input",
|
||||||
"kernel/comps/network",
|
"kernel/comps/network",
|
||||||
"kernel/comps/softirq",
|
"kernel/comps/softirq",
|
||||||
|
"kernel/comps/logger",
|
||||||
"kernel/comps/time",
|
"kernel/comps/time",
|
||||||
"kernel/comps/virtio",
|
"kernel/comps/virtio",
|
||||||
"kernel/libs/cpio-decoder",
|
"kernel/libs/cpio-decoder",
|
||||||
|
@ -6,6 +6,7 @@ input = { name = "aster-input" }
|
|||||||
block = { name = "aster-block" }
|
block = { name = "aster-block" }
|
||||||
console = { name = "aster-console" }
|
console = { name = "aster-console" }
|
||||||
softirq = { name = "aster-softirq" }
|
softirq = { name = "aster-softirq" }
|
||||||
|
logger = { name = "aster-logger" }
|
||||||
time = { name = "aster-time" }
|
time = { name = "aster-time" }
|
||||||
framebuffer = { name = "aster-framebuffer" }
|
framebuffer = { name = "aster-framebuffer" }
|
||||||
network = { name = "aster-network" }
|
network = { name = "aster-network" }
|
||||||
|
1
Makefile
1
Makefile
@ -145,6 +145,7 @@ OSDK_CRATES := \
|
|||||||
kernel/comps/input \
|
kernel/comps/input \
|
||||||
kernel/comps/network \
|
kernel/comps/network \
|
||||||
kernel/comps/softirq \
|
kernel/comps/softirq \
|
||||||
|
kernel/comps/logger \
|
||||||
kernel/comps/time \
|
kernel/comps/time \
|
||||||
kernel/comps/virtio \
|
kernel/comps/virtio \
|
||||||
kernel/libs/aster-util \
|
kernel/libs/aster-util \
|
||||||
|
@ -13,6 +13,7 @@ aster-network = { path = "comps/network" }
|
|||||||
aster-console = { path = "comps/console" }
|
aster-console = { path = "comps/console" }
|
||||||
aster-framebuffer = { path = "comps/framebuffer" }
|
aster-framebuffer = { path = "comps/framebuffer" }
|
||||||
aster-softirq = { path = "comps/softirq" }
|
aster-softirq = { path = "comps/softirq" }
|
||||||
|
aster-logger = { path = "comps/logger" }
|
||||||
aster-time = { path = "comps/time" }
|
aster-time = { path = "comps/time" }
|
||||||
aster-virtio = { path = "comps/virtio" }
|
aster-virtio = { path = "comps/virtio" }
|
||||||
aster-rights = { path = "libs/aster-rights" }
|
aster-rights = { path = "libs/aster-rights" }
|
||||||
|
@ -11,6 +11,7 @@ spin = "0.9.4"
|
|||||||
ostd = { path = "../../../ostd" }
|
ostd = { path = "../../../ostd" }
|
||||||
align_ext = { path = "../../../ostd/libs/align_ext" }
|
align_ext = { path = "../../../ostd/libs/align_ext" }
|
||||||
aster-util = { path = "../../libs/aster-util" }
|
aster-util = { path = "../../libs/aster-util" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
ostd = { path = "../../../ostd" }
|
ostd = { path = "../../../ostd" }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
spin = "0.9.4"
|
spin = "0.9.4"
|
||||||
font8x8 = { version = "0.2.5", default-features = false, features = [
|
font8x8 = { version = "0.2.5", default-features = false, features = [
|
||||||
|
@ -11,6 +11,7 @@ spin = "0.9.4"
|
|||||||
ostd = { path = "../../../ostd" }
|
ostd = { path = "../../../ostd" }
|
||||||
aster-util = { path = "../../libs/aster-util" }
|
aster-util = { path = "../../libs/aster-util" }
|
||||||
aster-rights = { path = "../../libs/aster-rights" }
|
aster-rights = { path = "../../libs/aster-rights" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
||||||
log = "0.4"
|
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);
|
||||||
|
}
|
@ -27,7 +27,7 @@ pub fn _print(args: Arguments) {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
($($arg:tt)*) => {{
|
($($arg:tt)*) => {{
|
||||||
$crate::console::_print(format_args!($($arg)*));
|
$crate::_print(format_args!($($arg)*));
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +38,6 @@ macro_rules! println {
|
|||||||
$crate::print!("\n")
|
$crate::print!("\n")
|
||||||
};
|
};
|
||||||
($($arg:tt)*) => {{
|
($($arg:tt)*) => {{
|
||||||
$crate::console::_print(format_args_nl!($($arg)*));
|
$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-util = { path = "../../libs/aster-util" }
|
||||||
aster-rights = { path = "../../libs/aster-rights" }
|
aster-rights = { path = "../../libs/aster-rights" }
|
||||||
aster-bigtcp = { path = "../../libs/aster-bigtcp" }
|
aster-bigtcp = { path = "../../libs/aster-bigtcp" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
bitflags = "1.3"
|
bitflags = "1.3"
|
||||||
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
|
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
ostd = { path = "../../../ostd" }
|
ostd = { path = "../../../ostd" }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
intrusive-collections = "0.9.5"
|
intrusive-collections = "0.9.5"
|
||||||
spin = "0.9.4"
|
spin = "0.9.4"
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ edition = "2021"
|
|||||||
ostd = { path = "../../../ostd" }
|
ostd = { path = "../../../ostd" }
|
||||||
aster-util = { path = "../../libs/aster-util" }
|
aster-util = { path = "../../libs/aster-util" }
|
||||||
component = { path = "../../libs/comp-sys/component" }
|
component = { path = "../../libs/comp-sys/component" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
spin = "0.9.4"
|
spin = "0.9.4"
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ bytes = { version = "1.4.0", default-features = false }
|
|||||||
align_ext = { path = "../../../ostd/libs/align_ext" }
|
align_ext = { path = "../../../ostd/libs/align_ext" }
|
||||||
aster-input = { path = "../input" }
|
aster-input = { path = "../input" }
|
||||||
aster-block = { path = "../block" }
|
aster-block = { path = "../block" }
|
||||||
|
aster-logger = { path = "../logger" }
|
||||||
aster-network = { path = "../network" }
|
aster-network = { path = "../network" }
|
||||||
aster-console = { path = "../console" }
|
aster-console = { path = "../console" }
|
||||||
aster-util = { path = "../../libs/aster-util" }
|
aster-util = { path = "../../libs/aster-util" }
|
||||||
|
@ -51,7 +51,6 @@ extern crate controlled;
|
|||||||
extern crate getset;
|
extern crate getset;
|
||||||
|
|
||||||
pub mod arch;
|
pub mod arch;
|
||||||
pub mod console;
|
|
||||||
pub mod context;
|
pub mod context;
|
||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
pub mod device;
|
pub mod device;
|
||||||
|
@ -47,11 +47,12 @@ macro_rules! current_thread {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) use aster_logger::{print, println};
|
||||||
|
|
||||||
pub(crate) use crate::{
|
pub(crate) use crate::{
|
||||||
context::{Context, CurrentUserSpace, ReadCString},
|
context::{Context, CurrentUserSpace, ReadCString},
|
||||||
current, current_thread,
|
current, current_thread,
|
||||||
error::{Errno, Error},
|
error::{Errno, Error},
|
||||||
print, println,
|
|
||||||
process::signal::Pause,
|
process::signal::Pause,
|
||||||
time::{wait::WaitTimeout, Clock},
|
time::{wait::WaitTimeout, Clock},
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user