feat(log): add support for logging raw pointer types

Requires the usage of either `:p`, `:x`, or `:X` display hints as one
rarely logs the raw decimal value of a pointer.
reviewable/pr1368/r1
Christian A. Jacobsen 4 weeks ago
parent 29dc775535
commit bfca3fbe5e
No known key found for this signature in database

@ -71,6 +71,9 @@ impl_formatter_for_types!(
impl<const N: usize> LowerHexFormatter for &[u8; N] {}
impl<T> LowerHexFormatter for *const T {}
impl<T> LowerHexFormatter for *mut T {}
pub trait UpperHexFormatter {}
impl_formatter_for_types!(
UpperHexFormatter: {
@ -82,6 +85,9 @@ impl_formatter_for_types!(
impl<const N: usize> UpperHexFormatter for &[u8; N] {}
impl<T> UpperHexFormatter for *const T {}
impl<T> UpperHexFormatter for *mut T {}
pub trait IpFormatter {}
impl IpFormatter for IpAddr {}
impl IpFormatter for Ipv4Addr {}
@ -144,6 +150,8 @@ pub enum ArgumentKind {
Bytes,
Str,
Pointer,
}
/// All display hints
@ -282,6 +290,20 @@ impl Argument for DisplayHint {
}
}
impl<T> sealed::Sealed for *const T {}
impl<T> Argument for *const T {
fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) {
(ArgumentKind::Pointer, (*self as usize).to_ne_bytes())
}
}
impl<T> sealed::Sealed for *mut T {}
impl<T> Argument for *mut T {
fn as_argument(&self) -> (ArgumentKind, impl AsRef<[u8]>) {
(ArgumentKind::Pointer, (*self as usize).to_ne_bytes())
}
}
fn wire_len(value: &[u8]) -> Option<[u8; 2]> {
match LogValueLength::try_from(value.len()) {
Ok(wire_len) => Some(wire_len.to_ne_bytes()),

@ -732,6 +732,16 @@ fn log_buf<T: ?Sized + Log>(mut buf: &[u8], logger: &T) -> Result<(), ()> {
}
Err(e) => error!("received invalid utf8 string: {e}"),
},
ArgumentKind::Pointer => {
full_log_msg.push_str(
&usize::from_ne_bytes(
value
.try_into()
.map_err(|std::array::TryFromSliceError { .. }| ())?,
)
.format(last_hint.take())?,
);
}
}
buf = rest;

Loading…
Cancel
Save