Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
add correct function name in catch_panic macro (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Sep 26, 2023
1 parent 067c248 commit dcb5840
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions sqld-libsql-bindings/src/wal_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,26 @@ impl<T: WalHook> WalMethodsHook<T> {
}

macro_rules! catch_panic {
($($body:tt)*) => {
let ret = catch_unwind(move || {
$($body)*
});

match ret {
Ok(x) => x,
Err(e) => {
let error = if let Some(s) = e.downcast_ref::<String>() {
s.as_str()
} else if let Some(s) = e.downcast_ref::<&str>() {
s
} else {
"unknown"
};
let bt = std::backtrace::Backtrace::force_capture();
tracing::error!("panic in call to xframe: {error}:\n{bt}");
resume_unwind(e)
($name:literal, { $($body:tt)* }) => {
{
let ret = catch_unwind(move || {
$($body)*
});

match ret {
Ok(x) => x,
Err(e) => {
let error = if let Some(s) = e.downcast_ref::<String>() {
s.as_str()
} else if let Some(s) = e.downcast_ref::<&str>() {
s
} else {
"unknown"
};
let bt = std::backtrace::Backtrace::force_capture();
tracing::error!("panic in call to {}: {error}:\n{bt}", $name);
resume_unwind(e)
}
}
}
};
Expand Down Expand Up @@ -320,13 +322,13 @@ pub extern "C" fn xUndo<T: WalHook>(
func: Option<unsafe extern "C" fn(*mut c_void, u32) -> i32>,
undo_ctx: *mut c_void,
) -> i32 {
catch_panic! {
catch_panic!("xUndo", {
assert!(!wal.is_null());
let wal = unsafe { &mut *wal };
let orig_methods = get_orig_methods::<T>(wal);
let orig_xundo = orig_methods.xUndo.unwrap();
T::on_undo(wal, func, undo_ctx, orig_xundo)
}
})
}

#[allow(non_snake_case)]
Expand All @@ -337,12 +339,12 @@ pub extern "C" fn xSavepoint<T: WalHook>(wal: *mut Wal, wal_data: *mut u32) {

#[allow(non_snake_case)]
pub extern "C" fn xSavepointUndo<T: WalHook>(wal: *mut Wal, wal_data: *mut u32) -> i32 {
catch_panic! {
catch_panic!("xSavepointUndo", {
let wal = unsafe { &mut *wal };
let orig_methods = get_orig_methods::<T>(wal);
let orig_xsavepointundo = orig_methods.xSavepointUndo.unwrap();
T::on_savepoint_undo(wal, wal_data, orig_xsavepointundo)
}
})
}

#[allow(non_snake_case)]
Expand All @@ -354,7 +356,7 @@ pub extern "C" fn xFrames<T: WalHook>(
is_commit: c_int,
sync_flags: c_int,
) -> c_int {
catch_panic! {
catch_panic!("xFrames", {
assert!(!wal.is_null());
let wal = unsafe { &mut *wal };
let orig_methods = get_orig_methods::<T>(wal);
Expand All @@ -369,7 +371,7 @@ pub extern "C" fn xFrames<T: WalHook>(
sync_flags,
orig_xframe,
)
}
})
}

#[tracing::instrument(skip(wal, db))]
Expand All @@ -386,7 +388,7 @@ pub extern "C" fn xCheckpoint<T: WalHook>(
frames_in_wal: *mut c_int,
backfilled_frames: *mut c_int,
) -> i32 {
catch_panic! {
catch_panic!("xCheckpoint", {
let wal = unsafe { &mut *wal };
let orig_methods = get_orig_methods::<T>(wal);
let orig_xcheckpoint = orig_methods.xCheckpoint.unwrap();
Expand All @@ -403,7 +405,7 @@ pub extern "C" fn xCheckpoint<T: WalHook>(
backfilled_frames,
orig_xcheckpoint,
)
}
})
}

#[allow(non_snake_case)]
Expand Down

0 comments on commit dcb5840

Please sign in to comment.