1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#![feature(proc_macro_span)]
extern crate crossbeam_channel;
extern crate holochain_tracing;
extern crate proc_macro;
extern crate quote;
extern crate syn;

mod autotrace;
#[cfg(feature = "newrelic_on")]
mod newrelic_trace;

use autotrace::Autotrace;
#[cfg(feature = "newrelic_on")]
use newrelic_trace::NewRelicTrace;
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;

#[proc_macro_attribute]
pub fn autotrace(_attr: TokenStream, code: TokenStream) -> TokenStream {
    if cfg!(feature = "tracing-on") {
        let mut at = Autotrace::default();
        let output = syn::fold::fold_item(&mut at, syn::parse(code).unwrap());
        let span = output.span();
        TokenStream::from(quote::quote_spanned! {span=>
            #output
        })
    } else {
        code
    }
}

#[proc_macro_attribute]
pub fn autotrace_deep(_attr: TokenStream, code: TokenStream) -> TokenStream {
    if cfg!(feature = "tracing-on") {
        let mut at = Autotrace::deep();
        let output = syn::fold::fold_item(&mut at, syn::parse(code).unwrap());
        TokenStream::from(quote! {#output})
    } else {
        code
    }
}

#[proc_macro]
pub fn autotrace_deep_block(code: TokenStream) -> TokenStream {
    if cfg!(feature = "tracing-on") {
        let output = Autotrace::rewrite_deep(syn::parse(code).unwrap());
        TokenStream::from(quote! {#output})
    } else {
        code
    }
}

#[cfg(feature = "newrelic_on")]
#[proc_macro_attribute]
pub fn newrelic_autotrace(attr: TokenStream, code: TokenStream) -> TokenStream {
    let mut new_relic = NewRelicTrace::new(attr);
    let output = syn::fold::fold_item(&mut new_relic, syn::parse(code).unwrap());
    let span = output.span();
    TokenStream::from(quote::quote_spanned! {span=>
        #output
    })
}

#[cfg(not(feature = "newrelic_on"))]
#[proc_macro_attribute]
pub fn newrelic_autotrace(_attr: TokenStream, code: TokenStream) -> TokenStream {
    code
}

#[proc_macro_attribute]
pub fn no_autotrace(_attr: TokenStream, code: TokenStream) -> TokenStream {
    code
}

#[proc_macro]
pub fn here(code: TokenStream) -> TokenStream {
    let span = code
        .into_iter()
        .next()
        .expect("Failed to unwrap code, pass in a ()")
        .span();
    let position = format!(
        "{}:{}",
        span.source_file().path().display(),
        span.start().line
    );
    TokenStream::from(quote! {String::from(#position)})
}