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

logger: don't panic in the panic handler #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ extern "C" {
fn proxy_log(level: LogLevel, message_data: *const u8, message_size: usize) -> Status;
}

// Useful for the panic hook to avoid a double-panic.
#[inline]
pub(crate) fn log_nopanic(level: LogLevel, message: &str) -> Result<Status, Status> {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason for using Result<Status, Status> instead of Result<(), Status> like rest of the SDK?

Copy link
Author

Choose a reason for hiding this comment

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

Just leaving the decision about what to do with the Status to the callers out of habit. Since this fn is private to this crate it won't show as part of the SDK to users, and this allows for callers to have more flexibility to make that decision or be extended based on this value without needing to modify this fn.

let status = unsafe { proxy_log(level, message.as_ptr(), message.len()) };
match status {
ok @ Status::Ok => Ok(ok),
unknown => Err(unknown),
}
}

pub fn log(level: LogLevel, message: &str) -> Result<(), Status> {
unsafe {
match proxy_log(level, message.as_ptr(), message.len()) {
Status::Ok => Ok(()),
status => panic!("unexpected status: {}", status as u32),
}
match log_nopanic(level, message) {
Ok(_) => Ok(()),
Err(status) => panic!("unexpected status: {}", status as u32),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn set_log_level(level: LogLevel) {
if !INITIALIZED.load(Ordering::Relaxed) {
log::set_logger(&LOGGER).unwrap();
panic::set_hook(Box::new(|panic_info| {
hostcalls::log(LogLevel::Critical, &panic_info.to_string()).unwrap();
let _ = hostcalls::log_nopanic(LogLevel::Critical, &panic_info.to_string());
}));
INITIALIZED.store(true, Ordering::Relaxed);
}
Expand Down