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)]
pub struct DebugCategory(ptr::NonNull<gst_sys::GstDebugCategory>);
pub struct DebugCategory(Option<ptr::NonNull<gst_sys::GstDebugCategory>>);
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<T>(
+ 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());
}
}