gstreamer: Fix formatting if a string literal needs formatting

E.g. "blabla {some_variable}" is a string literal but needs formatting,
so we can't just pass it as a literal to the logger.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1161>
This commit is contained in:
Sebastian Dröge 2022-12-11 11:16:27 +02:00
parent 2a935320e7
commit 0fa8d0d62f

View file

@ -634,6 +634,7 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
#[allow(clippy::redundant_closure_call)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
@ -650,6 +651,12 @@ macro_rules! log_with_level(
}; };
let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() }; let obj = unsafe { $obj.unsafe_cast_ref::<$crate::glib::Object>() };
// Check if formatting is necessary or not
// FIXME: This needs to be a closure because the return value of format_args!() can't
// be assigned to a variable
(|args: std::fmt::Arguments| {
if args.as_str().is_some() {
$crate::DebugCategory::log_literal_unfiltered( $crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some(obj), Some(obj),
@ -659,6 +666,18 @@ macro_rules! log_with_level(
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
} else {
$crate::DebugCategory::log_unfiltered(
$cat.clone(),
Some(obj),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
args,
)
}
})(format_args!($msg))
} }
}}; }};
($cat:expr, level: $level:expr, obj: $obj:expr, $($args:tt)*) => { { ($cat:expr, level: $level:expr, obj: $obj:expr, $($args:tt)*) => { {
@ -696,6 +715,7 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
#[allow(clippy::redundant_closure_call)]
if $cat.above_threshold($level) { if $cat.above_threshold($level) {
use $crate::glib::Cast; use $crate::glib::Cast;
@ -713,6 +733,12 @@ macro_rules! log_with_level(
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>() };
// Check if formatting is necessary or not
// FIXME: This needs to be a closure because the return value of format_args!() can't
// be assigned to a variable
(|args: std::fmt::Arguments| {
if args.as_str().is_some() {
$crate::DebugCategory::log_literal_unfiltered( $crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some(obj), Some(obj),
@ -722,6 +748,18 @@ macro_rules! log_with_level(
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
} else {
$crate::DebugCategory::log_unfiltered(
$cat.clone(),
Some(obj),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
args,
)
}
})(format_args!($msg))
} }
}}; }};
($cat:expr, level: $level:expr, imp: $imp:expr, $($args:tt)*) => { { ($cat:expr, level: $level:expr, imp: $imp:expr, $($args:tt)*) => { {
@ -760,6 +798,7 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
#[allow(clippy::redundant_closure_call)]
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 // FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication // avoid this complication
@ -773,6 +812,11 @@ macro_rules! log_with_level(
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name)) ::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
}; };
// Check if formatting is necessary or not
// FIXME: This needs to be a closure because the return value of format_args!() can't
// be assigned to a variable
(|args: std::fmt::Arguments| {
if args.as_str().is_some() {
$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)),
@ -782,6 +826,18 @@ macro_rules! log_with_level(
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
} else {
$crate::DebugCategory::log_id_unfiltered(
$cat.clone(),
Some($crate::glib::gstr!($id)),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
args,
)
}
})(format_args!($msg))
} }
}}; }};
($cat:expr, level: $level:expr, id: $id:literal, $($args:tt)*) => { { ($cat:expr, level: $level:expr, id: $id:literal, $($args:tt)*) => { {
@ -816,6 +872,7 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
#[allow(clippy::redundant_closure_call)]
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 // FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication // avoid this complication
@ -829,6 +886,11 @@ macro_rules! log_with_level(
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name)) ::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
}; };
// Check if formatting is necessary or not
// FIXME: This needs to be a closure because the return value of format_args!() can't
// be assigned to a variable
(|args: std::fmt::Arguments| {
if args.as_str().is_some() {
$crate::DebugCategory::log_id_literal_unfiltered( $crate::DebugCategory::log_id_literal_unfiltered(
$cat.clone(), $cat.clone(),
Some($id), Some($id),
@ -838,6 +900,18 @@ macro_rules! log_with_level(
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
} else {
$crate::DebugCategory::log_id_unfiltered(
$cat.clone(),
Some($id),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
args,
)
}
})(format_args!($msg))
} }
}}; }};
($cat:expr, level: $level:expr, id: $id:expr, $($args:tt)*) => { { ($cat:expr, level: $level:expr, id: $id:expr, $($args:tt)*) => { {
@ -872,6 +946,7 @@ macro_rules! log_with_level(
// 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)] #[allow(unused_unsafe)]
#[allow(clippy::redundant_closure_call)]
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 // FIXME: Once there's a function_name! macro that returns a string literal we can
// avoid this complication // avoid this complication
@ -885,6 +960,11 @@ macro_rules! log_with_level(
::std::borrow::Cow::Owned($crate::glib::GString::from(function_name)) ::std::borrow::Cow::Owned($crate::glib::GString::from(function_name))
}; };
// Check if formatting is necessary or not
// FIXME: This needs to be a closure because the return value of format_args!() can't
// be assigned to a variable
(|args: std::fmt::Arguments| {
if args.as_str().is_some() {
$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>,
@ -894,6 +974,18 @@ macro_rules! log_with_level(
line!(), line!(),
$crate::glib::gstr!($msg), $crate::glib::gstr!($msg),
) )
} else {
$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()) },
function_name.as_ref(),
line!(),
args,
)
}
})(format_args!($msg))
} }
}}; }};
($cat:expr, level: $level:expr, $($args:tt)*) => { { ($cat:expr, level: $level:expr, $($args:tt)*) => { {