Improve packet debug output

This commit is contained in:
Ryan 2022-07-01 16:03:15 -07:00
parent a259bdf840
commit 0fcedd3656
2 changed files with 28 additions and 5 deletions

View file

@ -2,10 +2,10 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::Duration;
use anyhow::bail; use anyhow::bail;
use chrono::{Utc, DateTime}; use chrono::{DateTime, Utc};
use clap::Parser; use clap::Parser;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};

View file

@ -264,8 +264,7 @@ macro_rules! def_bitfield {
),* $(,)? ),* $(,)?
} }
) => { ) => {
// TODO: custom Debug impl. #[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
$(#[$struct_attrs])* $(#[$struct_attrs])*
pub struct $name($inner_ty); pub struct $name($inner_ty);
@ -307,6 +306,18 @@ macro_rules! def_bitfield {
} }
} }
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct(stringify!($name));
paste! {
$(
s.field(stringify!($bit), &self. [<get_ $bit:snake>]());
)*
}
s.finish()
}
}
impl $crate::protocol::Encode for $name { impl $crate::protocol::Encode for $name {
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
self.0.encode(w) self.0.encode(w)
@ -328,7 +339,7 @@ macro_rules! def_packet_group {
$($packet:ident),* $(,)? $($packet:ident),* $(,)?
} }
) => { ) => {
#[derive(Clone, Debug)] #[derive(Clone)]
$(#[$attrs])* $(#[$attrs])*
pub enum $group_name { pub enum $group_name {
$($packet($packet)),* $($packet($packet)),*
@ -378,6 +389,18 @@ macro_rules! def_packet_group {
} }
} }
} }
impl fmt::Debug for $group_name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut t = f.debug_tuple(stringify!($group_name));
match self {
$(
Self::$packet(pkt) => t.field(pkt),
)*
};
t.finish()
}
}
} }
} }