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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![allow(unused_imports)]

use darling::FromMeta;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{AttributeArgs, ItemFn};

#[derive(Debug, FromMeta)]
struct TracingArgs {
    #[darling(default)]
    enabled: Option<bool>,
    #[darling(default)]
    main: Option<String>,
}

#[derive(Debug, FromMeta)]
struct DurationArgs {
    #[darling(default)]
    disabled: bool,
    #[darling(default)]
    printer: Option<String>,
}

#[derive(Debug, FromMeta)]
struct MacroArgs {
    #[darling(default)]
    tracing: Option<TracingArgs>,
    #[darling(default)]
    duration: Option<DurationArgs>,
}

use proc_macro2::TokenStream as Code;

fn codegen_tracing(options: &MacroArgs, function_name: &str) -> (Option<Code>, Option<Code>) {
    if let Some(_) = options.tracing {
        let begin = quote! {
            {
                let ts = std::time::SystemTime::now()
                    .duration_since(std::time::SystemTime::UNIX_EPOCH)
                    .unwrap()
                    .as_micros();
                timed::Trace::record(timed::Hop {
                    function_name: format!("{}::{}", __timed_module_path, #function_name),
                    timestamp: ts,
                    phase: timed::Phase::Start
                });
            }
        };
        let end = quote! {
            {
                let ts = std::time::SystemTime::now()
                    .duration_since(std::time::SystemTime::UNIX_EPOCH)
                    .unwrap()
                    .as_micros();
                timed::Trace::record(timed::Hop {
                    function_name: format!("{}::{}", __timed_module_path, #function_name),
                    timestamp: ts,
                    phase: timed::Phase::Finish(__timed_elapsed)
                });
            }
        };
        (Some(begin), Some(end))
    } else {
        (None, None)
    }
}

fn codegen_printer(options: &Option<String>) -> proc_macro2::TokenStream {
    let (printer, needs_bang) = match &options {
        Some(printer) => {
            if printer.ends_with('!') {
                (&printer[..&printer.len() - 1], true)
            } else {
                (printer.as_str(), false)
            }
        }
        None => ("println", true),
    };

    let bang = if needs_bang {
        Some(syn::token::Bang(Span::call_site()))
    } else {
        None
    };

    let printer = syn::Ident::new(printer, Span::call_site());
    quote! {
        #printer#bang
    }
}

fn codegen_duration(options: &MacroArgs, function_name: &String) -> (Code, Code) {
    // Generate printer
    let printer_options = match &options.duration {
        Some(options) => &options.printer,
        None => &None,
    };
    let printer = codegen_printer(&printer_options);

    // Decide if we generate duration at all
    let disabled = match options.duration {
        Some(DurationArgs { disabled, .. }) => disabled,
        _ => false,
    };

    let duration_begin = quote! {
        let __timed_start = std::time::Instant::now();
    };

    let duration_end = if disabled {
        quote! {
            let __timed_elapsed = __timed_start.elapsed();
        }
    } else {
        quote! {
            let __timed_elapsed = __timed_start.elapsed();
            #printer("function={} duration={:?}", #function_name, __timed_elapsed);
        }
    };

    (duration_begin, duration_end)
}

/// Macro that times your function execution.
#[proc_macro_attribute]
pub fn timed(args: TokenStream, input: TokenStream) -> TokenStream {
    //! ```text
    //! Call this using #[timed]
    //! It will print by default with `println!`
    //! If you want to change the printer you can use #[timed(printer = "info!")]
    //! or any other macro or function that takes in a String.`
    //! ```
    //!
    //! ```ignore
    //! #[timed(duration(printer = "println!"))]
    //! ```
    // debug!("Args {:?}", args);
    let options = syn::parse_macro_input!(args as AttributeArgs);
    let function = syn::parse_macro_input!(input as ItemFn);

    // Parse options
    let options: MacroArgs = match MacroArgs::from_list(&options) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };
    // debug!("Parsed options {:?}", options);

    // Parse function sig
    let ItemFn {
        attrs,
        vis,
        sig,
        block: body,
        ..
    } = &function;
    let name = function.sig.ident.to_string();

    let (duration_begin, duration_end) = codegen_duration(&options, &name);
    let (tracing_begin, tracing_end) = codegen_tracing(&options, name.as_str());

    let result = quote! {
        #(#attrs)*
        #vis #sig {
            let __timed_module_path = std::module_path!();

            #tracing_begin
            #duration_begin
            let result = { #body };

            #duration_end
            #tracing_end

            result
        }
    };

    let res: proc_macro::TokenStream = result.into();
    res
}