gstreamer: Actually log the function name instead of the module name

As there is no way to get the function name as a string literal
currently, we have to copy locally into a NUL-terminated array.
Up to 256 bytes this is managed on the stack, otherwise a heap
allocation is necessary.

For the <256 bytes case the compiler is optimizing most of this away.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1160>
This commit is contained in:
Sebastian Dröge 2022-12-10 19:57:22 +02:00
parent 355ebc243f
commit ef5d5f50e4

View file

@ -637,13 +637,25 @@ macro_rules! log_with_level(
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_literal_unfiltered( $crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some(obj), Some(obj),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
@ -656,13 +668,25 @@ macro_rules! log_with_level(
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_unfiltered( $crate::DebugCategory::log_unfiltered(
$cat.clone(), $cat.clone(),
Some(obj), Some(obj),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
format_args!($($args)*), format_args!($($args)*),
) )
@ -675,6 +699,18 @@ macro_rules! log_with_level(
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
let obj = $imp.obj(); let obj = $imp.obj();
let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_literal_unfiltered( $crate::DebugCategory::log_literal_unfiltered(
@ -682,7 +718,7 @@ macro_rules! log_with_level(
Some(obj), Some(obj),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
@ -695,6 +731,18 @@ macro_rules! log_with_level(
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
let obj = $imp.obj(); let obj = $imp.obj();
let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { obj.unsafe_cast_ref::<$crate::glib::Object>() };
$crate::DebugCategory::log_unfiltered( $crate::DebugCategory::log_unfiltered(
@ -702,7 +750,7 @@ macro_rules! log_with_level(
Some(obj), Some(obj),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
format_args!($($args)*), format_args!($($args)*),
) )
@ -713,12 +761,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_id_literal_unfiltered( $crate::DebugCategory::log_id_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some($crate::glib::gstr!($id)), Some($crate::glib::gstr!($id)),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
@ -729,12 +789,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_id_unfiltered( $crate::DebugCategory::log_id_unfiltered(
$cat.clone(), $cat.clone(),
Some($crate::glib::gstr!($id)), Some($crate::glib::gstr!($id)),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
format_args!($($args)*), format_args!($($args)*),
) )
@ -745,12 +817,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_id_literal_unfiltered( $crate::DebugCategory::log_id_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some($id), Some($id),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
@ -761,12 +845,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_id_unfiltered( $crate::DebugCategory::log_id_unfiltered(
$cat.clone(), $cat.clone(),
Some($id), Some($id),
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
format_args!($($args)*), format_args!($($args)*),
) )
@ -777,12 +873,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_literal_unfiltered( $crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), $cat.clone(),
None as Option<&$crate::glib::Object>, None as Option<&$crate::glib::Object>,
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
@ -793,12 +901,24 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
// FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication
let function_name = $crate::glib::function_name!();
let function_name_len = function_name.len();
let mut storage = [0u8; 256];
let function_name = if function_name_len < 256 {
storage[0..function_name_len].copy_from_slice(function_name.as_bytes());
::std::borrow::Cow::Borrowed(unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(&storage[..function_name_len+1]) })
} else {
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
};
$crate::DebugCategory::log_unfiltered( $crate::DebugCategory::log_unfiltered(
$cat.clone(), $cat.clone(),
None as Option<&$crate::glib::Object>, None as Option<&$crate::glib::Object>,
$level, $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!(file!(), "\0").as_bytes()) },
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(module_path!(), "\0").as_bytes()) }, function_name.as_ref(),
line!(), line!(),
format_args!($($args)*), format_args!($($args)*),
) )