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
//! A crate with printing macros.
//!
//! These macros aims to provide a simple and lazy way to print formatted traces.
//!
//!# Example:
//!
//! ```
//! #[macro_use(TRACE, ENTER)]
//! extern crate trace_macros;
//!
//!fn double_var(var: i32) -> i32 {
//!    ENTER!(var);
//!    var * var
//!}
//!
//!fn main() {
//!    ENTER!();
//!    TRACE!("I'm", "doing", "some", "lazy", "tracing");
//!    TRACE!("Result:", double_var(2));
//!    traceln!("2*2 = {}", double_var(2));
//!}
//! ```


///WARNING macro which uses ```TRACE!```
///
///Prints with the following format: ```file!:line! - WARNING: [Message]```
#[macro_export]
macro_rules! WARNING {
    ($($msg:expr),+) => {{ TRACE!(type => "WARNING", $($msg),+); }};
}

///ERROR macro which uses ```TRACE!```
///
///Prints with the following format: ```file!:line! - ERROR: [Message]```
#[macro_export]
macro_rules! ERROR {
    ($($msg:expr),+) => {{ TRACE!(type => "ERROR", $($msg),+); }};
}

///INFO macro which uses ```TRACE!```
///
///Prints with the following format: ```file!:line! - INFO: [Message]```
#[macro_export]
macro_rules! INFO {
    ($($msg:expr),+) => {{ TRACE!(type => "INFO", $($msg),+); }};
}

///DEBUG macro which uses ```TRACE!```
///
///Prints with the following format: ```file!:line! - DEBUG: [Message]```
#[macro_export]
macro_rules! DEBUG {
    ($($msg:expr),+) => {{ TRACE!(type => "DEBUG", $($msg),+); }};
}

///ENTER macro which uses ```TRACE!```
///
///Prints with the following format: ```file!:line! - ENTER: [arg_name=arg_value]```
///
///It is assumed to be used to wrap function call so all arguments are stringified, if any
#[macro_export]
macro_rules! ENTER {
    () => {{ TRACE!(type => "ENTER"); }};
    ($($msg:expr),+) => {{ TRACE!(type => "ENTER", $(format!("{}={}", stringify!($msg), $msg)),+); }};
}

