Skip to content

Commit cf34027

Browse files
authored
Merge pull request #100 from LearningOS/ch1
Ch1
2 parents d5fa7aa + c806efc commit cf34027

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

os/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ edition = "2021"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
10+
log = "0.4"
1111
[profile.release]
1212
debug = true

os/src/logging.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*!
2+
3+
本模块利用 log crate 为你提供了日志功能,使用方式见 main.rs.
4+
5+
*/
6+
7+
use log::{self, Level, LevelFilter, Log, Metadata, Record};
8+
9+
struct SimpleLogger;
10+
11+
impl Log for SimpleLogger {
12+
fn enabled(&self, _metadata: &Metadata) -> bool {
13+
true
14+
}
15+
fn log(&self, record: &Record) {
16+
if !self.enabled(record.metadata()) {
17+
return;
18+
}
19+
let color = match record.level() {
20+
Level::Error => 31, // Red
21+
Level::Warn => 93, // BrightYellow
22+
Level::Info => 34, // Blue
23+
Level::Debug => 32, // Green
24+
Level::Trace => 90, // BrightBlack
25+
};
26+
println!(
27+
"\u{1B}[{}m[{:>5}] {}\u{1B}[0m",
28+
color,
29+
record.level(),
30+
record.args(),
31+
);
32+
}
33+
fn flush(&self) {}
34+
}
35+
36+
pub fn init() {
37+
static LOGGER: SimpleLogger = SimpleLogger;
38+
log::set_logger(&LOGGER).unwrap();
39+
log::set_max_level(match option_env!("LOG") {
40+
Some("ERROR") => LevelFilter::Error,
41+
Some("WARN") => LevelFilter::Warn,
42+
Some("INFO") => LevelFilter::Info,
43+
Some("DEBUG") => LevelFilter::Debug,
44+
Some("TRACE") => LevelFilter::Trace,
45+
_ => LevelFilter::Off,
46+
});
47+
}

os/src/main.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#![feature(panic_info_message)]
1515

1616
use core::arch::global_asm;
17+
use log::*;
1718

1819
#[macro_use]
1920
mod console;
2021
mod lang_items;
22+
mod logging;
2123
mod sbi;
2224

2325
#[path = "boards/qemu.rs"]
@@ -50,18 +52,28 @@ pub fn rust_main() -> ! {
5052
fn boot_stack_top(); // stack top
5153
}
5254
clear_bss();
53-
println!("Hello, world!");
54-
println!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
55-
println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
56-
println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
57-
println!(
58-
"boot_stack top=bottom={:#x}, lower_bound={:#x}",
55+
logging::init();
56+
println!("[kernel] Hello, world!");
57+
trace!(
58+
"[kernel] .text [{:#x}, {:#x})",
59+
stext as usize,
60+
etext as usize
61+
);
62+
debug!(
63+
"[kernel] .rodata [{:#x}, {:#x})",
64+
srodata as usize, erodata as usize
65+
);
66+
info!(
67+
"[kernel] .data [{:#x}, {:#x})",
68+
sdata as usize, edata as usize
69+
);
70+
warn!(
71+
"[kernel] boot_stack top=bottom={:#x}, lower_bound={:#x}",
5972
boot_stack_top as usize, boot_stack_lower_bound as usize
6073
);
61-
println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize);
74+
error!("[kernel] .bss [{:#x}, {:#x})", sbss as usize, ebss as usize);
6275

6376
use crate::board::QEMUExit;
6477
crate::board::QEMU_EXIT_HANDLE.exit_success(); // CI autotest success
6578
//crate::board::QEMU_EXIT_HANDLE.exit_failure(); // CI autoest failed
66-
6779
}

os/src/sbi.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ use core::arch::asm;
55
const SBI_SET_TIMER: usize = 0;
66
const SBI_CONSOLE_PUTCHAR: usize = 1;
77
const SBI_CONSOLE_GETCHAR: usize = 2;
8-
const SBI_CLEAR_IPI: usize = 3;
9-
const SBI_SEND_IPI: usize = 4;
10-
const SBI_REMOTE_FENCE_I: usize = 5;
11-
const SBI_REMOTE_SFENCE_VMA: usize = 6;
12-
const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7;
138
const SBI_SHUTDOWN: usize = 8;
149

1510
#[inline(always)]

0 commit comments

Comments
 (0)