Skip to content

Commit

Permalink
Add a feature named enable-console-log.
Browse files Browse the repository at this point in the history
This feature can be enable to print log messages to console in
addition to storing them in the log buffer.
Enable this feature by default for debug builds.

Signed-off-by: Vasant Karasulli <[email protected]>
  • Loading branch information
vsntk18 committed Jun 29, 2023
1 parent 2a88bae commit fc7ff02
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
# ld to work, so build all the objects without performing the
# final linking step.
- name: Build
run: make FEATURES="default,enable-gdb" stage1/stage1.o stage1/reset.o
run: make FEATURES="default-debug,enable-gdb" stage1/stage1.o stage1/reset.o

- name: Run tests
run: make test
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ log = { version = "0.4.17", features = ["max_level_info", "release_max_level_inf
cc = "1.0.46"

[features]
default = ["enable-stacktrace"]
default-release = ["enable-stacktrace"]
default-debug = ["enable-stacktrace", "enable-console-log"]
enable-stacktrace = []
enable-gdb = []
enable-console-log = []
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
FEATURES ?= "default"
CARGO_ARGS = --features ${FEATURES}

ifdef RELEASE
TARGET_PATH="release"
FEATURES ?= "default-release"
CARGO_ARGS = --features ${FEATURES}
CARGO_ARGS += --release
else
TARGET_PATH="debug"
FEATURES ?= "default-debug"
CARGO_ARGS = --features ${FEATURES}
endif


Expand Down
51 changes: 32 additions & 19 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "enable-console-log")]
use crate::console::_print;

use crate::cpu::percpu::this_cpu_mut;
use crate::log_buffer::LB;
use crate::utils::immut_after_init::ImmutAfterInitCell;
Expand Down Expand Up @@ -65,32 +68,42 @@ impl log::Log for BufferLogger {
}

fn log(&self, record: &log::Record) {
let component: &'static str = &self.component;
let comp: &'static str = &self.component;
let line_buf: &mut LineBuffer = this_cpu_mut().get_line_buffer();
// Log format/detail depends on the level.
let rec_args = record.args();
let lvl = record.metadata().level().as_str();
let target = record.metadata().target();
match record.metadata().level() {
log::Level::Error | log::Level::Warn => write!(
line_buf,
"[{}] {}: {}\n",
component,
record.metadata().level().as_str(),
record.args()
)
.expect("write error"),
log::Level::Error | log::Level::Warn => {
line_buf
.write_fmt(format_args!("[{}] {}: {}\n", comp, lvl, rec_args))
.unwrap();
#[cfg(feature = "enable-console-log")]
{
_print(format_args!("[{}] {}: {}\n", comp, lvl, rec_args));
}
}

log::Level::Info => {
write!(line_buf, "[{}] {}\n", component, record.args()).expect("write error")
line_buf
.write_fmt(format_args!("[{}] {}\n", comp, rec_args))
.unwrap();
#[cfg(feature = "enable-console-log")]
{
_print(format_args!("[{}] {}\n", comp, rec_args));
}
}

log::Level::Debug | log::Level::Trace => write!(
line_buf,
"[{}/{}] {} {}\n",
component,
record.metadata().target(),
record.metadata().level().as_str(),
record.args()
)
.expect("write error"),
log::Level::Debug | log::Level::Trace => {
line_buf
.write_fmt(format_args!("[{}/{}] {} {}\n", comp, target, lvl, rec_args))
.unwrap();
#[cfg(feature = "enable-console-log")]
{
_print(format_args!("[{}/{}] {} {}\n", comp, target, lvl, rec_args));
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/stage2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ fn setup_env() {
unsafe {
WRITER.lock().set(&mut CONSOLE_SERIAL);
}
init_console();

#[cfg(feature = "enable-console-log")]
{
init_console();
}

init_log_buffer();
if let Err(_) = install_buffer_logger("Stage2") {
panic!("log buffer installation error")
Expand Down
6 changes: 5 additions & 1 deletion src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ pub extern "C" fn svsm_start(li: &KernelLaunchInfo, mi: &MigrateInfo) {
if let Err(_) = install_buffer_logger("SVSM") {
panic!("log buffer installation error")
}
init_console();

#[cfg(feature = "enable-console-log")]
{
init_console();
}

log::info!("COCONUT Secure Virtual Machine Service Module (SVSM)");

Expand Down

0 comments on commit fc7ff02

Please sign in to comment.