///Trace macro which concats passed arguments into one string.
///
///Prints with the following format: ```file!:line! - [type:] [Message]```
///
///Usage:
///
///* ```TRACE!(type=>[TYPE], sep=>[String], [arg1, arg2, ..., argN])```
///* ```TRACE!(type=>[TYPE], [arg1, arg2, ..., argN])```
///* ```TRACE!(sep=>[String], [arg1, arg2, ..., argN])```
///* ```TRACE!([arg1, arg2, ..., argN])```
///* ```TRACE!()```
///
///Arguments must have ```fmt::Display``` trait.
#[macro_export]
macro_rules! TRACE {
    (type=>$tp:expr, sep=>$sep:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
    (type=>$tp:expr, sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{}", $arg),)+].connect($sep)); }};
    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
    (type=>$tp:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{}", $arg),)+].connect(" ")); }};
    (type=>$tp:expr) => {{ println!("{}:{} - {}", file!(), line!(), $tp); }};
    (sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{}", $arg),)+].connect($sep)); }};
    ($msg:expr) => {{ println!("{}:{} - {}", file!(), line!(), $msg); }};
    ($($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{}", $arg),)+].connect(" ")); }};
    () => {{ println!("{}:{}", file!(), line!()); }};
}

///Debug trace macro like ```TRACE!```
///
///Prints with the following format: ```file!:line! - [type:] [Message]```
///
///Usage:
///
///* ```TRACE!(type=>[TYPE], sep=>[String], [arg1, arg2, ..., argN])```
///* ```TRACE!(type=>[TYPE], [arg1, arg2, ..., argN])```
///* ```TRACE!(sep=>[String], [arg1, arg2, ..., argN])```
///* ```TRACE!([arg1, arg2, ..., argN])```
///* ```TRACE!()```
///
///It is the same as ```TRACE!``` except that it prints with ```fmt::Debug``` trait.
#[macro_export]
macro_rules! DEBUG_TRACE {
    (type=>$tp:expr, sep=>$sep:expr, $msg:expr) => {{ println!("{}:{} - {}: {:?}", file!(), line!(), $tp, $msg); }};
    (type=>$tp:expr, sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{:?}", $arg),)+].connect($sep)); }};
    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {:?}", file!(), line!(), $tp, $msg); }};
    (type=>$tp:expr, $($arg:expr),+) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, [$(format!("{:?}", $arg),)+].connect(" ")); }};
    (type=>$tp:expr) => {{ println!("{}:{} - {}", file!(), line!(), $tp); }};
    (sep=>$sep:expr, $($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{:?}", $arg),)+].connect($sep)); }};
    ($msg:expr) => {{ println!("{}:{} - {:?}", file!(), line!(), $msg); }};
    ($($arg:expr),+) => {{ println!("{}:{} - {}", file!(), line!(), [$(format!("{:?}", $arg),)+].connect(" ")); }};
    () => {{ println!("{}:{}", file!(), line!()); }};
}

///Trace macro like ```println```
///
///It uses ```format_args!``` for creating formatted string from passed arguments.
///
///Prints with the following format: ```file!:line! - [type:] [Message]```
///
#[macro_export]
macro_rules! traceln {
    (type=>$tp:expr, $($arg:tt)+) => {{ traceln!("{}: {}", $tp, format_args!($($arg)+)); }};
    (type=>$tp:expr) => {{ traceln!("{}", $tp); }};
    ($($arg:tt)+) => {{ println!("{}", format_args!("{}:{} - {}", file!(), line!(), format_args!($($arg)+))); }};
}

///Thread trace macro like ```println```
///
///It uses ```format_args!``` for creating formatted string from passed arguments.
///
///Prints with the following format: ```[thread_name] file!:line! - [type:] [Message]```
///
#[macro_export]
macro_rules! th_trace {
    (type=>$tp:expr, $($arg:tt)+) => {{ th_trace!("{}: {}", $tp, format_args!($($arg)+)); }};
    (type=>$tp:expr) => {{ th_trace!("{}", $tp); }};
    ($($arg:tt)+) => {{ println!("{}", format_args!("[{}] {}:{} - {}", std::thread::current().name().unwrap_or("none"), file!(), line!(), format_args!($($arg)+))); }};
}

///WARNING macro which uses ```traceln!```
///
///Prints with the following format: ```file!:line! - WARNING: [Message]```
#[macro_export]
macro_rules! warning {
    ($($msg:tt)+) => {{ traceln!(type=>"WARNING", $($msg)+); }};
}

///ERROR macro which uses ```traceln!```
///
///Prints with the following format: ```file!:line! - ERROR: [Message]```
#[macro_export]
macro_rules! error {
    ($($msg:tt)+) => {{ traceln!(type=>"ERROR", $($msg)+); }};
}

///INFO macro which uses ```traceln!```
///
///Prints with the following format: ```file!:line! - INFO: [Message]```
#[macro_export]
macro_rules! info {
    ($($msg:tt)+) => {{ traceln!(type=>"INFO", $($msg)+); }};
}

///DEBUG macro which uses ```traceln!```
///
///Prints with the following format: ```file!:line! - DEBUG: [Message]```
#[macro_export]
macro_rules! debug {
    ($($msg:tt)+) => {{ traceln!(type=>"DEBUG", $($msg)+); }};
}

///ENTER macro which uses ```traceln!```
///
///Prints with the following format: ```file!:line! - ENTER: [arg_name=arg_value]```
///
///It is assumed to be used to wrap function call so all arguments are stringified, if any
#[macro_export]
macro_rules! enter {
    () => {{ traceln!(type=>"ENTER"); }};
    ($($msg:expr),+) => {{ traceln!(type=>"ENTER", "{}", [$(format!("{}={}", stringify!($msg), $msg)),+].connect(" ")); }};
}

///Simplified trace macro
///
///Prints with the following format: ```file!:line! - [TYPE:] [Message]```
///
///Argument must have ```fmt::Display``` trait.
#[macro_export]
macro_rules! strace {
    (type=>$tp:expr, $msg:expr) => {{ println!("{}:{} - {}: {}", file!(), line!(), $tp, $msg); }};
    ($msg:expr) => {{ println!("{}:{} - {}", file!(), line!(), $msg); }};
}

///Macro to concat several arguments into one string.
///
///Arguments:
///
///* ```sep``` is a string which is used to separate arguments. Default is white-space.
///* ```formatter``` is a valid string to pass in ```format!``` . Default is ```"{}"```.
///
///Usage:
///
///* ```connect_args!(formatter=>[String], sep=>[String], [arg1, arg2, ..., argN])```
///* ```connect_args!(sep=>[String], [arg1, arg2, ..., argN])```
///* ```connect_args!(formatter=>[String], [arg1, arg2, ..., argN])```
///* ```connect_args!([arg1, arg2, ..., argN])```
#[macro_export]
macro_rules! connect_args {
    (formatter=>$fr:expr, sep=>$sep:expr, $($arg:expr),+) => { [$(format!($fr, $arg),)+].connect($sep) };
    (sep=>$sep:expr, $($arg:expr),+) => { [$(format!("{}", $arg),)+].connect($sep) };
    (formatter=>$fr:expr, $($arg:expr),+) => { [$(format!($fr, $arg),)+].connect(" ") };
    ($msg:expr) => { format!("{}", $msg) };
    ($($arg:expr),+) => { [$(format!("{}", $arg),)+].connect(" ") };
}