gstreamer: Reduce some allocations for passing the filename/module name to the logging functions

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1160>
This commit is contained in:
Sebastian Dröge 2022-12-10 16:59:43 +02:00
parent 750422d8eb
commit 3a61276cdd
2 changed files with 60 additions and 28 deletions

View file

@ -127,8 +127,8 @@ impl LoggableError {
self.category.log( self.category.log(
None as Option<&glib::Object>, None as Option<&glib::Object>,
crate::DebugLevel::Error, crate::DebugLevel::Error,
self.bool_error.filename, glib::GString::from(self.bool_error.filename).as_gstr(),
self.bool_error.function, glib::GString::from(self.bool_error.function).as_gstr(),
self.bool_error.line, self.bool_error.line,
format_args!("{}", self.bool_error.message), format_args!("{}", self.bool_error.message),
); );
@ -138,8 +138,8 @@ impl LoggableError {
self.category.log( self.category.log(
Some(obj), Some(obj),
crate::DebugLevel::Error, crate::DebugLevel::Error,
self.bool_error.filename, glib::GString::from(self.bool_error.filename).as_gstr(),
self.bool_error.function, glib::GString::from(self.bool_error.function).as_gstr(),
self.bool_error.line, self.bool_error.line,
format_args!("{}", self.bool_error.message), format_args!("{}", self.bool_error.message),
); );
@ -151,8 +151,8 @@ impl LoggableError {
self.category.log( self.category.log(
Some(unsafe { imp.obj().unsafe_cast_ref::<glib::Object>() }), Some(unsafe { imp.obj().unsafe_cast_ref::<glib::Object>() }),
crate::DebugLevel::Error, crate::DebugLevel::Error,
self.bool_error.filename, glib::GString::from(self.bool_error.filename).as_gstr(),
self.bool_error.function, glib::GString::from(self.bool_error.function).as_gstr(),
self.bool_error.line, self.bool_error.line,
format_args!("{}", self.bool_error.message), format_args!("{}", self.bool_error.message),
); );

View file

@ -170,8 +170,8 @@ impl DebugCategory {
self, self,
obj: Option<&O>, obj: Option<&O>,
level: crate::DebugLevel, level: crate::DebugLevel,
file: &str, file: &glib::GStr,
module: &str, module: &glib::GStr,
line: u32, line: u32,
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
@ -198,8 +198,8 @@ impl DebugCategory {
self, self,
obj: Option<&O>, obj: Option<&O>,
level: crate::DebugLevel, level: crate::DebugLevel,
file: &str, file: &glib::GStr,
module: &str, module: &glib::GStr,
line: u32, line: u32,
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
@ -225,8 +225,8 @@ impl DebugCategory {
ffi::gst_debug_log_literal( ffi::gst_debug_log_literal(
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, module.as_ptr(),
line as i32, line as i32,
obj_ptr, obj_ptr,
w.into_string().to_glib_none().0, w.into_string().to_glib_none().0,
@ -237,8 +237,8 @@ impl DebugCategory {
ffi::gst_debug_log( ffi::gst_debug_log(
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, module.as_ptr(),
line as i32, line as i32,
obj_ptr, obj_ptr,
b"%s\0".as_ptr() as *const _, b"%s\0".as_ptr() as *const _,
@ -256,8 +256,8 @@ impl DebugCategory {
self, self,
id: Option<&str>, id: Option<&str>,
level: crate::DebugLevel, level: crate::DebugLevel,
file: &str, file: &glib::GStr,
module: &str, module: &glib::GStr,
line: u32, line: u32,
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
@ -293,8 +293,8 @@ impl DebugCategory {
self, self,
id: Option<&str>, id: Option<&str>,
level: crate::DebugLevel, level: crate::DebugLevel,
file: &str, file: &glib::GStr,
module: &str, module: &glib::GStr,
line: u32, line: u32,
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
@ -314,8 +314,8 @@ impl DebugCategory {
ffi::gst_debug_log_id_literal( ffi::gst_debug_log_id_literal(
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, module.as_ptr(),
line as i32, line as i32,
id.to_glib_none().0, id.to_glib_none().0,
w.into_string().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)*) => { { ($cat:expr, level: $level:expr, obj: $obj:expr, $($args:tt)*) => { {
// Check the log level before using `format_args!` otherwise // Check the log level before using `format_args!` otherwise
// formatted arguments are evaluated even if we end up not logging. // formatted arguments are evaluated even if we end up not logging.
#[allow(unused_unsafe)]
if $level <= $cat.threshold() { if $level <= $cat.threshold() {
use $crate::glib::Cast; use $crate::glib::Cast;
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_unfiltered($cat.clone(), Some(obj), $crate::DebugCategory::log_unfiltered(
$level, file!(), module_path!(), line!(), format_args!($($args)*)) $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)*) => { { ($cat:expr, level: $level:expr, imp: $imp:expr, $($args:tt)*) => { {
// Check the log level before using `format_args!` otherwise // Check the log level before using `format_args!` otherwise
// formatted arguments are evaluated even if we end up not logging. // formatted arguments are evaluated even if we end up not logging.
#[allow(unused_unsafe)]
if $level <= $cat.threshold() { if $level <= $cat.threshold() {
use $crate::glib::Cast; use $crate::glib::Cast;
let obj = $imp.obj(); let obj = $imp.obj();
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_unfiltered($cat.clone(), Some(obj), $crate::DebugCategory::log_unfiltered(
$level, file!(), module_path!(), line!(), format_args!($($args)*)) $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)*) => { { ($cat:expr, level: $level:expr, id: $id:expr, $($args:tt)*) => { {
// Check the log level before using `format_args!` otherwise // Check the log level before using `format_args!` otherwise
// formatted arguments are evaluated even if we end up not logging. // formatted arguments are evaluated even if we end up not logging.
#[allow(unused_unsafe)]
if $level <= $cat.threshold() { if $level <= $cat.threshold() {
$crate::DebugCategory::log_id_unfiltered($cat.clone(), Some($id), $crate::DebugCategory::log_id_unfiltered(
$level, file!(), module_path!(), line!(), format_args!($($args)*)) $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)*) => { { ($cat:expr, level: $level:expr, $($args:tt)*) => { {
// Check the log level before using `format_args!` otherwise // Check the log level before using `format_args!` otherwise
// formatted arguments are evaluated even if we end up not logging. // formatted arguments are evaluated even if we end up not logging.
#[allow(unused_unsafe)]
if $level <= $cat.threshold() { if $level <= $cat.threshold() {
$crate::DebugCategory::log_unfiltered($cat.clone(), None as Option<&$crate::glib::Object>, $crate::DebugCategory::log_unfiltered(
$level, file!(), module_path!(), line!(), format_args!($($args)*)) $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)*),
)
} }
}}; }};
); );