From 3a61276cdd7bf3ac247751a48793c62a910763c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 10 Dec 2022 16:59:43 +0200 Subject: [PATCH] gstreamer: Reduce some allocations for passing the filename/module name to the logging functions Part-of: --- gstreamer/src/error.rs | 12 +++---- gstreamer/src/log.rs | 76 ++++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/gstreamer/src/error.rs b/gstreamer/src/error.rs index 863ff8df9..1c7fa9f45 100644 --- a/gstreamer/src/error.rs +++ b/gstreamer/src/error.rs @@ -127,8 +127,8 @@ impl LoggableError { self.category.log( None as Option<&glib::Object>, crate::DebugLevel::Error, - self.bool_error.filename, - self.bool_error.function, + glib::GString::from(self.bool_error.filename).as_gstr(), + glib::GString::from(self.bool_error.function).as_gstr(), self.bool_error.line, format_args!("{}", self.bool_error.message), ); @@ -138,8 +138,8 @@ impl LoggableError { self.category.log( Some(obj), crate::DebugLevel::Error, - self.bool_error.filename, - self.bool_error.function, + glib::GString::from(self.bool_error.filename).as_gstr(), + glib::GString::from(self.bool_error.function).as_gstr(), self.bool_error.line, format_args!("{}", self.bool_error.message), ); @@ -151,8 +151,8 @@ impl LoggableError { self.category.log( Some(unsafe { imp.obj().unsafe_cast_ref::() }), crate::DebugLevel::Error, - self.bool_error.filename, - self.bool_error.function, + glib::GString::from(self.bool_error.filename).as_gstr(), + glib::GString::from(self.bool_error.function).as_gstr(), self.bool_error.line, format_args!("{}", self.bool_error.message), ); diff --git a/gstreamer/src/log.rs b/gstreamer/src/log.rs index c4736f405..3195be0f7 100644 --- a/gstreamer/src/log.rs +++ b/gstreamer/src/log.rs @@ -170,8 +170,8 @@ impl DebugCategory { self, obj: Option<&O>, level: crate::DebugLevel, - file: &str, - module: &str, + file: &glib::GStr, + module: &glib::GStr, line: u32, args: fmt::Arguments, ) { @@ -198,8 +198,8 @@ impl DebugCategory { self, obj: Option<&O>, level: crate::DebugLevel, - file: &str, - module: &str, + file: &glib::GStr, + module: &glib::GStr, line: u32, args: fmt::Arguments, ) { @@ -225,8 +225,8 @@ impl DebugCategory { ffi::gst_debug_log_literal( cat.as_ptr(), level.into_glib(), - file.to_glib_none().0, - module.to_glib_none().0, + file.as_ptr(), + module.as_ptr(), line as i32, obj_ptr, w.into_string().to_glib_none().0, @@ -237,8 +237,8 @@ impl DebugCategory { ffi::gst_debug_log( cat.as_ptr(), level.into_glib(), - file.to_glib_none().0, - module.to_glib_none().0, + file.as_ptr(), + module.as_ptr(), line as i32, obj_ptr, b"%s\0".as_ptr() as *const _, @@ -256,8 +256,8 @@ impl DebugCategory { self, id: Option<&str>, level: crate::DebugLevel, - file: &str, - module: &str, + file: &glib::GStr, + module: &glib::GStr, line: u32, args: fmt::Arguments, ) { @@ -293,8 +293,8 @@ impl DebugCategory { self, id: Option<&str>, level: crate::DebugLevel, - file: &str, - module: &str, + file: &glib::GStr, + module: &glib::GStr, line: u32, args: fmt::Arguments, ) { @@ -314,8 +314,8 @@ impl DebugCategory { ffi::gst_debug_log_id_literal( cat.as_ptr(), level.into_glib(), - file.to_glib_none().0, - module.to_glib_none().0, + file.as_ptr(), + module.as_ptr(), line as i32, id.to_glib_none().0, w.into_string().to_glib_none().0, @@ -565,42 +565,74 @@ macro_rules! log_with_level( ($cat:expr, level: $level:expr, obj: $obj:expr, $($args:tt)*) => { { // Check the log level before using `format_args!` otherwise // formatted arguments are evaluated even if we end up not logging. + #[allow(unused_unsafe)] if $level <= $cat.threshold() { use $crate::glib::Cast; #[allow(unused_unsafe)] let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() }; - $crate::DebugCategory::log_unfiltered($cat.clone(), Some(obj), - $level, file!(), module_path!(), line!(), format_args!($($args)*)) + $crate::DebugCategory::log_unfiltered( + $cat.clone(), + Some(obj), + $level, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, + line!(), + format_args!($($args)*), + ) } }}; ($cat:expr, level: $level:expr, imp: $imp:expr, $($args:tt)*) => { { // Check the log level before using `format_args!` otherwise // formatted arguments are evaluated even if we end up not logging. + #[allow(unused_unsafe)] if $level <= $cat.threshold() { use $crate::glib::Cast; let obj = $imp.obj(); #[allow(unused_unsafe)] let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() }; - $crate::DebugCategory::log_unfiltered($cat.clone(), Some(obj), - $level, file!(), module_path!(), line!(), format_args!($($args)*)) + $crate::DebugCategory::log_unfiltered( + $cat.clone(), + Some(obj), + $level, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, + line!(), + format_args!($($args)*), + ) } }}; ($cat:expr, level: $level:expr, id: $id:expr, $($args:tt)*) => { { // Check the log level before using `format_args!` otherwise // formatted arguments are evaluated even if we end up not logging. + #[allow(unused_unsafe)] if $level <= $cat.threshold() { - $crate::DebugCategory::log_id_unfiltered($cat.clone(), Some($id), - $level, file!(), module_path!(), line!(), format_args!($($args)*)) + $crate::DebugCategory::log_id_unfiltered( + $cat.clone(), + Some($id), + $level, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, + line!(), + format_args!($($args)*), + ) } }}; ($cat:expr, level: $level:expr, $($args:tt)*) => { { // Check the log level before using `format_args!` otherwise // formatted arguments are evaluated even if we end up not logging. + #[allow(unused_unsafe)] if $level <= $cat.threshold() { - $crate::DebugCategory::log_unfiltered($cat.clone(), None as Option<&$crate::glib::Object>, - $level, file!(), module_path!(), line!(), format_args!($($args)*)) + $crate::DebugCategory::log_unfiltered( + $cat.clone(), + None as Option<&$crate::glib::Object>, + $level, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, + unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, + line!(), + format_args!($($args)*), + ) } }}; );