@@ -9,7 +9,7 @@ use syn::{
9
9
} ;
10
10
11
11
use crate :: {
12
- attr:: { ErrorMode , Field , Fields , InstrumentArgs } ,
12
+ attr:: { Field , Fields , FormatMode , InstrumentArgs } ,
13
13
MaybeItemFnRef ,
14
14
} ;
15
15
@@ -191,8 +191,18 @@ fn gen_block<B: ToTokens>(
191
191
} ) ( ) ;
192
192
193
193
let err_event = match args. err_mode {
194
- Some ( ErrorMode :: Display ) => Some ( quote ! ( tracing:: error!( error = %e) ) ) ,
195
- Some ( ErrorMode :: Debug ) => Some ( quote ! ( tracing:: error!( error = ?e) ) ) ,
194
+ Some ( FormatMode :: Default ) | Some ( FormatMode :: Display ) => {
195
+ Some ( quote ! ( tracing:: error!( error = %e) ) )
196
+ }
197
+ Some ( FormatMode :: Debug ) => Some ( quote ! ( tracing:: error!( error = ?e) ) ) ,
198
+ _ => None ,
199
+ } ;
200
+
201
+ let ret_event = match args. ret_mode {
202
+ Some ( FormatMode :: Display ) => Some ( quote ! ( tracing:: event!( #level, return = %x) ) ) ,
203
+ Some ( FormatMode :: Default ) | Some ( FormatMode :: Debug ) => {
204
+ Some ( quote ! ( tracing:: event!( #level, return = ?x) ) )
205
+ }
196
206
_ => None ,
197
207
} ;
198
208
@@ -201,9 +211,26 @@ fn gen_block<B: ToTokens>(
201
211
// which is `instrument`ed using `tracing-futures`. Otherwise, this will
202
212
// enter the span and then perform the rest of the body.
203
213
// If `err` is in args, instrument any resulting `Err`s.
214
+ // If `ret` is in args, instrument any resulting `Ok`s when the function
215
+ // returns `Result`s, otherwise instrument any resulting values.
204
216
if async_context {
205
- let mk_fut = match err_event {
206
- Some ( err_event) => quote_spanned ! ( block. span( ) =>
217
+ let mk_fut = match ( err_event, ret_event) {
218
+ ( Some ( err_event) , Some ( ret_event) ) => quote_spanned ! ( block. span( ) =>
219
+ async move {
220
+ match async move { #block } . await {
221
+ #[ allow( clippy:: unit_arg) ]
222
+ Ok ( x) => {
223
+ #ret_event;
224
+ Ok ( x)
225
+ } ,
226
+ Err ( e) => {
227
+ #err_event;
228
+ Err ( e)
229
+ }
230
+ }
231
+ }
232
+ ) ,
233
+ ( Some ( err_event) , None ) => quote_spanned ! ( block. span( ) =>
207
234
async move {
208
235
match async move { #block } . await {
209
236
#[ allow( clippy:: unit_arg) ]
@@ -215,7 +242,14 @@ fn gen_block<B: ToTokens>(
215
242
}
216
243
}
217
244
) ,
218
- None => quote_spanned ! ( block. span( ) =>
245
+ ( None , Some ( ret_event) ) => quote_spanned ! ( block. span( ) =>
246
+ async move {
247
+ let x = async move { #block } . await ;
248
+ #ret_event;
249
+ x
250
+ }
251
+ ) ,
252
+ ( None , None ) => quote_spanned ! ( block. span( ) =>
219
253
async move { #block }
220
254
) ,
221
255
} ;
@@ -254,8 +288,23 @@ fn gen_block<B: ToTokens>(
254
288
}
255
289
) ;
256
290
257
- if let Some ( err_event) = err_event {
258
- return quote_spanned ! ( block. span( ) =>
291
+ match ( err_event, ret_event) {
292
+ ( Some ( err_event) , Some ( ret_event) ) => quote_spanned ! { block. span( ) =>
293
+ #span
294
+ #[ allow( clippy:: redundant_closure_call) ]
295
+ match ( move || #block) ( ) {
296
+ #[ allow( clippy:: unit_arg) ]
297
+ Ok ( x) => {
298
+ #ret_event;
299
+ Ok ( x)
300
+ } ,
301
+ Err ( e) => {
302
+ #err_event;
303
+ Err ( e)
304
+ }
305
+ }
306
+ } ,
307
+ ( Some ( err_event) , None ) => quote_spanned ! ( block. span( ) =>
259
308
#span
260
309
#[ allow( clippy:: redundant_closure_call) ]
261
310
match ( move || #block) ( ) {
@@ -266,22 +315,28 @@ fn gen_block<B: ToTokens>(
266
315
Err ( e)
267
316
}
268
317
}
269
- ) ;
270
- }
271
-
272
- quote_spanned ! ( block. span( ) =>
273
- // Because `quote` produces a stream of tokens _without_ whitespace, the
274
- // `if` and the block will appear directly next to each other. This
275
- // generates a clippy lint about suspicious `if/else` formatting.
276
- // Therefore, suppress the lint inside the generated code...
277
- #[ allow( clippy:: suspicious_else_formatting) ]
278
- {
318
+ ) ,
319
+ ( None , Some ( ret_event) ) => quote_spanned ! ( block. span( ) =>
279
320
#span
280
- // ...but turn the lint back on inside the function body.
281
- #[ warn( clippy:: suspicious_else_formatting) ]
282
- #block
283
- }
284
- )
321
+ #[ allow( clippy:: redundant_closure_call) ]
322
+ let x = ( move || #block) ( ) ;
323
+ #ret_event;
324
+ x
325
+ ) ,
326
+ ( None , None ) => quote_spanned ! ( block. span( ) =>
327
+ // Because `quote` produces a stream of tokens _without_ whitespace, the
328
+ // `if` and the block will appear directly next to each other. This
329
+ // generates a clippy lint about suspicious `if/else` formatting.
330
+ // Therefore, suppress the lint inside the generated code...
331
+ #[ allow( clippy:: suspicious_else_formatting) ]
332
+ {
333
+ #span
334
+ // ...but turn the lint back on inside the function body.
335
+ #[ warn( clippy:: suspicious_else_formatting) ]
336
+ #block
337
+ }
338
+ ) ,
339
+ }
285
340
}
286
341
287
342
/// Indicates whether a field should be recorded as `Value` or `Debug`.
0 commit comments