melib/connections: don't print raw bytes as escaped unicode

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/370/head
Manos Pitsidianakis 2 months ago
parent 8014af2563
commit f31b5c4000
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -226,6 +226,29 @@ impl GlobMatch for str {
} }
} }
pub mod hex {
use std::fmt::Write;
use crate::error::Result;
pub fn bytes_to_hex(bytes: &[u8]) -> Result<String> {
let mut retval = String::with_capacity(bytes.len() / 2 + bytes.len() / 4);
for (i, c) in bytes.chunks(2).enumerate() {
if i % 16 == 0 {
writeln!(&mut retval)?;
} else if i % 4 == 0 {
write!(&mut retval, " ")?;
}
if c.len() == 2 {
write!(&mut retval, "{:02x}{:02x}", c[0], c[1])?;
} else {
write!(&mut retval, "{:02x}", c[0])?;
}
}
Ok(retval)
}
}
pub const _ALICE_CHAPTER_1: &str = r#"CHAPTER I. Down the Rabbit-Hole pub const _ALICE_CHAPTER_1: &str = r#"CHAPTER I. Down the Rabbit-Hole
Alice was beginning to get very tired of sitting by her sister on the Alice was beginning to get very tired of sitting by her sister on the

@ -20,7 +20,7 @@
*/ */
//! Connections layers (TCP/fd/TLS/Deflate) to use with remote backends. //! Connections layers (TCP/fd/TLS/Deflate) to use with remote backends.
use std::{os::unix::io::AsRawFd, time::Duration}; use std::{borrow::Cow, os::unix::io::AsRawFd, time::Duration};
use flate2::{read::DeflateDecoder, write::DeflateEncoder, Compression}; use flate2::{read::DeflateDecoder, write::DeflateEncoder, Compression};
#[cfg(any(target_os = "openbsd", target_os = "netbsd", target_os = "haiku"))] #[cfg(any(target_os = "openbsd", target_os = "netbsd", target_os = "haiku"))]
@ -399,25 +399,33 @@ impl std::io::Read for Connection {
}; };
if self.is_trace_enabled() { if self.is_trace_enabled() {
let id = self.id(); let id = self.id();
if let Ok(len) = &res { match &res {
log::trace!( Ok(len) => {
"{}{}{}{:?} read {:?} bytes:{:?}", let slice = &buf[..*len];
if id.is_some() { "[" } else { "" }, log::trace!(
if let Some(id) = id.as_ref() { id } else { "" }, "{}{}{}{:?} read {:?} bytes:{}",
if id.is_some() { "]: " } else { "" }, if id.is_some() { "[" } else { "" },
self, if let Some(id) = id.as_ref() { id } else { "" },
len, if id.is_some() { "]: " } else { "" },
String::from_utf8_lossy(&buf[..*len]) self,
); len,
} else { std::str::from_utf8(slice)
log::trace!( .map(Cow::Borrowed)
"{}{}{}{:?} could not read {:?}", .or_else(|_| crate::text::hex::bytes_to_hex(slice).map(Cow::Owned))
if id.is_some() { "[" } else { "" }, .unwrap_or(Cow::Borrowed("Could not convert to hex."))
if let Some(id) = id.as_ref() { id } else { "" }, );
if id.is_some() { "]: " } else { "" }, }
self, Err(err) if matches!(err.kind(), std::io::ErrorKind::WouldBlock) => {}
&res Err(err) => {
); log::trace!(
"{}{}{}{:?} could not read {:?}",
if id.is_some() { "[" } else { "" },
if let Some(id) = id.as_ref() { id } else { "" },
if id.is_some() { "]: " } else { "" },
self,
err,
);
}
} }
} }
res res
@ -429,13 +437,16 @@ impl std::io::Write for Connection {
if self.is_trace_enabled() { if self.is_trace_enabled() {
let id = self.id(); let id = self.id();
log::trace!( log::trace!(
"{}{}{}{:?} writing {} bytes:{:?}", "{}{}{}{:?} writing {} bytes:{}",
if id.is_some() { "[" } else { "" }, if id.is_some() { "[" } else { "" },
if let Some(id) = id.as_ref() { id } else { "" }, if let Some(id) = id.as_ref() { id } else { "" },
if id.is_some() { "]: " } else { "" }, if id.is_some() { "]: " } else { "" },
self, self,
buf.len(), buf.len(),
String::from_utf8_lossy(buf) std::str::from_utf8(buf)
.map(Cow::Borrowed)
.or_else(|_| crate::text::hex::bytes_to_hex(buf).map(Cow::Owned))
.unwrap_or(Cow::Borrowed("Could not convert to hex."))
); );
} }
match self { match self {

Loading…
Cancel
Save