gstreamer: Reduce allocations in custom logging functions for the file/function strings

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1160>
This commit is contained in:
Sebastian Dröge 2022-12-10 17:22:27 +02:00
parent 0e7f5a19df
commit de04e3f827

View file

@ -423,8 +423,8 @@ impl DebugCategory {
pub fn get_line( pub fn get_line(
&self, &self,
level: crate::DebugLevel, level: crate::DebugLevel,
file: &str, file: &glib::GStr,
module: &str, function: &glib::GStr,
line: u32, line: u32,
object: Option<&LoggedObject>, object: Option<&LoggedObject>,
message: &DebugMessage, message: &DebugMessage,
@ -435,8 +435,8 @@ impl DebugCategory {
from_glib_full(ffi::gst_debug_log_get_line( from_glib_full(ffi::gst_debug_log_get_line(
cat.as_ptr(), cat.as_ptr(),
level.into_glib(), level.into_glib(),
file.to_glib_none().0, file.as_ptr(),
module.to_glib_none().0, function.as_ptr(),
line as i32, line as i32,
object.map(|o| o.as_ptr()).unwrap_or(ptr::null_mut()), object.map(|o| o.as_ptr()).unwrap_or(ptr::null_mut()),
message.0.as_ptr(), message.0.as_ptr(),
@ -840,8 +840,15 @@ unsafe extern "C" fn log_handler<T>(
message: *mut ffi::GstDebugMessage, message: *mut ffi::GstDebugMessage,
user_data: gpointer, user_data: gpointer,
) where ) where
T: Fn(DebugCategory, DebugLevel, &str, &str, u32, Option<&LoggedObject>, &DebugMessage) T: Fn(
+ Send DebugCategory,
DebugLevel,
&glib::GStr,
&glib::GStr,
u32,
Option<&LoggedObject>,
&DebugMessage,
) + Send
+ Sync + Sync
+ 'static, + 'static,
{ {
@ -850,8 +857,8 @@ unsafe extern "C" fn log_handler<T>(
} }
let category = DebugCategory(Some(ptr::NonNull::new_unchecked(category))); 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 = glib::GStr::from_ptr(file);
let function = CStr::from_ptr(function).to_string_lossy(); let function = glib::GStr::from_ptr(function);
let line = line as u32; let line = line as u32;
let object = ptr::NonNull::new(object).map(LoggedObject); let object = ptr::NonNull::new(object).map(LoggedObject);
let message = DebugMessage(ptr::NonNull::new_unchecked(message)); let message = DebugMessage(ptr::NonNull::new_unchecked(message));
@ -859,8 +866,8 @@ unsafe extern "C" fn log_handler<T>(
(handler)( (handler)(
category, category,
level, level,
&file, file,
&function, function,
line, line,
object.as_ref(), object.as_ref(),
&message, &message,
@ -961,8 +968,15 @@ impl fmt::Display for LoggedObject {
#[doc(alias = "gst_debug_add_log_function")] #[doc(alias = "gst_debug_add_log_function")]
pub fn debug_add_log_function<T>(function: T) -> DebugLogFunction pub fn debug_add_log_function<T>(function: T) -> DebugLogFunction
where where
T: Fn(DebugCategory, DebugLevel, &str, &str, u32, Option<&LoggedObject>, &DebugMessage) T: Fn(
+ Send DebugCategory,
DebugLevel,
&glib::GStr,
&glib::GStr,
u32,
Option<&LoggedObject>,
&DebugMessage,
) + Send
+ Sync + Sync
+ 'static, + 'static,
{ {
@ -1075,8 +1089,8 @@ mod tests {
let handler = move |category: DebugCategory, let handler = move |category: DebugCategory,
level: DebugLevel, level: DebugLevel,
_file: &str, _file: &glib::GStr,
_function: &str, _function: &glib::GStr,
_line: u32, _line: u32,
_object: Option<&LoggedObject>, _object: Option<&LoggedObject>,
message: &DebugMessage| { message: &DebugMessage| {