Skip to content

Commit

Permalink
Get some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Dorezyuk committed Dec 6, 2023
1 parent d9b47b1 commit 4ff2f19
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zvt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ async-stream = "0.3.5"
serde = { version = "1.0.185", features = ["derive"] }
serde_json = "1.0.105"
futures = "0.3.28"
pin-project = "1.1.3"

5 changes: 3 additions & 2 deletions zvt/src/bin/status/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::info;
use std::io::Write;
use tokio::net::TcpStream;
use tokio_stream::StreamExt;
use zvt::{packets, sequences, sequences::Sequence};
use zvt::{logging, packets, sequences, sequences::Sequence};

#[derive(Parser, Debug)]
struct Args {
Expand Down Expand Up @@ -57,7 +57,8 @@ async fn main() -> std::io::Result<()> {
let args = Args::parse();

info!("Using the args {:?}", args);
let mut socket = TcpStream::connect(args.ip).await?;
let source = TcpStream::connect(args.ip).await?;
let mut socket = logging::LoggingSource { source };

let request = packets::Registration {
password: args.password,
Expand Down
1 change: 1 addition & 0 deletions zvt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod constants;
pub mod feig;
pub mod logging;
pub mod packets;
pub mod sequences;

Expand Down
67 changes: 67 additions & 0 deletions zvt/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use log::debug;
use pin_project::pin_project;
use std::pin::Pin;
use std::task::Poll;
use tokio::io::{AsyncRead, AsyncWrite};

/// Wrapper around an io interface which will log on successful read and write.
///
/// The logging happens every time the async methods return [Poll::Ready],
/// assuming the runtime will not poll them after this point.
#[pin_project]
pub struct LoggingSource<Source> {
#[pin]
pub source: Source,
}

impl<Source> AsyncRead for LoggingSource<Source>
where
Source: AsyncRead + Send,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
let res = self.project().source.poll_read(cx, buf);
if let Poll::Ready(_) = res {
debug!("Read the bytes {:?}", buf)
}

res
}
}

impl<Source> AsyncWrite for LoggingSource<Source>
where
Source: AsyncWrite,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> std::task::Poll<std::prelude::v1::Result<usize, std::io::Error>> {
let res = self.project().source.poll_write(cx, buf);
if let Poll::Ready(_) = res {
debug!("Write the bytes {:?}", buf);
}

res
}

fn poll_flush(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::prelude::v1::Result<(), std::io::Error>> {
// No logging here.
self.project().source.poll_flush(cx)
}

fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::prelude::v1::Result<(), std::io::Error>> {
// No logging here.
self.project().source.poll_shutdown(cx)
}
}
3 changes: 0 additions & 3 deletions zvt/src/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{encoding, ZvtEnum, ZvtParser, ZvtSerializer};
use anyhow::Result;
use async_stream::try_stream;
use futures::Stream;
use log::debug;
use std::boxed::Box;
use std::marker::Unpin;
use std::pin::Pin;
Expand All @@ -26,8 +25,6 @@ pub async fn read_packet_async(src: &mut Pin<&mut impl AsyncReadExt>) -> Result<
buf.resize(start + len, 0);
src.read_exact(&mut buf[start..]).await?;

debug!("Received {buf:?}");

Ok(buf.to_vec())
}

Expand Down

0 comments on commit 4ff2f19

Please sign in to comment.