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
//! Zero-fuss debug tracing macro.
//!
//! Cargo.toml:
//! ```text
//! [dependencies]
//! eztrace = "*"
//! ```
//!
//! Usage:
//! ```
//! #[macro_use]
//! extern crate eztrace;
//! # fn main() {
//! # let (my_variable, other_variable) = (42, 237);
//! trace!(my_variable, other_variable);
//! # }
//! ```
//!
//! Prints this:
//! ```text
//! my_variable, other_variable: 42 237
//! ```

/// Prints out variables and their debug representation.
///
/// Non-`Copy` types do what you would hope.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # #[macro_use] extern crate eztrace;
/// let a = 3;
/// let b = 4;
/// let c = 5;
/// trace!(a, b, c);
/// // a, b, c: 3 4 5
/// trace!(a * a + b * b, c * c);
/// // a * a + b * b, c * c: 25 25
/// ```
///
/// To print just the file & line:
///
/// ```
/// # #[macro_use] extern crate eztrace;
/// trace!();
/// // eztrace.rs:1
/// ```
#[macro_export]
macro_rules! trace {
    () => {
        println!(trace!(@line));
    };
    ($($IT:expr),* $(,)*) => {
        println!(
            trace!(@fmt $($IT),*),
            $(&$IT),*
        );
    };
    (@line) => {
        concat!(
            file!(), ":", line!(),
        )
    };
    (@fmt $($IT:expr),*) => {
        concat!(
            trace!(@stringify $($IT,)*),
            ":",
            $(trace!(@fmtcode $IT)),*
        )
    };
    (@fmtcode $_:expr) => {
        " {:?}"
    };
    (@stringify $HEAD:expr, $($IT:expr,)*) => {
        concat!(
            stringify!($HEAD),
            $(
                ", ",
                stringify!($IT),
            )*
        )
    };
}


#[cfg(test)]
mod tests {
    #[test]
    fn no_move() {
        let string = format!("hey");
        trace!(string, 9);
        trace!(string, string);
        trace!(
            string,
            string,
            string,
        );
    }

    #[test]
    fn single_eval_per_arg() {
        let mut n = 0;
        fn incr(i: &mut usize) { *i += 1; }
        trace!(
            "check that each argument",
            "is only evaluated",
            "once",
            incr(&mut n),
        );
        trace!(n);
        assert_eq!(n, 1);
    }

    #[test]
    fn empty() {
        trace!();
    }

    #[test]
    fn single() {
        trace!("hello");
    }

    #[test]
    fn multi() {
        trace!("hello", "world!");
    }

    #[test]
    fn the_docs() {
        let a = 3;
        let b = 4;
        let c = 5;
        trace!(a, b, c);
        // a, b, c: 3 4 5
        trace!(a * a + b * b, c * c);
        // a * a + b * b, c * c: 25 25
        trace!();
    }
}