From c987bb0c7ddf3d73c0b7f30395373451d7ae0006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Fri, 9 Oct 2020 21:58:22 +0200 Subject: [PATCH] gstreamer/message: enhance Debug impl for Message[Ref] When "{:?}" printing a Message[Ref], the following issues lower the experience: - If the Message seqnum is GST_SEQNUM_INVALID (0), a panic occurs due to an assertion failure in MessageRef::get_seqnum. - The src of the Message displays the GString address. Origin issue for an occurrence of the first case above fixed in https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/860 --- gstreamer/src/message.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 2fd56f332..f354131c9 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -140,14 +140,26 @@ impl fmt::Debug for Message { impl fmt::Debug for MessageRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // Don't retrieve `seqnum` using `MessageRef::get_seqnum` + // because it would generate a new seqnum if a buggy `Element` + // emitted a `Message` with an invalid `seqnum`. + // We want to help the user find out there is something wrong here, + // so they can investigate the origin. + let seqnum = unsafe { gst_sys::gst_message_get_seqnum(self.as_mut_ptr()) }; + let seqnum = if seqnum != 0 { + &seqnum as &dyn fmt::Debug + } else { + &"INVALID (0)" as &dyn fmt::Debug + }; + f.debug_struct("Message") .field("ptr", unsafe { &self.as_ptr() }) .field("type", &unsafe { let type_ = gst_sys::gst_message_type_get_name((*self.as_ptr()).type_); CStr::from_ptr(type_).to_str().unwrap() }) - .field("seqnum", &self.get_seqnum()) - .field("src", &self.get_src().map(|s| s.get_name())) + .field("seqnum", seqnum) + .field("src", &self.get_src().map(|s| s.get_name().to_owned())) .field("structure", &self.get_structure()) .finish() }