Skip to content

Commit

Permalink
Fully implement trace formatting for events
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Dec 19, 2024
1 parent dc062db commit f1be19d
Show file tree
Hide file tree
Showing 25 changed files with 4,779 additions and 833 deletions.
4 changes: 4 additions & 0 deletions gen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl ArgType {
pub const fn is_fd(&self) -> bool {
matches!(self, Self::Fd)
}

pub const fn is_array(&self) -> bool {
matches!(self, Self::Array)
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down
25 changes: 20 additions & 5 deletions gen/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn write_dispatchers(interface: &Interface) -> Vec<TokenStream> {
#name #tryinto
});

tracing_fmt.push(format!("{{}}"));
tracing_fmt.push("{}");
tracing_args.push(quote! { #name #fd_print #map_display });
}

Expand Down Expand Up @@ -218,26 +218,41 @@ fn write_events(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<Token
quote! {client: &mut crate::server::Client},
];

let mut tracing_fmt = Vec::new();
let mut tracing_args = Vec::new();

for arg in &event.args {
let mut ty = arg.to_rust_type_token(arg.find_protocol(&pairs).as_ref().unwrap_or(pair));

let mut map_display = quote! {};
let mut fd_print = quote! {};

if arg.allow_null {
ty = quote! {Option<#ty>};
map_display = quote! {.as_ref().map_or("null".to_string(), |v| v.to_string())}
}

if arg.ty.is_fd() {
fd_print = quote! { .as_raw_fd() }
}

let name = make_ident(arg.name.to_snek_case());

args.push(quote! {#name: #ty});

tracing_args.push("rq");
if !arg.ty.is_array() {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name #fd_print #map_display });
} else {
tracing_fmt.push("array[{}]");
tracing_args.push(quote! { #name .len() });
}
}

let tracing_args = tracing_args.join(", ");
let tracing_fmt = tracing_fmt.join(", ");

let tracing_inner = format!(
"-> {interface}#{{}}.{event}({tracing_args})",
"-> {interface}#{{}}.{event}({tracing_fmt})",
interface = interface.name,
event = event.name.to_snek_case()
);
Expand Down Expand Up @@ -289,7 +304,7 @@ fn write_events(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<Token
events.push(quote! {
#(#docs)*
async fn #name(#(#args),*) -> crate::server::Result<()> {
tracing::debug!(#tracing_inner, object.id);
tracing::debug!(#tracing_inner, object.id, #(#tracing_args),*);

let (payload,fds) = crate::wire::PayloadBuilder::new()
#(#build_args)*
Expand Down
12 changes: 12 additions & 0 deletions gen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ pub fn write_enums(interface: &Interface) -> Vec<TokenStream> {
}
}
}

impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(*self as u32).fmt(f)
}
}
})
} else {
let mut variants = Vec::new();
Expand Down Expand Up @@ -135,6 +141,12 @@ pub fn write_enums(interface: &Interface) -> Vec<TokenStream> {
Self::from_bits(v).ok_or(crate::wire::DecodeError::MalformedPayload)
}
}

impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.bits().fmt(f)
}
}
})
}
}
Expand Down
Loading

0 comments on commit f1be19d

Please sign in to comment.