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
#![deny(unused_crate_dependencies)]
#![deny(missing_docs)]
#![deny(clippy::dbg_macro)]
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{parse2, parse_quote, punctuated::Punctuated, Result, Token};
mod types;
use self::types::*;
#[cfg(test)]
mod tests;
#[proc_macro]
pub fn error(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
gum(item, Level::Error)
}
#[proc_macro]
pub fn warn(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
gum(item, Level::Warn)
}
#[proc_macro]
pub fn info(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
gum(item, Level::Info)
}
#[proc_macro]
pub fn debug(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
gum(item, Level::Debug)
}
#[proc_macro]
pub fn trace(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
gum(item, Level::Trace)
}
pub(crate) fn gum(item: proc_macro::TokenStream, level: Level) -> proc_macro::TokenStream {
let item: TokenStream = item.into();
let res = expander::Expander::new("gum")
.add_comment("Generated overseer code by `gum::warn!(..)`".to_owned())
.dry(true)
.verbose(false)
.fmt(expander::Edition::_2021)
.maybe_write_to_out_dir(impl_gum2(item, level))
.expect("Expander does not fail due to IO in OUT_DIR. qed");
res.unwrap_or_else(|err| err.to_compile_error()).into()
}
pub(crate) fn impl_gum2(orig: TokenStream, level: Level) -> Result<TokenStream> {
let args: Args = parse2(orig)?;
let krate = support_crate();
let span = Span::call_site();
let Args { target, comma, mut values, fmt } = args;
let maybe_candidate_hash = values.iter_mut().find(|value| value.as_ident() == "candidate_hash");
if let Some(kv) = maybe_candidate_hash {
let (ident, rhs_expr, replace_with) = match kv {
Value::Alias(alias) => {
let ValueWithAliasIdent { alias, marker, expr, .. } = alias.clone();
(
alias.clone(),
expr.to_token_stream(),
Some(Value::Value(ValueWithFormatMarker {
marker,
ident: alias,
dot: None,
inner: Punctuated::new(),
})),
)
},
Value::Value(value) => (value.ident.clone(), value.ident.to_token_stream(), None),
};
if let Some(replace_with) = replace_with {
let _old = std::mem::replace(kv, replace_with);
};
let had_trailing_comma = values.trailing_punct();
if !had_trailing_comma {
values.push_punct(Token![,](span));
}
values.push_value(parse_quote! {
traceID = % trace_id
});
if had_trailing_comma {
values.push_punct(Token![,](span));
}
Ok(quote! {
if #krate :: enabled!(#target #comma #level) {
use ::std::ops::Deref;
let value = #rhs_expr;
let value = &value;
let value: & #krate:: Hash = value.deref();
let #ident: #krate:: Hash = * value;
let trace_id = #krate:: hash_to_trace_identifier ( #ident );
#krate :: event!(
#target #comma #level, #values #fmt
)
}
})
} else {
Ok(quote! {
#krate :: event!(
#target #comma #level, #values #fmt
)
})
}
}
fn support_crate() -> TokenStream {
let support_crate_name = if cfg!(test) {
quote! {crate}
} else {
use proc_macro_crate::{crate_name, FoundCrate};
let crate_name = crate_name("tracing-gum")
.expect("Support crate `tracing-gum` is present in `Cargo.toml`. qed");
match crate_name {
FoundCrate::Itself => quote! {crate},
FoundCrate::Name(name) => Ident::new(&name, Span::call_site()).to_token_stream(),
}
};
support_crate_name
}