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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use crate::config::{OutputPosition, OutputType};
use quote::quote;
use syn::{Ident, ReturnType};
/// Template for generating log statements with proper formatting.
///
/// This struct handles the creation of log message templates and generates
/// the appropriate logging code based on the configuration.
///
/// # Examples
///
/// ```
/// use funlog::log_template::LogTemplate;
/// use syn::{parse_quote, ReturnType, Ident};
/// use quote::format_ident;
///
/// let params = vec![format_ident!("x"), format_ident!("y")];
/// let return_type: ReturnType = parse_quote! { -> i32 };
/// let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
/// ```
pub struct LogTemplate {
/// The name of the function being logged
pub function_name: String,
/// Template string for parameter formatting
pub parameters_placeholder: String,
/// Template string for return value formatting
pub return_placeholder: String,
/// Whether the function has parameters to log
pub has_parameters: bool,
/// Whether the function has a return value to log
pub has_return_value: bool,
}
impl LogTemplate {
/// Creates a new LogTemplate with the specified configuration.
///
/// # Arguments
///
/// * `function_name` - The name of the function to log
/// * `params_for_output` - The parameters to include in logging
/// * `return_type` - The return type of the function
/// * `output_ret_value` - Whether to include return value in logs
///
/// # Returns
///
/// Returns a new LogTemplate instance
///
/// # Examples
///
/// ```
/// use funlog::log_template::LogTemplate;
/// use syn::{parse_quote, ReturnType};
/// use quote::format_ident;
///
/// let params = vec![format_ident!("x")];
/// let return_type: ReturnType = parse_quote! { -> i32 };
/// let template = LogTemplate::new("test", ¶ms, &return_type, true);
/// assert_eq!(template.function_name, "test");
/// assert!(template.has_parameters);
/// assert!(template.has_return_value);
/// ```
pub fn new(
function_name: &str,
params_for_output: &[Ident],
return_type: &ReturnType,
output_ret_value: bool,
) -> Self {
let parameters_placeholder = params_for_output
.iter()
.map(|p| format!("{p}:{{}}"))
.collect::<Vec<String>>()
.join(", ");
let return_placeholder = match return_type {
ReturnType::Default => String::new(),
_ => "return:{}".to_string(),
};
Self {
function_name: function_name.to_string(),
parameters_placeholder,
return_placeholder,
has_parameters: !params_for_output.is_empty(),
has_return_value: !matches!(return_type, ReturnType::Default) && output_ret_value,
}
}
/// Formats the template string for function start logging.
///
/// # Returns
///
/// Returns a formatted string template for the start of function execution
///
/// # Examples
///
/// ```
/// use funlog::log_template::LogTemplate;
/// use syn::{parse_quote, ReturnType};
/// use quote::format_ident;
///
/// let params = vec![format_ident!("x")];
/// let return_type: ReturnType = parse_quote! { -> i32 };
/// let template = LogTemplate::new("test", ¶ms, &return_type, false);
/// let start_template = template.format_start_template();
/// assert!(start_template.contains("test [in ]"));
/// ```
pub fn format_start_template(&self) -> String {
if self.has_parameters {
format!(
"{} [in ]: {}",
self.function_name, self.parameters_placeholder
)
} else {
format!("{} [in ]", self.function_name)
}
}
/// Formats the template string for function end logging.
///
/// # Arguments
///
/// * `include_params` - Whether to include parameters in the end template
///
/// # Returns
///
/// Returns a formatted string template for the end of function execution
///
/// # Examples
///
/// ```
/// use funlog::log_template::LogTemplate;
/// use syn::{parse_quote, ReturnType};
/// use quote::format_ident;
///
/// let params = vec![format_ident!("x")];
/// let return_type: ReturnType = parse_quote! { -> i32 };
/// let template = LogTemplate::new("test", ¶ms, &return_type, true);
/// let end_template = template.format_end_template(true);
/// assert!(end_template.contains("test [out]"));
/// ```
pub fn format_end_template(&self, include_params: bool) -> String {
match (include_params && self.has_parameters, self.has_return_value) {
(true, true) => format!(
"{} [out]: {}, {}",
self.function_name, self.parameters_placeholder, self.return_placeholder
),
(true, false) => format!(
"{} [out]: {}",
self.function_name, self.parameters_placeholder
),
(false, true) => format!("{} [out]: {}", self.function_name, self.return_placeholder),
(false, false) => format!("{} [out]", self.function_name),
}
}
/// Generates the actual log statements as TokenStreams.
///
/// # Arguments
///
/// * `output_position` - When to output logs (start, end, or both)
/// * `output_type` - What type of logging to use (print, debug, etc.)
/// * `original_params` - The original parameter identifiers
/// * `saved_param_values` - The saved parameter value identifiers
///
/// # Returns
///
/// Returns a tuple of (start_statement, end_statement) TokenStreams
///
/// # Examples
///
/// ```
/// use funlog::log_template::LogTemplate;
/// use funlog::config::{OutputPosition, OutputType};
/// use syn::{parse_quote, ReturnType};
/// use quote::format_ident;
///
/// let params = vec![format_ident!("x")];
/// let return_type: ReturnType = parse_quote! { -> i32 };
/// let template = LogTemplate::new("test", ¶ms, &return_type, false);
/// let (start, end) = template.generate_log_statements_with_context(
/// &OutputPosition::OnStart,
/// &OutputType::Print,
/// ¶ms,
/// &[]
/// );
/// ```
pub fn generate_log_statements_with_context(
&self,
output_position: &OutputPosition,
output_type: &OutputType,
original_params: &[Ident],
saved_param_values: &[Ident],
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
let log_method = self.get_log_method(output_type);
let start_statement = match output_position {
OutputPosition::OnStart | OutputPosition::OnStartAndEnd => {
let template = self.format_start_template();
if self.has_parameters {
// For start logging, use original parameters with format!
let format_args = original_params.iter().map(|p| {
quote! { format!("{:?}", #p) }
});
quote! {
#log_method(#template, #(#format_args,)*);
}
} else {
quote! {
#log_method(#template);
}
}
}
_ => quote! {},
};
let end_statement = match output_position {
OutputPosition::OnEnd => {
// For OnEnd, include parameters in the end log
let template = self.format_end_template(true);
match (self.has_parameters, self.has_return_value) {
(true, true) => quote! {
#log_method(#template, #(#saved_param_values,)* format!("{:?}", output));
},
(true, false) => quote! {
#log_method(#template, #(#saved_param_values,)*);
},
(false, true) => quote! {
#log_method(#template, format!("{:?}", output));
},
(false, false) => quote! {
#log_method(#template);
},
}
}
OutputPosition::OnStartAndEnd => {
// For OnStartAndEnd, only include return value in the end log (parameters already logged at start)
let template = self.format_end_template(false);
if self.has_return_value {
quote! {
#log_method(#template, format!("{:?}", output));
}
} else {
quote! {
#log_method(#template);
}
}
}
_ => quote! {},
};
(start_statement, end_statement)
}
/// Gets the appropriate logging method TokenStream for the output type.
///
/// # Arguments
///
/// * `output_type` - The type of output to generate method for
///
/// # Returns
///
/// Returns a TokenStream representing the logging method call
fn get_log_method(&self, output_type: &OutputType) -> proc_macro2::TokenStream {
match output_type {
OutputType::Debug => quote! { log::debug! },
OutputType::Info => quote! { log::info! },
OutputType::Warn => quote! { log::warn! },
OutputType::Error => quote! { log::error! },
OutputType::Trace => quote! { log::trace! },
OutputType::Print => quote! { println! },
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use quote::format_ident;
use syn::{parse_quote, ReturnType};
#[test]
fn test_new_with_parameters_and_return() {
let params = vec![format_ident!("x"), format_ident!("y")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
assert_eq!(template.function_name, "test_func");
assert_eq!(template.parameters_placeholder, "x:{}, y:{}");
assert_eq!(template.return_placeholder, "return:{}");
assert!(template.has_parameters);
assert!(template.has_return_value);
}
#[test]
fn test_new_no_parameters_no_return() {
let params = vec![];
let return_type: ReturnType = parse_quote! {};
let template = LogTemplate::new("test_func", ¶ms, &return_type, false);
assert_eq!(template.function_name, "test_func");
assert_eq!(template.parameters_placeholder, "");
assert_eq!(template.return_placeholder, "");
assert!(!template.has_parameters);
assert!(!template.has_return_value);
}
#[test]
fn test_new_with_parameters_no_return() {
let params = vec![format_ident!("x")];
let return_type: ReturnType = parse_quote! {};
let template = LogTemplate::new("test_func", ¶ms, &return_type, false);
assert_eq!(template.parameters_placeholder, "x:{}");
assert!(template.has_parameters);
assert!(!template.has_return_value);
}
#[test]
fn test_new_no_parameters_with_return() {
let params = vec![];
let return_type: ReturnType = parse_quote! { -> String };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
assert!(!template.has_parameters);
assert!(template.has_return_value);
assert_eq!(template.return_placeholder, "return:{}");
}
#[test]
fn test_format_start_template_with_params() {
let params = vec![format_ident!("x"), format_ident!("y")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let start_template = template.format_start_template();
assert_eq!(start_template, "test_func [in ]: x:{}, y:{}");
}
#[test]
fn test_format_start_template_no_params() {
let params = vec![];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let start_template = template.format_start_template();
assert_eq!(start_template, "test_func [in ]");
}
#[test]
fn test_format_end_template_with_params_and_return() {
let params = vec![format_ident!("x")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let end_template = template.format_end_template(true);
assert_eq!(end_template, "test_func [out]: x:{}, return:{}");
}
#[test]
fn test_format_end_template_with_params_no_return() {
let params = vec![format_ident!("x")];
let return_type: ReturnType = parse_quote! {};
let template = LogTemplate::new("test_func", ¶ms, &return_type, false);
let end_template = template.format_end_template(true);
assert_eq!(end_template, "test_func [out]: x:{}");
}
#[test]
fn test_format_end_template_no_params_with_return() {
let params = vec![];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let end_template = template.format_end_template(false);
assert_eq!(end_template, "test_func [out]: return:{}");
}
#[test]
fn test_format_end_template_no_params_no_return() {
let params = vec![];
let return_type: ReturnType = parse_quote! {};
let template = LogTemplate::new("test_func", ¶ms, &return_type, false);
let end_template = template.format_end_template(false);
assert_eq!(end_template, "test_func [out]");
}
#[test]
fn test_get_log_method() {
let template = LogTemplate::new("test", &[], &parse_quote! {}, false);
// Test all output types
let debug_method = template.get_log_method(&OutputType::Debug);
let info_method = template.get_log_method(&OutputType::Info);
let warn_method = template.get_log_method(&OutputType::Warn);
let error_method = template.get_log_method(&OutputType::Error);
let trace_method = template.get_log_method(&OutputType::Trace);
let print_method = template.get_log_method(&OutputType::Print);
// We can't easily test the exact token content, but we can verify
// that the methods return non-empty token streams
assert!(!debug_method.is_empty());
assert!(!info_method.is_empty());
assert!(!warn_method.is_empty());
assert!(!error_method.is_empty());
assert!(!trace_method.is_empty());
assert!(!print_method.is_empty());
}
#[test]
fn test_generate_log_statements_on_start() {
let params = vec![format_ident!("x")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let (start, end) = template.generate_log_statements_with_context(
&OutputPosition::OnStart,
&OutputType::Print,
¶ms,
&[],
);
assert!(!start.is_empty());
assert!(end.is_empty());
}
#[test]
fn test_generate_log_statements_on_end() {
let params = vec![format_ident!("x")];
let saved_params = vec![format_ident!("__x_value__")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let (start, end) = template.generate_log_statements_with_context(
&OutputPosition::OnEnd,
&OutputType::Print,
¶ms,
&saved_params,
);
assert!(start.is_empty());
assert!(!end.is_empty());
}
#[test]
fn test_generate_log_statements_on_start_and_end() {
let params = vec![format_ident!("x")];
let saved_params = vec![format_ident!("__x_value__")];
let return_type: ReturnType = parse_quote! { -> i32 };
let template = LogTemplate::new("test_func", ¶ms, &return_type, true);
let (start, end) = template.generate_log_statements_with_context(
&OutputPosition::OnStartAndEnd,
&OutputType::Print,
¶ms,
&saved_params,
);
assert!(!start.is_empty());
assert!(!end.is_empty());
}
#[test]
fn test_generate_log_statements_no_params_no_return() {
let params = vec![];
let return_type: ReturnType = parse_quote! {};
let template = LogTemplate::new("test_func", ¶ms, &return_type, false);
let (start, end) = template.generate_log_statements_with_context(
&OutputPosition::OnStartAndEnd,
&OutputType::Debug,
¶ms,
&[],
);
assert!(!start.is_empty());
assert!(!end.is_empty());
}
}