From 520938abb2ac8d57a68f1ee36a5a29809276667a Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Sat, 7 Mar 2020 20:28:34 -0500 Subject: [PATCH] don't make the logger raise errors for christ sake --- Cargo.lock | 1 - hptp-recv/src/main.rs | 4 ++-- hptp/Cargo.toml | 1 - hptp/src/log.rs | 46 +++++++++++++++++++++---------------------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b89e0d..1a67913 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -88,7 +88,6 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" name = "hptp" version = "0.1.0" dependencies = [ - "failure", "tokio", ] diff --git a/hptp-recv/src/main.rs b/hptp-recv/src/main.rs index 706b6b8..d9df368 100644 --- a/hptp-recv/src/main.rs +++ b/hptp-recv/src/main.rs @@ -18,7 +18,7 @@ async fn start(log: &mut Logger) -> Result<(), Error> { let host = (Ipv4Addr::LOCALHOST, 3700); let sock = UdpSocket::bind(host).await?; - log.bound(sock.local_addr()?.port()).await?; + log.bound(sock.local_addr()?.port()).await; let (mut rx, _tx) = sock.split(); let mut buf = [0u8; 2000]; @@ -28,6 +28,6 @@ async fn start(log: &mut Logger) -> Result<(), Error> { Ok(s) => format!("got: {:?}", s), Err(e) => format!("got: {:?} (invalid utf8)", e.as_bytes()), }) - .await?; + .await; } } diff --git a/hptp/Cargo.toml b/hptp/Cargo.toml index 60a7352..613a9b8 100644 --- a/hptp/Cargo.toml +++ b/hptp/Cargo.toml @@ -9,4 +9,3 @@ edition = "2018" [dependencies] tokio = {version = "0.2.*", features = ["io-std", "io-util"]} -failure = "0.1.7" \ No newline at end of file diff --git a/hptp/src/log.rs b/hptp/src/log.rs index cec02fc..0ee3f11 100644 --- a/hptp/src/log.rs +++ b/hptp/src/log.rs @@ -1,10 +1,9 @@ -use failure::Error; +extern crate tokio; + use std::fmt::Display; use std::time::SystemTime; use tokio::io::{AsyncWriteExt, Stderr}; -type Result = std::result::Result<(), Error>; - pub struct Logger { out: Stderr, } @@ -16,13 +15,17 @@ impl Logger { } } - async fn log_message(&mut self, msg: LogMessage) -> Result { - self.out.write_all(format!("{}\n", msg).as_bytes()).await?; - self.out.flush().await?; - Ok(()) + async fn log_message(&mut self, msg: LogMessage<'_>) { + // These two lines should never fail and we realistically can't do anything about + // it if they do. + self.out + .write_all(format!("{}\n", msg).as_bytes()) + .await + .expect("failed to write log message"); + self.out.flush().await.expect("failed to flush stderr"); } - async fn log_payload(&mut self, payload: LogPayload) -> Result { + async fn log_payload(&mut self, payload: LogPayload<'_>) { self.log_message(LogMessage { timestamp: SystemTime::now(), payload, @@ -32,37 +35,32 @@ impl Logger { // ----------------------------------------------------------------------------------- - pub async fn debug_msg(&mut self, what: String) -> Result { - self.log_payload(LogPayload::Debug { what }).await + pub async fn debug_msg>(&mut self, what: S) { + self.log_payload(LogPayload::Debug { what: what.as_ref() }).await } - pub async fn bound(&mut self, port: u16) -> Result { + pub async fn bound(&mut self, port: u16) { self.log_payload(LogPayload::Bound { port }).await } - pub async fn recv_data_accepted( - &mut self, - start: usize, - len: usize, - order: AcceptedOrder, - ) -> Result { + pub async fn recv_data_accepted(&mut self, start: usize, len: usize, order: AcceptedOrder) { self.log_payload(LogPayload::RecvDataAccepted { start, len, order }) .await } - pub async fn recv_data_ignored(&mut self) -> Result { + pub async fn recv_data_ignored(&mut self) { self.log_payload(LogPayload::RecvDataIgnored).await } } -pub struct LogMessage { +struct LogMessage<'a> { pub timestamp: SystemTime, - pub payload: LogPayload, + pub payload: LogPayload<'a>, } -pub enum LogPayload { +enum LogPayload<'a> { Debug { - what: String, + what: &'a str, }, Bound { port: u16, @@ -83,14 +81,14 @@ pub enum AcceptedOrder { pub use AcceptedOrder::*; -impl Display for LogMessage { +impl Display for LogMessage<'_> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { // TODO: write timestamp self.payload.fmt(f) } } -impl Display for LogPayload { +impl Display for LogPayload<'_> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { LogPayload::Debug { what } => write!(f, "[debug] {}", what),