don't make the logger raise errors for christ sake

This commit is contained in:
Milo Turner 2020-03-07 20:28:34 -05:00
parent da55e83e3b
commit 520938abb2
4 changed files with 24 additions and 28 deletions

1
Cargo.lock generated
View File

@ -88,7 +88,6 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
name = "hptp" name = "hptp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"failure",
"tokio", "tokio",
] ]

View File

@ -18,7 +18,7 @@ async fn start(log: &mut Logger) -> Result<(), Error> {
let host = (Ipv4Addr::LOCALHOST, 3700); let host = (Ipv4Addr::LOCALHOST, 3700);
let sock = UdpSocket::bind(host).await?; 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 rx, _tx) = sock.split();
let mut buf = [0u8; 2000]; let mut buf = [0u8; 2000];
@ -28,6 +28,6 @@ async fn start(log: &mut Logger) -> Result<(), Error> {
Ok(s) => format!("got: {:?}", s), Ok(s) => format!("got: {:?}", s),
Err(e) => format!("got: {:?} (invalid utf8)", e.as_bytes()), Err(e) => format!("got: {:?} (invalid utf8)", e.as_bytes()),
}) })
.await?; .await;
} }
} }

View File

@ -9,4 +9,3 @@ edition = "2018"
[dependencies] [dependencies]
tokio = {version = "0.2.*", features = ["io-std", "io-util"]} tokio = {version = "0.2.*", features = ["io-std", "io-util"]}
failure = "0.1.7"

View File

@ -1,10 +1,9 @@
use failure::Error; extern crate tokio;
use std::fmt::Display; use std::fmt::Display;
use std::time::SystemTime; use std::time::SystemTime;
use tokio::io::{AsyncWriteExt, Stderr}; use tokio::io::{AsyncWriteExt, Stderr};
type Result = std::result::Result<(), Error>;
pub struct Logger { pub struct Logger {
out: Stderr, out: Stderr,
} }
@ -16,13 +15,17 @@ impl Logger {
} }
} }
async fn log_message(&mut self, msg: LogMessage) -> Result { async fn log_message(&mut self, msg: LogMessage<'_>) {
self.out.write_all(format!("{}\n", msg).as_bytes()).await?; // These two lines should never fail and we realistically can't do anything about
self.out.flush().await?; // it if they do.
Ok(()) 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 { self.log_message(LogMessage {
timestamp: SystemTime::now(), timestamp: SystemTime::now(),
payload, payload,
@ -32,37 +35,32 @@ impl Logger {
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
pub async fn debug_msg(&mut self, what: String) -> Result { pub async fn debug_msg<S: AsRef<str>>(&mut self, what: S) {
self.log_payload(LogPayload::Debug { what }).await 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 self.log_payload(LogPayload::Bound { port }).await
} }
pub async fn recv_data_accepted( pub async fn recv_data_accepted(&mut self, start: usize, len: usize, order: AcceptedOrder) {
&mut self,
start: usize,
len: usize,
order: AcceptedOrder,
) -> Result {
self.log_payload(LogPayload::RecvDataAccepted { start, len, order }) self.log_payload(LogPayload::RecvDataAccepted { start, len, order })
.await .await
} }
pub async fn recv_data_ignored(&mut self) -> Result { pub async fn recv_data_ignored(&mut self) {
self.log_payload(LogPayload::RecvDataIgnored).await self.log_payload(LogPayload::RecvDataIgnored).await
} }
} }
pub struct LogMessage { struct LogMessage<'a> {
pub timestamp: SystemTime, pub timestamp: SystemTime,
pub payload: LogPayload, pub payload: LogPayload<'a>,
} }
pub enum LogPayload { enum LogPayload<'a> {
Debug { Debug {
what: String, what: &'a str,
}, },
Bound { Bound {
port: u16, port: u16,
@ -83,14 +81,14 @@ pub enum AcceptedOrder {
pub use AcceptedOrder::*; pub use AcceptedOrder::*;
impl Display for LogMessage { impl Display for LogMessage<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// TODO: write timestamp // TODO: write timestamp
self.payload.fmt(f) self.payload.fmt(f)
} }
} }
impl Display for LogPayload { impl Display for LogPayload<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
LogPayload::Debug { what } => write!(f, "[debug] {}", what), LogPayload::Debug { what } => write!(f, "[debug] {}", what),