forked from romanz/electrs
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3422bd5
commit 4e39879
Showing
4 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "instrumented_macro" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" | ||
proc-macro2 = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn instrumented(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let additional_fields = if !attr.is_empty() { | ||
let attr_tokens: proc_macro2::TokenStream = attr.into(); | ||
quote! {, #attr_tokens } | ||
} else { | ||
quote! {} | ||
}; | ||
|
||
let function = parse_macro_input!(item as ItemFn); | ||
|
||
let fields_tokens = quote! { | ||
fields(module = module_path!(), file = file!(), line = line!() #additional_fields) | ||
}; | ||
|
||
let expanded = quote! { | ||
#[tracing::instrument(skip_all, #fields_tokens)] | ||
#function | ||
}; | ||
|
||
expanded.into() | ||
} |