Use LoggableError in user defined functions and callbacks

`LoggableError` ensures an error in a user defined function is always
logged. This commit changes eligible function signatures accordingly.
This commit is contained in:
François Laignel 2019-01-24 22:11:43 +01:00
parent c5f0bab614
commit f59e35d0a3
3 changed files with 47 additions and 30 deletions

View file

@ -8,6 +8,7 @@
use Bin; use Bin;
use Element; use Element;
use LoggableError;
use glib; use glib;
use glib::object::Cast; use glib::object::Cast;
@ -27,7 +28,7 @@ pub trait GstBinExtManual: 'static {
fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>; fn add_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>; fn remove_many<E: IsA<Element>>(&self, elements: &[&E]) -> Result<(), glib::BoolError>;
fn connect_do_latency<F: Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>( fn connect_do_latency<F: Fn(&Self) -> Result<(), LoggableError> + Send + Sync + 'static>(
&self, &self,
f: F, f: F,
) -> SignalHandlerId; ) -> SignalHandlerId;
@ -82,12 +83,12 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
Ok(()) Ok(())
} }
fn connect_do_latency<F: Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>( fn connect_do_latency<F: Fn(&Self) -> Result<(), LoggableError> + Send + Sync + 'static>(
&self, &self,
f: F, f: F,
) -> SignalHandlerId { ) -> SignalHandlerId {
unsafe { unsafe {
let f: Box_<Box_<Fn(&Self) -> Result<(), glib::BoolError> + Send + Sync + 'static>> = let f: Box_<Box_<Fn(&Self) -> Result<(), LoggableError> + Send + Sync + 'static>> =
Box_::new(Box_::new(f)); Box_::new(Box_::new(f));
connect_raw( connect_raw(
self.as_ptr() as *mut _, self.as_ptr() as *mut _,
@ -163,11 +164,11 @@ unsafe extern "C" fn do_latency_trampoline<P>(
where where
P: IsA<Bin>, P: IsA<Bin>,
{ {
let f: &&(Fn(&P) -> Result<(), glib::BoolError> + Send + Sync + 'static) = transmute(f); let f: &&(Fn(&P) -> Result<(), LoggableError> + Send + Sync + 'static) = transmute(f);
match f(&Bin::from_glib_borrow(this).unsafe_cast()) { match f(&Bin::from_glib_borrow(this).unsafe_cast()) {
Ok(()) => true, Ok(()) => true,
Err(err) => { Err(err) => {
gst_error!(::CAT_RUST, obj: &Bin::from_glib_borrow(this), "{}", err); err.log_with_object(&Bin::from_glib_borrow(this));
false false
} }
} }

View file

@ -16,6 +16,7 @@ use FlowSuccess;
use Format; use Format;
use FormattedValue; use FormattedValue;
use GenericFormattedValue; use GenericFormattedValue;
use LoggableError;
use Pad; use Pad;
use PadLinkCheck; use PadLinkCheck;
use PadLinkError; use PadLinkError;
@ -164,11 +165,11 @@ pub trait PadExtManual: 'static {
fn set_activate_function<F>(&self, func: F) fn set_activate_function<F>(&self, func: F)
where where
F: Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static; F: Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static;
fn set_activatemode_function<F>(&self, func: F) fn set_activatemode_function<F>(&self, func: F)
where where
F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError>
+ Send + Send
+ Sync + Sync
+ 'static; + 'static;
@ -516,12 +517,12 @@ impl<O: IsA<Pad>> PadExtManual for O {
fn set_activate_function<F>(&self, func: F) fn set_activate_function<F>(&self, func: F)
where where
F: Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static, F: Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static,
{ {
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
unsafe { unsafe {
let func_box: Box< let func_box: Box<
Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> + Send + Sync + 'static, Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static,
> = Box::new(func); > = Box::new(func);
ffi::gst_pad_set_activate_function_full( ffi::gst_pad_set_activate_function_full(
self.as_ref().to_glib_none().0, self.as_ref().to_glib_none().0,
@ -534,7 +535,7 @@ impl<O: IsA<Pad>> PadExtManual for O {
fn set_activatemode_function<F>(&self, func: F) fn set_activatemode_function<F>(&self, func: F)
where where
F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError>
+ Send + Send
+ Sync + Sync
+ 'static, + 'static,
@ -542,7 +543,7 @@ impl<O: IsA<Pad>> PadExtManual for O {
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
unsafe { unsafe {
let func_box: Box< let func_box: Box<
Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), glib::BoolError> Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError>
+ Send + Send
+ Sync + Sync
+ 'static, + 'static,
@ -1119,7 +1120,7 @@ unsafe extern "C" fn trampoline_activate_function(
parent: *mut ffi::GstObject, parent: *mut ffi::GstObject,
) -> glib_ffi::gboolean { ) -> glib_ffi::gboolean {
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
let func: &&(Fn(&Pad, &Option<::Object>) -> Result<(), glib::BoolError> let func: &&(Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError>
+ Send + Send
+ Sync + Sync
+ 'static) = transmute((*pad).activatedata); + 'static) = transmute((*pad).activatedata);
@ -1128,7 +1129,7 @@ unsafe extern "C" fn trampoline_activate_function(
match func(&pad, &from_glib_borrow(parent)) { match func(&pad, &from_glib_borrow(parent)) {
Ok(()) => true, Ok(()) => true,
Err(err) => { Err(err) => {
gst_error!(::CAT_RUST, obj: &pad, "{}", err); err.log_with_object(&pad);
false false
} }
} }
@ -1142,15 +1143,24 @@ unsafe extern "C" fn trampoline_activatemode_function(
active: glib_ffi::gboolean, active: glib_ffi::gboolean,
) -> glib_ffi::gboolean { ) -> glib_ffi::gboolean {
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
let func: &&(Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> bool + Send + Sync + 'static) = let func: &&(Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError>
transmute((*pad).activatemodedata); + Send
+ Sync
+ 'static) = transmute((*pad).activatemodedata);
func( let pad: Pad = from_glib_borrow(pad);
&from_glib_borrow(pad), match func(
&pad,
&from_glib_borrow(parent), &from_glib_borrow(parent),
from_glib(mode), from_glib(mode),
from_glib(active), from_glib(active),
) ) {
Ok(()) => true,
Err(err) => {
err.log_with_object(&pad);
false
}
}
.to_glib() .to_glib()
} }

View file

@ -17,14 +17,15 @@ use glib::subclass::prelude::*;
use Bin; use Bin;
use BinClass; use BinClass;
use Element; use Element;
use LoggableError;
use Message; use Message;
pub trait BinImpl: ElementImpl + Send + Sync + 'static { pub trait BinImpl: ElementImpl + Send + Sync + 'static {
fn add_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { fn add_element(&self, bin: &Bin, element: &Element) -> Result<(), LoggableError> {
self.parent_add_element(bin, element) self.parent_add_element(bin, element)
} }
fn remove_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { fn remove_element(&self, bin: &Bin, element: &Element) -> Result<(), LoggableError> {
self.parent_remove_element(bin, element) self.parent_remove_element(bin, element)
} }
@ -32,29 +33,34 @@ pub trait BinImpl: ElementImpl + Send + Sync + 'static {
self.parent_handle_message(bin, message) self.parent_handle_message(bin, message)
} }
fn parent_add_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { fn parent_add_element(&self, bin: &Bin, element: &Element) -> Result<(), LoggableError> {
unsafe { unsafe {
let data = self.get_type_data(); let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass; let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass;
let f = (*parent_class) let f = (*parent_class).add_element.ok_or_else(|| {
.add_element gst_loggable_error!(::CAT_RUST, "Parent function `add_element` is not defined")
.ok_or_else(|| glib_bool_error!("Parent function `add_element` is not defined"))?; })?;
glib_result_from_gboolean!( gst_result_from_gboolean!(
f(bin.to_glib_none().0, element.to_glib_none().0), f(bin.to_glib_none().0, element.to_glib_none().0),
::CAT_RUST,
"Failed to add the element using the parent function" "Failed to add the element using the parent function"
) )
} }
} }
fn parent_remove_element(&self, bin: &Bin, element: &Element) -> Result<(), glib::BoolError> { fn parent_remove_element(&self, bin: &Bin, element: &Element) -> Result<(), LoggableError> {
unsafe { unsafe {
let data = self.get_type_data(); let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass; let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass;
let f = (*parent_class).remove_element.ok_or_else(|| { let f = (*parent_class).remove_element.ok_or_else(|| {
glib_bool_error!("Parent function `remove_element` is not defined") gst_loggable_error!(
::CAT_RUST,
"Parent function `remove_element` is not defined"
)
})?; })?;
glib_result_from_gboolean!( gst_result_from_gboolean!(
f(bin.to_glib_none().0, element.to_glib_none().0), f(bin.to_glib_none().0, element.to_glib_none().0),
::CAT_RUST,
"Failed to remove the element using the parent function" "Failed to remove the element using the parent function"
) )
} }
@ -103,7 +109,7 @@ where
match imp.add_element(&wrap, &from_glib_borrow(element)) { match imp.add_element(&wrap, &from_glib_borrow(element)) {
Ok(()) => true, Ok(()) => true,
Err(err) => { Err(err) => {
gst_error!(::CAT_RUST, obj: &wrap, "{}", err); err.log_with_object(&wrap);
false false
} }
} }
@ -128,7 +134,7 @@ where
match imp.remove_element(&wrap, &from_glib_borrow(element)) { match imp.remove_element(&wrap, &from_glib_borrow(element)) {
Ok(()) => true, Ok(()) => true,
Err(err) => { Err(err) => {
gst_error!(::CAT_RUST, obj: &wrap, "{}", err); err.log_with_object(&wrap);
false false
} }
} }