From fb412d0cfba07c1e7df5377860d7a6c73c1d0944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 31 Jan 2021 12:21:30 +0200 Subject: [PATCH] gstreamer/log: Handle compiled out GStreamer debug system properly By mirroring the no-op behaviour of the C code instead of failing because of a NULL debug category. Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/306 --- gstreamer/src/log.rs | 73 +++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/gstreamer/src/log.rs b/gstreamer/src/log.rs index 19ec03c39..2bfd57003 100644 --- a/gstreamer/src/log.rs +++ b/gstreamer/src/log.rs @@ -47,7 +47,7 @@ impl DebugMessage { } #[derive(PartialEq, Eq, Clone, Copy)] -pub struct DebugCategory(ptr::NonNull); +pub struct DebugCategory(Option>); impl DebugCategory { pub fn new(name: &str, color: ::DebugColorFlags, description: Option<&str>) -> DebugCategory { @@ -67,8 +67,8 @@ impl DebugCategory { color.to_glib(), description.to_glib_none().0, ); - assert!(!ptr.is_null()); - DebugCategory(ptr::NonNull::new_unchecked(ptr)) + // Can be NULL if the debug system is compiled out + DebugCategory(ptr::NonNull::new(ptr)) } } @@ -84,44 +84,62 @@ impl DebugCategory { if cat.is_null() { None } else { - Some(DebugCategory(ptr::NonNull::new_unchecked(cat))) + Some(DebugCategory(Some(ptr::NonNull::new_unchecked(cat)))) } } } pub fn get_threshold(self) -> ::DebugLevel { - from_glib(unsafe { gst_sys::gst_debug_category_get_threshold(self.0.as_ptr()) }) + match self.0 { + Some(cat) => unsafe { + from_glib(gst_sys::gst_debug_category_get_threshold(cat.as_ptr())) + }, + None => ::DebugLevel::None, + } } pub fn set_threshold(self, threshold: ::DebugLevel) { - unsafe { gst_sys::gst_debug_category_set_threshold(self.0.as_ptr(), threshold.to_glib()) } + if let Some(cat) = self.0 { + unsafe { gst_sys::gst_debug_category_set_threshold(cat.as_ptr(), threshold.to_glib()) } + } } pub fn reset_threshold(self) { - unsafe { gst_sys::gst_debug_category_reset_threshold(self.0.as_ptr()) } + if let Some(cat) = self.0 { + unsafe { gst_sys::gst_debug_category_reset_threshold(cat.as_ptr()) } + } } pub fn get_color(self) -> ::DebugColorFlags { - unsafe { from_glib(gst_sys::gst_debug_category_get_color(self.0.as_ptr())) } + match self.0 { + Some(cat) => unsafe { from_glib(gst_sys::gst_debug_category_get_color(cat.as_ptr())) }, + None => ::DebugColorFlags::empty(), + } } pub fn get_name<'a>(self) -> &'a str { - unsafe { - CStr::from_ptr(gst_sys::gst_debug_category_get_name(self.0.as_ptr())) - .to_str() - .unwrap() + match self.0 { + Some(cat) => unsafe { + CStr::from_ptr(gst_sys::gst_debug_category_get_name(cat.as_ptr())) + .to_str() + .unwrap() + }, + None => "", } } pub fn get_description<'a>(self) -> Option<&'a str> { - unsafe { - let ptr = gst_sys::gst_debug_category_get_description(self.0.as_ptr()); + match self.0 { + Some(cat) => unsafe { + let ptr = gst_sys::gst_debug_category_get_description(cat.as_ptr()); - if ptr.is_null() { - None - } else { - Some(CStr::from_ptr(ptr).to_str().unwrap()) - } + if ptr.is_null() { + None + } else { + Some(CStr::from_ptr(ptr).to_str().unwrap()) + } + }, + None => None, } } @@ -135,8 +153,13 @@ impl DebugCategory { line: u32, args: fmt::Arguments, ) { + let cat = match self.0 { + Some(cat) => cat, + None => return, + }; + unsafe { - if level.to_glib() as i32 > self.0.as_ref().threshold { + if level.to_glib() as i32 > cat.as_ref().threshold { return; } } @@ -148,7 +171,7 @@ impl DebugCategory { unsafe { gst_sys::gst_debug_log( - self.0.as_ptr(), + cat.as_ptr(), level.to_glib(), file.to_glib_none().0, module.to_glib_none().0, @@ -326,7 +349,10 @@ unsafe extern "C" fn log_handler( + Sync + 'static, { - let category = DebugCategory(ptr::NonNull::new_unchecked(category)); + if category.is_null() { + return; + } + let category = DebugCategory(Some(ptr::NonNull::new_unchecked(category))); let level = from_glib(level); let file = CStr::from_ptr(file).to_string_lossy(); let function = CStr::from_ptr(function).to_string_lossy(); @@ -389,8 +415,7 @@ pub fn debug_remove_default_log_function() { pub fn debug_remove_log_function(log_fn: DebugLogFunction) { skip_assert_initialized!(); unsafe { - let removed = gst_sys::gst_debug_remove_log_function_by_data(log_fn.0.as_ptr()); - assert_eq!(removed, 1); + gst_sys::gst_debug_remove_log_function_by_data(log_fn.0.as_ptr()); } }