Skip to content

Commit b1b0de2

Browse files
committed
Apply the static level optimization to async fns and error printing
1 parent 27bd20e commit b1b0de2

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

tracing-attributes/src/lib.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,9 @@ fn gen_block(
516516
if err {
517517
quote_spanned!(block.span()=>
518518
let __tracing_attr_span = #span;
519-
tracing::Instrument::instrument(async move {
519+
// See comment on the default case at the end of this function
520+
// for why we do this a bit roundabout.
521+
let fut = async move {
520522
match async move { #block }.await {
521523
#[allow(clippy::unit_arg)]
522524
Ok(x) => Ok(x),
@@ -525,22 +527,46 @@ fn gen_block(
525527
Err(e)
526528
}
527529
}
528-
}, __tracing_attr_span).await
530+
};
531+
if tracing::level_enabled!(#level) {
532+
tracing::Instrument::instrument(
533+
fut,
534+
__tracing_attr_span
535+
)
536+
.await
537+
} else {
538+
fut.await
539+
}
529540
)
530541
} else {
531542
quote_spanned!(block.span()=>
532543
let __tracing_attr_span = #span;
544+
// See comment on the default case at the end of this function
545+
// for why we do this a bit roundabout.
546+
let fut = async move { #block };
547+
if tracing::level_enabled!(#level) {
533548
tracing::Instrument::instrument(
534-
async move { #block },
549+
fut,
535550
__tracing_attr_span
536551
)
537552
.await
553+
} else {
554+
fut.await
555+
}
538556
)
539557
}
540558
} else if err {
541559
quote_spanned!(block.span()=>
542-
let __tracing_attr_span = #span;
543-
let __tracing_attr_guard = __tracing_attr_span.enter();
560+
// See comment on the default case at the end of this function
561+
// for why we do this a bit roundabout.
562+
let __tracing_attr_span;
563+
let __tracing_attr_guard;
564+
if tracing::level_enabled!(#level) {
565+
__tracing_attr_span = #span;
566+
__tracing_attr_guard = __tracing_attr_span.enter();
567+
}
568+
// pacify clippy::suspicious_else_formatting
569+
let _ = ();
544570
#[allow(clippy::redundant_closure_call)]
545571
match (move || #block)() {
546572
#[allow(clippy::unit_arg)]

0 commit comments

Comments
 (0)