trace_macros/
lib.rs

1//! A crate with printing macros.
2//!
3//! These macros aims to provide a simple and lazy way to print formatted traces.
4//!
5//! Simple example of usage:
6//!
7//! ```
8//! #[macro_use(TRACE, ENTER)]
9//! extern crate trace_macros;
10//!
11//!fn double_var(var: i32) -> i32 {
12//!    ENTER!(var);
13//!    var * var
14//!}
15//!
16//!fn main() {
17//!    ENTER!();
18//!    TRACE!("I'm", "doing", "some", "lazy", "tracing");
19//!    TRACE!("Result:", double_var(2));
20//!    traceln!("2*2 = {}", double_var(2));
21//!}
22//! ```
23
24
25///WARNING macro which uses ```TRACE!```
26///
27///Prints with the following format: ```file!:line! - WARNING: [Message]```
28#[macro_export]
29macro_rules! WARNING {
30    ($($msg:expr),+) => {{ TRACE!(type => "WARNING", $($msg),+); }};
31}
32
33///ERROR macro which uses ```TRACE!```
34///
35///Prints with the following format: ```file!:line! - ERROR: [Message]```
36#[macro_export]
37macro_rules! ERROR {
38    ($($msg:expr),+) => {{ TRACE!(type => "ERROR", $($msg),+); }};
39}
40
41///INFO macro which uses ```TRACE!```
42///
43///Prints with the following format: ```file!:line! - INFO: [Message]```
44#[macro_export]
45macro_rules! INFO {
46    ($($msg:expr),+) => {{ TRACE!(type => "INFO", $($msg),+); }};
47}
48
49///DEBUG macro which uses ```TRACE!```
50///
51///Prints with the following format: ```file!:line! - DEBUG: [Message]```
52#[macro_export]
53macro_rules! DEBUG {
54    ($($msg:expr),+) => {{ TRACE!(type => "DEBUG", $($msg),+); }};
55}
56
57///ENTER macro which uses ```TRACE!```
58///
59///Prints with the following format: ```file!:line! - ENTER: [arg_name=arg_value]```
60///
61///It is assumed to be used to wrap function call so all arguments are stringified, if any
62#[macro_export]
63macro_rules! ENTER {
64    () => {{ TRACE!(type => "ENTER"); }};
65    ($($msg:expr),+) => {{ TRACE!(type => "ENTER", $(format!("{}={}", stringify!($msg), $msg)),+); }};
66}
67
68///Trace macro which concats passed arguments into one string.
69///
70///Prints with the following format: ```file!:line! - [type:] [Message]```
71///
72///Usage:
73///
74///* ```TRACE!(type=>[TYPE], sep=>[String], [arg1, arg2, ..., argN])```
75///* ```TRACE!(type=>[TYPE], [arg1, arg2, ..., argN])```
76///* ```TRACE!(sep=>[String], [arg1, arg2, ..., argN])```
77///* ```TRACE!([arg1, arg2, ..., argN])```
78///* ```TRACE!()```
79///
80///Arguments must have ```fmt::Display``` trait.
81#[macro_export]
82macro_rules! TRACE {
83    (type=>$tp:expr, sep=>$sep:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
84    (type=>$tp:expr, sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{}", $arg),)+].connect($sep)); }};
85    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
86    (type=>$tp:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{}", $arg),)+].connect(" ")); }};
87    (type=>$tp:expr) => {{ println!("{}:{} - {}", file!(), line!(), $tp); }};
88    (sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{}", $arg),)+].connect($sep)); }};
89    ($msg:expr) => {{ println!("{}:{} - {}", file!(), line!(), $msg); }};
90    ($($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{}", $arg),)+].connect(" ")); }};
91    () => {{ println!("{}:{}", file!(), line!()); }};
92}
93
94///Debug trace macro like ```TRACE!```
95///
96///Prints with the following format: ```file!:line! - [type:] [Message]```
97///
98///Usage:
99///
100///* ```TRACE!(type=>[TYPE], sep=>[String], [arg1, arg2, ..., argN])```
101///* ```TRACE!(type=>[TYPE], [arg1, arg2, ..., argN])```
102///* ```TRACE!(sep=>[String], [arg1, arg2, ..., argN])```
103///* ```TRACE!([arg1, arg2, ..., argN])```
104///* ```TRACE!()```
105///
106///It is the same as ```TRACE!``` except that it prints with ```fmt::Debug``` trait.
107#[macro_export]
108macro_rules! DEBUG_TRACE {
109    (type=>$tp:expr, sep=>$sep:expr, $msg:expr) => {{ println!("{}:{} - {}: {:?}", file!(), line!(), $tp, $msg); }};
110    (type=>$tp:expr, sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{:?}", $arg),)+].connect($sep)); }};
111    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {:?}", file!(), line!(), $tp, $msg); }};
112    (type=>$tp:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{:?}", $arg),)+].connect(" ")); }};
113    (type=>$tp:expr) => {{ println!("{}:{} - {}", file!(), line!(), $tp); }};
114    (sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{:?}", $arg),)+].connect($sep)); }};
115    ($msg:expr) => {{ println!("{}:{} - {:?}", file!(), line!(), $msg); }};
116    ($($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{:?}", $arg),)+].connect(" ")); }};
117    () => {{ println!("{}:{}", file!(), line!()); }};
118}
119
120///Trace macro like ```println```
121///
122///It uses ```format_args!``` for creating formatted string from passed arguments.
123///
124///Prints with the following format: ```file!:line! - [type:] [Message]```
125///
126#[macro_export]
127macro_rules! traceln {
128    (type=>$tp:expr, $($arg:tt)+) => {{ traceln!("{}: {}", $tp, format_args!($($arg)+)); }};
129    (type=>$tp:expr) => {{ traceln!("{}", $tp); }};
130    ($($arg:tt)+) => {{ println!("{}", format_args!("{}:{} - {}", file!(), line!(), format_args!($($arg)+))); }};
131}
132
133///Thread trace macro like ```println```
134///
135///It uses ```format_args!``` for creating formatted string from passed arguments.
136///
137///Prints with the following format: ```[thread_name] file!:line! - [type:] [Message]```
138///
139#[macro_export]
140macro_rules! th_trace {
141    (type=>$tp:expr, $($arg:tt)+) => {{ th_trace!("{}: {}", $tp, format_args!($($arg)+)); }};
142    (type=>$tp:expr) => {{ th_trace!("{}", $tp); }};
143    ($($arg:tt)+) => {{ println!("{}", format_args!("[{}] {}:{} - {}", std::thread::current().name().unwrap_or("none"), file!(), line!(), format_args!($($arg)+))); }};
144}
145
146///WARNING macro which uses ```traceln!```
147///
148///Prints with the following format: ```file!:line! - WARNING: [Message]```
149#[macro_export]
150macro_rules! warning {
151    ($($msg:tt)+) => {{ traceln!(type=>"WARNING", $($msg)+); }};
152}
153
154///ERROR macro which uses ```traceln!```
155///
156///Prints with the following format: ```file!:line! - ERROR: [Message]```
157#[macro_export]
158macro_rules! error {
159    ($($msg:tt)+) => {{ traceln!(type=>"ERROR", $($msg)+); }};
160}
161
162///INFO macro which uses ```traceln!```
163///
164///Prints with the following format: ```file!:line! - INFO: [Message]```
165#[macro_export]
166macro_rules! info {
167    ($($msg:tt)+) => {{ traceln!(type=>"INFO", $($msg)+); }};
168}
169
170///DEBUG macro which uses ```traceln!```
171///
172///Prints with the following format: ```file!:line! - DEBUG: [Message]```
173#[macro_export]
174macro_rules! debug {
175    ($($msg:tt)+) => {{ traceln!(type=>"DEBUG", $($msg)+); }};
176}
177
178///ENTER macro which uses ```traceln!```
179///
180///Prints with the following format: ```file!:line! - ENTER: [arg_name=arg_value]```
181///
182///It is assumed to be used to wrap function call so all arguments are stringified, if any
183#[macro_export]
184macro_rules! enter {
185    () => {{ traceln!(type=>"ENTER"); }};
186    ($($msg:expr),+) => {{ traceln!(type=>"ENTER", "{}", [$(format!("{}={}", stringify!($msg), $msg)),+].connect(" ")); }};
187}
188
189///Simplified trace macro
190///
191///Prints with the following format: ```file!:line! - [TYPE:] [Message]```
192///
193///Argument must have ```fmt::Display``` trait.
194#[macro_export]
195macro_rules! strace {
196    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
197    ($msg:expr) => {{ println!("{}:{} - {}", file!(), line!(), $msg); }};
198}
199
200///Macro to concat several arguments into one string.
201///
202///Arguments:
203///
204///* ```sep``` is a string which is used to separate arguments. Default is white-space.
205///* ```formatter``` is a valid string to pass in ```format!``` . Default is ```"{}"```.
206///
207///Usage:
208///
209///* ```connect_args!(formatter=>[String], sep=>[String], [arg1, arg2, ..., argN])```
210///* ```connect_args!(sep=>[String], [arg1, arg2, ..., argN])```
211///* ```connect_args!(formatter=>[String], [arg1, arg2, ..., argN])```
212///* ```connect_args!([arg1, arg2, ..., argN])```
213#[macro_export]
214macro_rules! connect_args {
215    (formatter=>$fr:expr, sep=>$sep:expr, $($arg:expr),+) => { [$(format!($fr, $arg),)+].connect($sep) };
216    (sep=>$sep:expr, $($arg:expr),+) => { [$(format!("{}", $arg),)+].connect($sep) };
217    (formatter=>$fr:expr, $($arg:expr),+) => { [$(format!($fr, $arg),)+].connect(" ") };
218    ($msg:expr) => { format!("{}", $msg) };
219    ($($arg:expr),+) => { [$(format!("{}", $arg),)+].connect(" ") };
220}