Skip to content

Commit 3384b2e

Browse files
committed
Construct fake module path targets for some events
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.
1 parent 4a00c1f commit 3384b2e

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

core/codegen/src/attribute/route/mod.rs

+50-18
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ fn query_decls(route: &Route) -> Option<TokenStream> {
105105
)*
106106

107107
if !__e.is_empty() {
108-
::rocket::trace::span_info!("codegen",
108+
::rocket::trace::span_info!(
109+
"codegen",
109110
"query string failed to match route declaration" =>
110-
{ for _err in __e { ::rocket::trace::info!("{_err}"); } }
111+
{ for _err in __e { ::rocket::trace::info!(
112+
target: concat!("rocket::codegen::", module_path!()),
113+
"{_err}"
114+
); } }
111115
);
112116

113117
return #Outcome::Forward((#__data, #Status::UnprocessableEntity));
@@ -128,17 +132,27 @@ fn request_guard_decl(guard: &Guard) -> TokenStream {
128132
let #ident: #ty = match <#ty as #FromRequest>::from_request(#__req).await {
129133
#Outcome::Success(__v) => __v,
130134
#Outcome::Forward(__e) => {
131-
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
132-
type_name = stringify!(#ty), status = __e.code,
133-
"request guard forwarding");
135+
::rocket::trace::info!(
136+
name: "forward",
137+
target: concat!("rocket::codegen::", module_path!()),
138+
parameter = stringify!(#ident),
139+
type_name = stringify!(#ty),
140+
status = __e.code,
141+
"request guard forwarding"
142+
);
134143

135144
return #Outcome::Forward((#__data, __e));
136145
},
137146
#[allow(unreachable_code)]
138147
#Outcome::Error((__c, __e)) => {
139-
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
140-
type_name = stringify!(#ty), reason = %#display_hack!(__e),
141-
"request guard failed");
148+
::rocket::trace::info!(
149+
name: "failure",
150+
target: concat!("rocket::codegen::", module_path!()),
151+
parameter = stringify!(#ident),
152+
type_name = stringify!(#ty),
153+
reason = %#display_hack!(__e),
154+
"request guard failed"
155+
);
142156

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

156170
// Returned when a dynamic parameter fails to parse.
157171
let parse_error = quote!({
158-
::rocket::trace::info!(name: "forward", parameter = #name,
159-
type_name = stringify!(#ty), reason = %#display_hack!(__error),
160-
"path guard forwarding");
172+
::rocket::trace::info!(
173+
name: "forward",
174+
target: concat!("rocket::codegen::", module_path!()),
175+
parameter = #name,
176+
type_name = stringify!(#ty),
177+
reason = %#display_hack!(__error),
178+
"path guard forwarding"
179+
);
161180

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

181203
return #Outcome::Forward((#__data, #Status::InternalServerError));
182204
}
@@ -203,17 +225,27 @@ fn data_guard_decl(guard: &Guard) -> TokenStream {
203225
let #ident: #ty = match <#ty as #FromData>::from_data(#__req, #__data).await {
204226
#Outcome::Success(__d) => __d,
205227
#Outcome::Forward((__d, __e)) => {
206-
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
207-
type_name = stringify!(#ty), status = __e.code,
208-
"data guard forwarding");
228+
::rocket::trace::info!(
229+
name: "forward",
230+
target: concat!("rocket::codegen::", module_path!()),
231+
parameter = stringify!(#ident),
232+
type_name = stringify!(#ty),
233+
status = __e.code,
234+
"data guard forwarding"
235+
);
209236

210237
return #Outcome::Forward((__d, __e));
211238
}
212239
#[allow(unreachable_code)]
213240
#Outcome::Error((__c, __e)) => {
214-
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
215-
type_name = stringify!(#ty), reason = %#display_hack!(__e),
216-
"data guard failed");
241+
::rocket::trace::info!(
242+
name: "failure",
243+
target: concat!("rocket::codegen::", module_path!()),
244+
parameter = stringify!(#ident),
245+
type_name = stringify!(#ty),
246+
reason = %#display_hack!(__e),
247+
"data guard failed"
248+
);
217249

218250
return #Outcome::Error(__c);
219251
}

core/lib/src/trace/subscriber/dynamic.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ impl RocketDynFmt {
3939
return;
4040
}
4141

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

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

5858
if result.is_ok() {
5959
assert!(HANDLE.set(reload_handle).is_ok());
60-
} if let Some(handle) = HANDLE.get() {
60+
} else if let Some(handle) = HANDLE.get() {
6161
assert!(handle.modify(|layer| *layer = formatter(format)).is_ok());
6262
}
6363
}

0 commit comments

Comments
 (0)