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
This commit is contained in:
Sebastian Dröge 2021-01-31 12:21:30 +02:00
parent bad30dbbda
commit fb412d0cfb

View file

@ -47,7 +47,7 @@ impl DebugMessage {
} }
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub struct DebugCategory(ptr::NonNull<gst_sys::GstDebugCategory>); pub struct DebugCategory(Option<ptr::NonNull<gst_sys::GstDebugCategory>>);
impl DebugCategory { impl DebugCategory {
pub fn new(name: &str, color: ::DebugColorFlags, description: Option<&str>) -> DebugCategory { pub fn new(name: &str, color: ::DebugColorFlags, description: Option<&str>) -> DebugCategory {
@ -67,8 +67,8 @@ impl DebugCategory {
color.to_glib(), color.to_glib(),
description.to_glib_none().0, description.to_glib_none().0,
); );
assert!(!ptr.is_null()); // Can be NULL if the debug system is compiled out
DebugCategory(ptr::NonNull::new_unchecked(ptr)) DebugCategory(ptr::NonNull::new(ptr))
} }
} }
@ -84,44 +84,62 @@ impl DebugCategory {
if cat.is_null() { if cat.is_null() {
None None
} else { } else {
Some(DebugCategory(ptr::NonNull::new_unchecked(cat))) Some(DebugCategory(Some(ptr::NonNull::new_unchecked(cat))))
} }
} }
} }
pub fn get_threshold(self) -> ::DebugLevel { 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) { 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) { 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 { 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 { pub fn get_name<'a>(self) -> &'a str {
unsafe { match self.0 {
CStr::from_ptr(gst_sys::gst_debug_category_get_name(self.0.as_ptr())) Some(cat) => unsafe {
.to_str() CStr::from_ptr(gst_sys::gst_debug_category_get_name(cat.as_ptr()))
.unwrap() .to_str()
.unwrap()
},
None => "",
} }
} }
pub fn get_description<'a>(self) -> Option<&'a str> { pub fn get_description<'a>(self) -> Option<&'a str> {
unsafe { match self.0 {
let ptr = gst_sys::gst_debug_category_get_description(self.0.as_ptr()); Some(cat) => unsafe {
let ptr = gst_sys::gst_debug_category_get_description(cat.as_ptr());
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
Some(CStr::from_ptr(ptr).to_str().unwrap()) Some(CStr::from_ptr(ptr).to_str().unwrap())
} }
},
None => None,
} }
} }
@ -135,8 +153,13 @@ impl DebugCategory {
line: u32, line: u32,
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
let cat = match self.0 {
Some(cat) => cat,
None => return,
};
unsafe { unsafe {
if level.to_glib() as i32 > self.0.as_ref().threshold { if level.to_glib() as i32 > cat.as_ref().threshold {
return; return;
} }
} }
@ -148,7 +171,7 @@ impl DebugCategory {
unsafe { unsafe {
gst_sys::gst_debug_log( gst_sys::gst_debug_log(
self.0.as_ptr(), cat.as_ptr(),
level.to_glib(), level.to_glib(),
file.to_glib_none().0, file.to_glib_none().0,
module.to_glib_none().0, module.to_glib_none().0,
@ -326,7 +349,10 @@ unsafe extern "C" fn log_handler<T>(
+ Sync + Sync
+ 'static, + '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 level = from_glib(level);
let file = CStr::from_ptr(file).to_string_lossy(); let file = CStr::from_ptr(file).to_string_lossy();
let function = CStr::from_ptr(function).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) { pub fn debug_remove_log_function(log_fn: DebugLogFunction) {
skip_assert_initialized!(); skip_assert_initialized!();
unsafe { unsafe {
let removed = gst_sys::gst_debug_remove_log_function_by_data(log_fn.0.as_ptr()); gst_sys::gst_debug_remove_log_function_by_data(log_fn.0.as_ptr());
assert_eq!(removed, 1);
} }
} }