Skip to content

Commit

Permalink
Construct fake module path targets for some events
Browse files Browse the repository at this point in the history
Constructs fake module path targets for events generated by macros.
This puts these events in the `rocket::codegen::{module_path!()}`
module, which enables filtering them out by checking the `rocket` (or
`rocket::codegen`) prefix, or more precise filtering by looking at the
original module path.

This also makes a couple of minor style changes to the trace module.
  • Loading branch information
the10thWiz committed Jun 9, 2024
1 parent 4a00c1f commit 3384b2e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
68 changes: 50 additions & 18 deletions core/codegen/src/attribute/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ fn query_decls(route: &Route) -> Option<TokenStream> {
)*

if !__e.is_empty() {
::rocket::trace::span_info!("codegen",
::rocket::trace::span_info!(
"codegen",
"query string failed to match route declaration" =>
{ for _err in __e { ::rocket::trace::info!("{_err}"); } }
{ for _err in __e { ::rocket::trace::info!(
target: concat!("rocket::codegen::", module_path!()),
"{_err}"
); } }
);

return #Outcome::Forward((#__data, #Status::UnprocessableEntity));
Expand All @@ -128,17 +132,27 @@ fn request_guard_decl(guard: &Guard) -> TokenStream {
let #ident: #ty = match <#ty as #FromRequest>::from_request(#__req).await {
#Outcome::Success(__v) => __v,
#Outcome::Forward(__e) => {
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
type_name = stringify!(#ty), status = __e.code,
"request guard forwarding");
::rocket::trace::info!(
name: "forward",
target: concat!("rocket::codegen::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
status = __e.code,
"request guard forwarding"
);

return #Outcome::Forward((#__data, __e));
},
#[allow(unreachable_code)]
#Outcome::Error((__c, __e)) => {
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
type_name = stringify!(#ty), reason = %#display_hack!(__e),
"request guard failed");
::rocket::trace::info!(
name: "failure",
target: concat!("rocket::codegen::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
reason = %#display_hack!(__e),
"request guard failed"
);

return #Outcome::Error(__c);
}
Expand All @@ -155,9 +169,14 @@ fn param_guard_decl(guard: &Guard) -> TokenStream {

// Returned when a dynamic parameter fails to parse.
let parse_error = quote!({
::rocket::trace::info!(name: "forward", parameter = #name,
type_name = stringify!(#ty), reason = %#display_hack!(__error),
"path guard forwarding");
::rocket::trace::info!(
name: "forward",
target: concat!("rocket::codegen::", module_path!()),
parameter = #name,
type_name = stringify!(#ty),
reason = %#display_hack!(__error),
"path guard forwarding"
);

#Outcome::Forward((#__data, #Status::UnprocessableEntity))
});
Expand All @@ -174,9 +193,12 @@ fn param_guard_decl(guard: &Guard) -> TokenStream {
},
#_None => {
::rocket::trace::error!(
target: concat!("rocket::codegen::", module_path!()),
"Internal invariant broken: dyn param {} not found.\n\
Please report this to the Rocket issue tracker.\n\
https://github.com/rwf2/Rocket/issues", #i);
https://github.com/rwf2/Rocket/issues",
#i
);

return #Outcome::Forward((#__data, #Status::InternalServerError));
}
Expand All @@ -203,17 +225,27 @@ fn data_guard_decl(guard: &Guard) -> TokenStream {
let #ident: #ty = match <#ty as #FromData>::from_data(#__req, #__data).await {
#Outcome::Success(__d) => __d,
#Outcome::Forward((__d, __e)) => {
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
type_name = stringify!(#ty), status = __e.code,
"data guard forwarding");
::rocket::trace::info!(
name: "forward",
target: concat!("rocket::codegen::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
status = __e.code,
"data guard forwarding"
);

return #Outcome::Forward((__d, __e));
}
#[allow(unreachable_code)]
#Outcome::Error((__c, __e)) => {
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
type_name = stringify!(#ty), reason = %#display_hack!(__e),
"data guard failed");
::rocket::trace::info!(
name: "failure",
target: concat!("rocket::codegen::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
reason = %#display_hack!(__e),
"data guard failed"
);

return #Outcome::Error(__c);
}
Expand Down
10 changes: 5 additions & 5 deletions core/lib/src/trace/subscriber/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl RocketDynFmt {
return;
}

let workers = config.map(|c| c.workers).unwrap_or(num_cpus::get());
let colors = config.map(|c| c.cli_colors).unwrap_or(CliColors::Auto);
let level = config.map(|c| c.log_level).unwrap_or(Some(Level::INFO));
let format = config.map(|c| c.log_format).unwrap_or(TraceFormat::Pretty);
let workers = config.map_or(num_cpus::get(), |c| c.workers);
let colors = config.map_or(CliColors::Auto, |c| c.cli_colors);
let level = config.map_or(Some(Level::INFO), |c| c.log_level);
let format = config.map_or(TraceFormat::Pretty, |c| c.log_format);

let formatter = |format| match format {
TraceFormat::Pretty => Self::from(RocketFmt::<Pretty>::new(workers, colors, level)),
Expand All @@ -57,7 +57,7 @@ impl RocketDynFmt {

if result.is_ok() {
assert!(HANDLE.set(reload_handle).is_ok());
} if let Some(handle) = HANDLE.get() {
} else if let Some(handle) = HANDLE.get() {
assert!(handle.modify(|layer| *layer = formatter(format)).is_ok());
}
}
Expand Down

0 comments on commit 3384b2e

Please sign in to comment.