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"
version = "0.1.0"
dependencies = [
"failure",
"tokio",
]

View File

@ -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;
}
}

View File

@ -9,4 +9,3 @@ edition = "2018"
[dependencies]
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::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<S: AsRef<str>>(&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),