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,15 +651,33 @@ 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>() };
$crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), // Check if formatting is necessary or not
Some(obj), // FIXME: This needs to be a closure because the return value of format_args!() can't
$level, // be assigned to a variable
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, (|args: std::fmt::Arguments| {
function_name.as_ref(), if args.as_str().is_some() {
line!(), $crate::DebugCategory::log_literal_unfiltered(
$crate::glib::gstr!($msg), $cat.clone(),
) Some(obj),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
$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,15 +733,33 @@ 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>() };
$crate::DebugCategory::log_literal_unfiltered(
$cat.clone(), // Check if formatting is necessary or not
Some(obj), // FIXME: This needs to be a closure because the return value of format_args!() can't
$level, // be assigned to a variable
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, (|args: std::fmt::Arguments| {
function_name.as_ref(), if args.as_str().is_some() {
line!(), $crate::DebugCategory::log_literal_unfiltered(
$crate::glib::gstr!($msg), $cat.clone(),
) Some(obj),
$level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
$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,15 +812,32 @@ 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))
}; };
$crate::DebugCategory::log_id_literal_unfiltered( // Check if formatting is necessary or not
$cat.clone(), // FIXME: This needs to be a closure because the return value of format_args!() can't
Some($crate::glib::gstr!($id)), // be assigned to a variable
$level, (|args: std::fmt::Arguments| {
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, if args.as_str().is_some() {
function_name.as_ref(), $crate::DebugCategory::log_id_literal_unfiltered(
line!(), $cat.clone(),
$crate::glib::gstr!($msg), 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!(),
$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,15 +886,32 @@ 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))
}; };
$crate::DebugCategory::log_id_literal_unfiltered( // Check if formatting is necessary or not
$cat.clone(), // FIXME: This needs to be a closure because the return value of format_args!() can't
Some($id), // be assigned to a variable
$level, (|args: std::fmt::Arguments| {
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, if args.as_str().is_some() {
function_name.as_ref(), $crate::DebugCategory::log_id_literal_unfiltered(
line!(), $cat.clone(),
$crate::glib::gstr!($msg), Some($id),
) $level,
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) },
function_name.as_ref(),
line!(),
$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,15 +960,32 @@ 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))
}; };
$crate::DebugCategory::log_literal_unfiltered( // Check if formatting is necessary or not
$cat.clone(), // FIXME: This needs to be a closure because the return value of format_args!() can't
None as Option<&$crate::glib::Object>, // be assigned to a variable
$level, (|args: std::fmt::Arguments| {
unsafe { $crate::glib::GStr::from_bytes_with_nul_unchecked(concat!(file!(), "\0").as_bytes()) }, if args.as_str().is_some() {
function_name.as_ref(), $crate::DebugCategory::log_literal_unfiltered(
line!(), $cat.clone(),
$crate::glib::gstr!($msg), 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!(),
$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)*) => { {