gstreamer: Simplify and speed up log message string construction

For pre-1.20 simply use `%s` as format string instead of escaping the
`%` inline while writing. This allows a simpler implementation and is
also faster, see https://github.com/gtk-rs/gtk-rs-core/pull/583.
This commit is contained in:
Sebastian Dröge 2022-02-26 13:31:35 +02:00
parent ab04715dc7
commit 319f1c68a5

View file

@ -180,70 +180,37 @@ impl DebugCategory {
None => ptr::null_mut(), None => ptr::null_mut(),
}; };
let mut w = glib::GStringBuilder::default();
// Can't really happen but better safe than sorry
if fmt::write(&mut w, args).is_err() {
return;
}
#[cfg(feature = "v1_20")] #[cfg(feature = "v1_20")]
{ unsafe {
let mut w = glib::GStringBuilder::default(); ffi::gst_debug_log_literal(
cat.as_ptr(),
// Can't really happen but better safe than sorry level.into_glib(),
if fmt::write(&mut w, args).is_err() { file.to_glib_none().0,
return; module.to_glib_none().0,
} line as i32,
obj_ptr,
unsafe { w.into_string().to_glib_none().0,
ffi::gst_debug_log_literal( );
cat.as_ptr(),
level.into_glib(),
file.to_glib_none().0,
module.to_glib_none().0,
line as i32,
obj_ptr,
w.into_string().to_glib_none().0,
);
}
} }
#[cfg(not(feature = "v1_20"))] #[cfg(not(feature = "v1_20"))]
{ unsafe {
// Replace literal percentage signs with two so that they are not interpreted as printf ffi::gst_debug_log(
// format specifiers cat.as_ptr(),
struct Write(glib::GStringBuilder); level.into_glib(),
impl fmt::Write for Write { file.to_glib_none().0,
fn write_str(&mut self, mut s: &str) -> Result<(), fmt::Error> { module.to_glib_none().0,
while let Some((prefix, suffix)) = s.split_once('%') { line as i32,
self.0.append(prefix); obj_ptr,
self.0.append("%%"); b"%s\0".as_ptr() as *const _,
s = suffix; ToGlibPtr::<*const i8>::to_glib_none(&w.into_string()).0,
} );
self.0.append(s);
Ok(())
}
fn write_char(&mut self, c: char) -> fmt::Result {
if c == '%' {
self.0.append("%%");
} else {
self.0.append_c(c);
}
Ok(())
}
}
let mut w = Write(glib::GStringBuilder::default());
// Can't really happen but better safe than sorry
if fmt::write(&mut w, args).is_err() {
return;
}
unsafe {
ffi::gst_debug_log(
cat.as_ptr(),
level.into_glib(),
file.to_glib_none().0,
module.to_glib_none().0,
line as i32,
obj_ptr,
w.0.into_string().to_glib_none().0,
);
}
} }
} }