Skip to content

Commit d020eab

Browse files
Copilotdblnz
andcommitted
Replace log with tracing in guest code and update Cargo.toml features
Co-authored-by: dblnz <13020154+dblnz@users.noreply.github.com>
1 parent f4dd6d2 commit d020eab

14 files changed

Lines changed: 72 additions & 35 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ workspace = true
1717
[dependencies]
1818
flatbuffers = { version = "25.9.23", default-features = false }
1919
anyhow = { version = "1.0.100", default-features = false }
20-
tracing = { version = "0.1.41" }
20+
tracing = { version = "0.1.41", default-features = false, features = [ "attributes", "log" ] }
2121
arbitrary = {version = "1.4.2", optional = true, features = ["derive"]}
2222
spin = "0.10.0"
2323

src/hyperlight_common/src/flatbuffer_wrappers/guest_log_level.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,15 @@ impl From<Level> for LogLevel {
110110
}
111111
}
112112
}
113+
114+
impl From<tracing::log::Level> for LogLevel {
115+
fn from(val: tracing::log::Level) -> LogLevel {
116+
match val {
117+
tracing::log::Level::Trace => LogLevel::Trace,
118+
tracing::log::Level::Debug => LogLevel::Debug,
119+
tracing::log::Level::Info => LogLevel::Information,
120+
tracing::log::Level::Warn => LogLevel::Warning,
121+
tracing::log::Level::Error => LogLevel::Error,
122+
}
123+
}
124+
}

src/hyperlight_guest_bin/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ hyperlight-guest = { workspace = true, default-features = false }
2525
hyperlight-common = { workspace = true, default-features = false }
2626
hyperlight-guest-tracing = { workspace = true, default-features = false }
2727
buddy_system_allocator = "0.11.0"
28-
log = { version = "0.4", default-features = false }
2928
spin = "0.10.0"
3029
flatbuffers = { version = "25.2.10", default-features = false }
31-
tracing = { version = "0.1.41", default-features = false, features = ["attributes"] }
30+
tracing = { version = "0.1.41", default-features = false, features = ["attributes", "log"] }
3231

3332
[lints]
3433
workspace = true

src/hyperlight_guest_bin/src/guest_function/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn internal_dispatch_function() {
9090
let handle = unsafe { GUEST_HANDLE };
9191

9292
#[cfg(debug_assertions)]
93-
log::trace!("internal_dispatch_function");
93+
tracing::trace!("internal_dispatch_function");
9494

9595
let function_call = handle
9696
.try_pop_shared_input_data_into::<FunctionCall>()

src/hyperlight_guest_bin/src/guest_logger.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
use alloc::format;
1818

1919
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
20-
use log::{LevelFilter, Metadata, Record};
20+
use tracing::log::{LevelFilter, Metadata, Record};
2121

2222
use crate::GUEST_HANDLE;
2323

@@ -27,11 +27,11 @@ struct GuestLogger {}
2727
pub(crate) fn init_logger(level: LevelFilter) {
2828
// if this `expect` fails we have no way to recover anyway, so we actually prefer a panic here
2929
// below temporary guest logger is promoted to static by the compiler.
30-
log::set_logger(&GuestLogger {}).expect("unable to setup guest logger");
31-
log::set_max_level(level);
30+
tracing::log::set_logger(&GuestLogger {}).expect("unable to setup guest logger");
31+
tracing::log::set_max_level(level);
3232
}
3333

34-
impl log::Log for GuestLogger {
34+
impl tracing::log::Log for GuestLogger {
3535
// The various macros like `info!` and `error!` will call the global log::max_level()
3636
// before calling our `log`. This means that we should log every message we get, because
3737
// we won't even see the ones that are above the set max level.

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use hyperlight_common::mem::HyperlightPEB;
3232
use hyperlight_common::outb::OutBAction;
3333
use hyperlight_guest::exit::{halt, write_abort};
3434
use hyperlight_guest::guest_handle::handle::GuestHandle;
35-
use log::LevelFilter;
3635
use spin::Once;
36+
use tracing::log::LevelFilter;
3737

3838
// === Modules ===
3939
#[cfg(target_arch = "x86_64")]

src/hyperlight_guest_capi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ hyperlight-guest-bin = { workspace = true, default-features = true }
1717
hyperlight-common = { workspace = true, default-features = false }
1818

1919
flatbuffers = { version = "25.2.10", default-features = false }
20-
log = { version = "0.4", default-features = false }
20+
tracing = { version = "0.1.41", default-features = false, features = ["log"] }
2121

2222
[build-dependencies]
2323
cbindgen = "0.29.2"

src/hyperlight_guest_capi/src/logging.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,41 @@ limitations under the License.
1616

1717
use core::ffi::c_char;
1818

19+
/// C-compatible log level enum
20+
#[repr(C)]
21+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
22+
pub enum Level {
23+
Error = 1,
24+
Warn = 2,
25+
Info = 3,
26+
Debug = 4,
27+
Trace = 5,
28+
}
29+
30+
impl From<Level> for tracing::log::Level {
31+
fn from(level: Level) -> Self {
32+
match level {
33+
Level::Error => tracing::log::Level::Error,
34+
Level::Warn => tracing::log::Level::Warn,
35+
Level::Info => tracing::log::Level::Info,
36+
Level::Debug => tracing::log::Level::Debug,
37+
Level::Trace => tracing::log::Level::Trace,
38+
}
39+
}
40+
}
41+
1942
#[unsafe(no_mangle)]
20-
pub extern "C" fn hl_log(
21-
level: log::Level,
22-
message: *const c_char,
23-
line: i32,
24-
file: *const c_char,
25-
) {
26-
if log::log_enabled!(level) {
43+
pub extern "C" fn hl_log(level: Level, message: *const c_char, line: i32, file: *const c_char) {
44+
let log_level: tracing::log::Level = level.into();
45+
46+
if tracing::log::log_enabled!(log_level) {
2747
let message = unsafe { core::ffi::CStr::from_ptr(message).to_string_lossy() };
2848
let file = unsafe { core::ffi::CStr::from_ptr(file).to_string_lossy() };
2949

30-
log::logger().log(
31-
&log::RecordBuilder::new()
32-
.args(format_args!("{}: {}", level, message))
33-
.level(level)
50+
tracing::log::logger().log(
51+
&tracing::log::RecordBuilder::new()
52+
.args(format_args!("{}: {}", log_level, message))
53+
.level(log_level)
3454
.line(Some(line as u32))
3555
.file(Some(&file))
3656
.build(),

src/tests/rust_guests/dummyguest/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)