Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log level option at compile time #184

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/migtd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ attestation = { path = "../attestation", default-features = false }
cc-measurement = { path = "../../deps/td-shim/cc-measurement"}
crypto = { path = "../crypto" }
lazy_static = { version = "1.0", features = ["spin_no_std"] }
log = { version = "0.4.13", features = ["release_max_level_off"] }
log = { version = "0.4.13" }
pci = { path="../devices/pci" }
policy = {path = "../policy"}
rust_std_stub = { path = "../std-support/rust-std-stub" }
Expand Down
12 changes: 6 additions & 6 deletions src/migtd/src/bin/migtd/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

extern crate alloc;

use log::info;
use migtd::migration::{session::MigrationSession, MigrationResult};
use migtd::{config, event_log, migration};
use td_payload::println;

const MIGTD_VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn runtime_main() {
}

fn basic_info() {
println!("MigTD Version - {}", MIGTD_VERSION);
info!("MigTD Version - {}", MIGTD_VERSION);
}

fn get_policy_and_measure(event_log: &mut [u8]) {
Expand Down Expand Up @@ -74,7 +74,7 @@ fn handle_pre_mig() {
panic!("Migration is not supported by VMM");
}
// Loop to wait for request
println!("Loop to wait for request");
info!("Loop to wait for request");
loop {
let mut session = MigrationSession::new();
if session.wait_for_request().is_ok() {
Expand Down Expand Up @@ -103,7 +103,7 @@ fn handle_pre_mig() {
let coverage_len = minicov::get_coverage_data_size();
assert!(coverage_len < MAX_COVERAGE_DATA_PAGE_COUNT * td_paging::PAGE_SIZE);
minicov::capture_coverage_to_buffer(&mut buffer[0..coverage_len]);
println!(
td_payload::println!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use td_payload::println? but not info! or error! ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is test code for benchmark, maybe it will be better that it is not controlled by log level.

"coverage addr: {:x}, coverage len: {}",
buffer.as_ptr() as u64,
coverage_len
Expand All @@ -129,11 +129,11 @@ fn test_memory() {
#[cfg(feature = "test_stack_size")]
{
let value = td_benchmark::StackProfiling::stack_usage().unwrap();
println!("max stack usage: {}", value);
td_payload::println!("max stack usage: {}", value);
}
#[cfg(feature = "test_heap_size")]
{
let value = td_benchmark::HeapProfiling::heap_usage().unwrap();
println!("max heap usage: {}", value);
td_payload::println!("max heap usage: {}", value);
}
}
15 changes: 7 additions & 8 deletions src/migtd/src/ratls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

use alloc::{string::ToString, vec::Vec};
use policy::PolicyError;
use rust_std_stub::io::{Read, Write};
use td_payload::println;
use tdx_tdcall::TdCallError;

use crate::{event_log::get_event_log, mig_policy};
use alloc::{string::ToString, vec::Vec};
use crypto::{
ecdsa::{ecdsa_verify, EcdsaPk},
hash::digest_sha384,
Expand All @@ -19,6 +14,10 @@ use crypto::{
},
Error as CryptoError, Result as CryptoResult,
};
use log::error;
use policy::PolicyError;
use rust_std_stub::io::{Read, Write};
use tdx_tdcall::TdCallError;

type Result<T> = core::result::Result<T, RatlsError>;

Expand Down Expand Up @@ -198,8 +197,8 @@ fn verify_peer_cert(
);

if let Err(e) = &policy_check_result {
println!("Policy check failed, below is the detail information:");
println!("{:x?}", e);
error!("Policy check failed, below is the detail information:");
error!("{:x?}", e);
}

return policy_check_result.map_err(|e| match e {
Expand Down
47 changes: 47 additions & 0 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,53 @@ pub(crate) struct BuildArgs {
/// Path of the configuration file for td-shim image layout
#[clap(long)]
image_layout: Option<PathBuf>,
/// Log level control in migtd, default value is `off` for release and `info` for debug
#[clap(short, long)]
log_level: Option<LogLevel>,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Platform {
Kvm,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum LogLevel {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}

impl LogLevel {
// Log levels can be statically set at compile time via Cargo features and they are
// configured separately for release and debug build.
// This function is used to output feature for `migtd` crate to control its log level.
fn debug_feature(&self) -> &str {
match self {
LogLevel::Off => "log/max_level_off",
LogLevel::Error => "log/max_level_error",
LogLevel::Warn => "log/max_level_warn",
LogLevel::Info => "log/max_level_info",
LogLevel::Debug => "log/max_level_debug",
LogLevel::Trace => "log/max_level_trace",
}
}

fn relase_feature(&self) -> &str {
match self {
LogLevel::Off => "log/release_max_level_off",
LogLevel::Error => "log/release_max_level_error",
LogLevel::Warn => "log/release_max_level_warn",
LogLevel::Info => "log/release_max_level_info",
LogLevel::Debug => "log/release_max_level_debug",
LogLevel::Trace => "log/release_max_level_trace",
}
}
}

impl BuildArgs {
pub fn build(&self) -> Result<PathBuf> {
let (reset_vector, shim) = self.build_shim()?;
Expand Down Expand Up @@ -225,6 +265,13 @@ impl BuildArgs {
features.push_str(selected);
}

features.push_str(",");
if self.debug {
features.push_str(self.log_level.unwrap_or(LogLevel::Info).debug_feature());
} else {
features.push_str(self.log_level.unwrap_or(LogLevel::Off).relase_feature());
}

features
}

Expand Down
Loading