Catch panics during plugin initialization and log them

This commit is contained in:
Sebastian Dröge 2018-01-18 18:54:15 +02:00
parent 9375503296
commit d98c666bd9
2 changed files with 19 additions and 1 deletions

View file

@ -12,6 +12,7 @@ extern crate byte_slice_cast;
extern crate glib; extern crate glib;
#[macro_use] #[macro_use]
extern crate gst_plugin; extern crate gst_plugin;
#[macro_use]
extern crate gstreamer as gst; extern crate gstreamer as gst;
extern crate gstreamer_audio as gst_audio; extern crate gstreamer_audio as gst_audio;
extern crate gstreamer_base as gst_base; extern crate gstreamer_base as gst_base;

View file

@ -40,7 +40,24 @@ macro_rules! plugin_define(
}); });
unsafe extern "C" fn plugin_init_trampoline(plugin: *mut $crate::gst_ffi::GstPlugin) -> $crate::glib_ffi::gboolean { unsafe extern "C" fn plugin_init_trampoline(plugin: *mut $crate::gst_ffi::GstPlugin) -> $crate::glib_ffi::gboolean {
super::$plugin_init(&from_glib_borrow(plugin)).to_glib() use std::panic::{self, AssertUnwindSafe};
let result = panic::catch_unwind(AssertUnwindSafe(|| super::$plugin_init(&from_glib_borrow(plugin)).to_glib()));
match result {
Ok(result) => result,
Err(err) => {
let cat = $crate::gst::DebugCategory::get("GST_PLUGIN_LOADING").unwrap();
if let Some(cause) = err.downcast_ref::<&str>() {
gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause);
} else if let Some(cause) = err.downcast_ref::<String>() {
gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause);
} else {
gst_error!(cat, "Failed to initialize plugin due to panic");
}
$crate::glib_ffi::GFALSE
}
}
} }
} }
}; };