From 55ed9e054665ba303e1fb381c7ac590056da7724 Mon Sep 17 00:00:00 2001 From: GrigorenkoPV Date: Fri, 4 Oct 2024 10:26:14 +0300 Subject: [PATCH] fix(aya-log): print &[u8] using full width (#1008) Otherwise `&[1u8, 0u8]` cannot be distinguished from `&[0x10u8]` (they both become 10) --- aya-log/src/lib.rs | 56 ++++++++++++------ xtask/public-api/aya-log.txt | 108 +++++++++++++++++------------------ 2 files changed, 94 insertions(+), 70 deletions(-) diff --git a/aya-log/src/lib.rs b/aya-log/src/lib.rs index 52a89756..610d37b6 100644 --- a/aya-log/src/lib.rs +++ b/aya-log/src/lib.rs @@ -205,15 +205,12 @@ where } } -pub struct LowerHexDebugFormatter; -impl Formatter<&[T]> for LowerHexDebugFormatter -where - T: LowerHex, -{ - fn format(v: &[T]) -> String { +pub struct LowerHexBytesFormatter; +impl Formatter<&[u8]> for LowerHexBytesFormatter { + fn format(v: &[u8]) -> String { let mut s = String::new(); for v in v { - let () = core::fmt::write(&mut s, format_args!("{v:x}")).unwrap(); + let () = core::fmt::write(&mut s, format_args!("{v:02x}")).unwrap(); } s } @@ -229,15 +226,12 @@ where } } -pub struct UpperHexDebugFormatter; -impl Formatter<&[T]> for UpperHexDebugFormatter -where - T: UpperHex, -{ - fn format(v: &[T]) -> String { +pub struct UpperHexBytesFormatter; +impl Formatter<&[u8]> for UpperHexBytesFormatter { + fn format(v: &[u8]) -> String { let mut s = String::new(); for v in v { - let () = core::fmt::write(&mut s, format_args!("{v:X}")).unwrap(); + let () = core::fmt::write(&mut s, format_args!("{v:02X}")).unwrap(); } s } @@ -290,8 +284,8 @@ trait Format { impl Format for &[u8] { fn format(&self, last_hint: Option) -> Result { match last_hint.map(|DisplayHintWrapper(dh)| dh) { - Some(DisplayHint::LowerHex) => Ok(LowerHexDebugFormatter::format(self)), - Some(DisplayHint::UpperHex) => Ok(UpperHexDebugFormatter::format(self)), + Some(DisplayHint::LowerHex) => Ok(LowerHexBytesFormatter::format(self)), + Some(DisplayHint::UpperHex) => Ok(UpperHexBytesFormatter::format(self)), _ => Err(()), } } @@ -782,6 +776,36 @@ mod test { }); } + #[test] + fn test_bytes_unambiguous() { + testing_logger::setup(); + let (mut len, mut input) = new_log(5).unwrap(); + + len += DisplayHint::LowerHex + .write(&mut input[len..]) + .unwrap() + .get(); + len += [0x01, 0x02].write(&mut input[len..]).unwrap().get(); + + len += " ".write(&mut input[len..]).unwrap().get(); + + len += DisplayHint::LowerHex + .write(&mut input[len..]) + .unwrap() + .get(); + len += [0x12].write(&mut input[len..]).unwrap().get(); + + _ = len; + + let logger = logger(); + let () = log_buf(&input, logger).unwrap(); + testing_logger::validate(|captured_logs| { + assert_eq!(captured_logs.len(), 1); + assert_eq!(captured_logs[0].body, "0102 12"); + assert_eq!(captured_logs[0].level, Level::Info); + }); + } + #[test] fn test_display_hint_default() { testing_logger::setup(); diff --git a/xtask/public-api/aya-log.txt b/xtask/public-api/aya-log.txt index 8c04980e..3e5bdf8c 100644 --- a/xtask/public-api/aya-log.txt +++ b/xtask/public-api/aya-log.txt @@ -145,31 +145,31 @@ impl core::borrow::BorrowMut for aya_log::Ipv6Formatter where T: core::mar pub fn aya_log::Ipv6Formatter::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_log::Ipv6Formatter pub fn aya_log::Ipv6Formatter::from(t: T) -> T -pub struct aya_log::LowerHexDebugFormatter -impl aya_log::Formatter<&[T]> for aya_log::LowerHexDebugFormatter where T: core::fmt::LowerHex -pub fn aya_log::LowerHexDebugFormatter::format(v: &[T]) -> alloc::string::String -impl core::marker::Freeze for aya_log::LowerHexDebugFormatter -impl core::marker::Send for aya_log::LowerHexDebugFormatter -impl core::marker::Sync for aya_log::LowerHexDebugFormatter -impl core::marker::Unpin for aya_log::LowerHexDebugFormatter -impl core::panic::unwind_safe::RefUnwindSafe for aya_log::LowerHexDebugFormatter -impl core::panic::unwind_safe::UnwindSafe for aya_log::LowerHexDebugFormatter -impl core::convert::Into for aya_log::LowerHexDebugFormatter where U: core::convert::From -pub fn aya_log::LowerHexDebugFormatter::into(self) -> U -impl core::convert::TryFrom for aya_log::LowerHexDebugFormatter where U: core::convert::Into -pub type aya_log::LowerHexDebugFormatter::Error = core::convert::Infallible -pub fn aya_log::LowerHexDebugFormatter::try_from(value: U) -> core::result::Result>::Error> -impl core::convert::TryInto for aya_log::LowerHexDebugFormatter where U: core::convert::TryFrom -pub type aya_log::LowerHexDebugFormatter::Error = >::Error -pub fn aya_log::LowerHexDebugFormatter::try_into(self) -> core::result::Result>::Error> -impl core::any::Any for aya_log::LowerHexDebugFormatter where T: 'static + core::marker::Sized -pub fn aya_log::LowerHexDebugFormatter::type_id(&self) -> core::any::TypeId -impl core::borrow::Borrow for aya_log::LowerHexDebugFormatter where T: core::marker::Sized -pub fn aya_log::LowerHexDebugFormatter::borrow(&self) -> &T -impl core::borrow::BorrowMut for aya_log::LowerHexDebugFormatter where T: core::marker::Sized -pub fn aya_log::LowerHexDebugFormatter::borrow_mut(&mut self) -> &mut T -impl core::convert::From for aya_log::LowerHexDebugFormatter -pub fn aya_log::LowerHexDebugFormatter::from(t: T) -> T +pub struct aya_log::LowerHexBytesFormatter +impl aya_log::Formatter<&[u8]> for aya_log::LowerHexBytesFormatter +pub fn aya_log::LowerHexBytesFormatter::format(v: &[u8]) -> alloc::string::String +impl core::marker::Freeze for aya_log::LowerHexBytesFormatter +impl core::marker::Send for aya_log::LowerHexBytesFormatter +impl core::marker::Sync for aya_log::LowerHexBytesFormatter +impl core::marker::Unpin for aya_log::LowerHexBytesFormatter +impl core::panic::unwind_safe::RefUnwindSafe for aya_log::LowerHexBytesFormatter +impl core::panic::unwind_safe::UnwindSafe for aya_log::LowerHexBytesFormatter +impl core::convert::Into for aya_log::LowerHexBytesFormatter where U: core::convert::From +pub fn aya_log::LowerHexBytesFormatter::into(self) -> U +impl core::convert::TryFrom for aya_log::LowerHexBytesFormatter where U: core::convert::Into +pub type aya_log::LowerHexBytesFormatter::Error = core::convert::Infallible +pub fn aya_log::LowerHexBytesFormatter::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_log::LowerHexBytesFormatter where U: core::convert::TryFrom +pub type aya_log::LowerHexBytesFormatter::Error = >::Error +pub fn aya_log::LowerHexBytesFormatter::try_into(self) -> core::result::Result>::Error> +impl core::any::Any for aya_log::LowerHexBytesFormatter where T: 'static + core::marker::Sized +pub fn aya_log::LowerHexBytesFormatter::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_log::LowerHexBytesFormatter where T: core::marker::Sized +pub fn aya_log::LowerHexBytesFormatter::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_log::LowerHexBytesFormatter where T: core::marker::Sized +pub fn aya_log::LowerHexBytesFormatter::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_log::LowerHexBytesFormatter +pub fn aya_log::LowerHexBytesFormatter::from(t: T) -> T pub struct aya_log::LowerHexFormatter impl aya_log::Formatter for aya_log::LowerHexFormatter where T: core::fmt::LowerHex pub fn aya_log::LowerHexFormatter::format(v: T) -> alloc::string::String @@ -220,31 +220,31 @@ impl core::borrow::BorrowMut for aya_log::LowerMacFormatter where T: core: pub fn aya_log::LowerMacFormatter::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_log::LowerMacFormatter pub fn aya_log::LowerMacFormatter::from(t: T) -> T -pub struct aya_log::UpperHexDebugFormatter -impl aya_log::Formatter<&[T]> for aya_log::UpperHexDebugFormatter where T: core::fmt::UpperHex -pub fn aya_log::UpperHexDebugFormatter::format(v: &[T]) -> alloc::string::String -impl core::marker::Freeze for aya_log::UpperHexDebugFormatter -impl core::marker::Send for aya_log::UpperHexDebugFormatter -impl core::marker::Sync for aya_log::UpperHexDebugFormatter -impl core::marker::Unpin for aya_log::UpperHexDebugFormatter -impl core::panic::unwind_safe::RefUnwindSafe for aya_log::UpperHexDebugFormatter -impl core::panic::unwind_safe::UnwindSafe for aya_log::UpperHexDebugFormatter -impl core::convert::Into for aya_log::UpperHexDebugFormatter where U: core::convert::From -pub fn aya_log::UpperHexDebugFormatter::into(self) -> U -impl core::convert::TryFrom for aya_log::UpperHexDebugFormatter where U: core::convert::Into -pub type aya_log::UpperHexDebugFormatter::Error = core::convert::Infallible -pub fn aya_log::UpperHexDebugFormatter::try_from(value: U) -> core::result::Result>::Error> -impl core::convert::TryInto for aya_log::UpperHexDebugFormatter where U: core::convert::TryFrom -pub type aya_log::UpperHexDebugFormatter::Error = >::Error -pub fn aya_log::UpperHexDebugFormatter::try_into(self) -> core::result::Result>::Error> -impl core::any::Any for aya_log::UpperHexDebugFormatter where T: 'static + core::marker::Sized -pub fn aya_log::UpperHexDebugFormatter::type_id(&self) -> core::any::TypeId -impl core::borrow::Borrow for aya_log::UpperHexDebugFormatter where T: core::marker::Sized -pub fn aya_log::UpperHexDebugFormatter::borrow(&self) -> &T -impl core::borrow::BorrowMut for aya_log::UpperHexDebugFormatter where T: core::marker::Sized -pub fn aya_log::UpperHexDebugFormatter::borrow_mut(&mut self) -> &mut T -impl core::convert::From for aya_log::UpperHexDebugFormatter -pub fn aya_log::UpperHexDebugFormatter::from(t: T) -> T +pub struct aya_log::UpperHexBytesFormatter +impl aya_log::Formatter<&[u8]> for aya_log::UpperHexBytesFormatter +pub fn aya_log::UpperHexBytesFormatter::format(v: &[u8]) -> alloc::string::String +impl core::marker::Freeze for aya_log::UpperHexBytesFormatter +impl core::marker::Send for aya_log::UpperHexBytesFormatter +impl core::marker::Sync for aya_log::UpperHexBytesFormatter +impl core::marker::Unpin for aya_log::UpperHexBytesFormatter +impl core::panic::unwind_safe::RefUnwindSafe for aya_log::UpperHexBytesFormatter +impl core::panic::unwind_safe::UnwindSafe for aya_log::UpperHexBytesFormatter +impl core::convert::Into for aya_log::UpperHexBytesFormatter where U: core::convert::From +pub fn aya_log::UpperHexBytesFormatter::into(self) -> U +impl core::convert::TryFrom for aya_log::UpperHexBytesFormatter where U: core::convert::Into +pub type aya_log::UpperHexBytesFormatter::Error = core::convert::Infallible +pub fn aya_log::UpperHexBytesFormatter::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_log::UpperHexBytesFormatter where U: core::convert::TryFrom +pub type aya_log::UpperHexBytesFormatter::Error = >::Error +pub fn aya_log::UpperHexBytesFormatter::try_into(self) -> core::result::Result>::Error> +impl core::any::Any for aya_log::UpperHexBytesFormatter where T: 'static + core::marker::Sized +pub fn aya_log::UpperHexBytesFormatter::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_log::UpperHexBytesFormatter where T: core::marker::Sized +pub fn aya_log::UpperHexBytesFormatter::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_log::UpperHexBytesFormatter where T: core::marker::Sized +pub fn aya_log::UpperHexBytesFormatter::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_log::UpperHexBytesFormatter +pub fn aya_log::UpperHexBytesFormatter::from(t: T) -> T pub struct aya_log::UpperHexFormatter impl aya_log::Formatter for aya_log::UpperHexFormatter where T: core::fmt::UpperHex pub fn aya_log::UpperHexFormatter::format(v: T) -> alloc::string::String @@ -297,14 +297,14 @@ impl core::convert::From for aya_log::UpperMacFormatter pub fn aya_log::UpperMacFormatter::from(t: T) -> T pub trait aya_log::Formatter pub fn aya_log::Formatter::format(v: T) -> alloc::string::String +impl aya_log::Formatter<&[u8]> for aya_log::LowerHexBytesFormatter +pub fn aya_log::LowerHexBytesFormatter::format(v: &[u8]) -> alloc::string::String +impl aya_log::Formatter<&[u8]> for aya_log::UpperHexBytesFormatter +pub fn aya_log::UpperHexBytesFormatter::format(v: &[u8]) -> alloc::string::String impl aya_log::Formatter<[u8; 6]> for aya_log::LowerMacFormatter pub fn aya_log::LowerMacFormatter::format(v: [u8; 6]) -> alloc::string::String impl aya_log::Formatter<[u8; 6]> for aya_log::UpperMacFormatter pub fn aya_log::UpperMacFormatter::format(v: [u8; 6]) -> alloc::string::String -impl aya_log::Formatter<&[T]> for aya_log::LowerHexDebugFormatter where T: core::fmt::LowerHex -pub fn aya_log::LowerHexDebugFormatter::format(v: &[T]) -> alloc::string::String -impl aya_log::Formatter<&[T]> for aya_log::UpperHexDebugFormatter where T: core::fmt::UpperHex -pub fn aya_log::UpperHexDebugFormatter::format(v: &[T]) -> alloc::string::String impl aya_log::Formatter for aya_log::DefaultFormatter where T: alloc::string::ToString pub fn aya_log::DefaultFormatter::format(v: T) -> alloc::string::String impl aya_log::Formatter for aya_log::Ipv4Formatter where T: core::convert::Into