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
// TODO: Add #[cfg] options to disable levels completely, eliminating
// all the code associated with logging those levels completely from
// the executable

// TODO: Switch to proc macros to allow us to automatically access
// `cx` without mentioning it explicitly.

/// Log an error with context info
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! error {
    ( $($x:tt)+ ) => {{
        $crate::log!(Error $($x)+);
    }}
}

/// Log a warning with context info
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! warn {
    ( $($x:tt)+ ) => {{
        $crate::log!(Warn $($x)+);
    }}
}

/// Log information with context info
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! info {
    ( $($x:tt)+ ) => {{
        $crate::log!(Info $($x)+);
    }}
}

/// Log debugging with context info
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! debug {
    ( $($x:tt)+ ) => {{
        $crate::log!(Debug $($x)+);
    }}
}

/// Log tracing with context info
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! trace {
    ( $($x:tt)+ ) => {{
        $crate::log!(Trace $($x)+);
    }}
}

/// Log an audit record
///
/// See [top-level docs](index.html) for details.
#[macro_export]
macro_rules! audit {
    ( [$($cx:tt)+], $tag:ident $(, $($tail:tt)+)? ) => {{
        $crate::log!(Audit [$($cx)+] $(, $($tail)+)? , "{}", ::std::stringify!($tag));
    }};
    ( [$($cx:tt)+], $tag:literal $(, $($tail:tt)+)? ) => {{
        $crate::log!(Audit [$($cx)+] $(, $($tail)+)? , "{}", $tag);
    }};
    ( [$($cx:tt)+], ($tag:expr) $(, $($tail:tt)+)? ) => {{
        $crate::log!(Audit [$($cx)+] $(, $($tail)+)? , "{}", $tag);
    }};
}

#[macro_export]
#[doc(hidden)]
macro_rules! log_key_string {
    ($key:ident) => {
        ::std::stringify!($key)
    };
    ($base:ident . $($tail:tt)+) => {
        $crate::log_key_string!($($tail)+)
    };
}

/// Internal macro which handles translation to a log call
//
// Called with: `(level [cx] kv-args fmt fmt-args)`
#[macro_export]
macro_rules! log {
    // Initial part
    ($level:ident $fmt:literal $($tail:tt)*) => {{
        ::std::compile_error!("Stakker logging macros need `[cx]` or `[core]` or `[actor, core]` as first argument");
    }};
    ($level:ident [$cx:expr] $(, $($tail:tt)+)?) => {{
        $crate::log!($level [$cx, $cx] $(, $($tail)+)?)
    }};
    ($level:ident [$src:expr, $core:expr], target: $target:literal $(, $($tail:tt)+)?) => {{
        $crate::log!([$src.access_log_id(), $core, $level, $target] $($($tail)+)?)
    }};
    ($level:ident [$src:expr, $core:expr] $(, $($tail:tt)+)?) => {{
        $crate::log!([$src.access_log_id(), $core, $level, ""] $($($tail)+)?)
    }};
    ($level:ident $($tail:tt)*) => {{
        ::std::compile_error!("Stakker logging macros need `[cx]` or `[core]` or `[actor, core]` as first argument");
    }};
    // Primitive values (no % or ?)
    ([$($a:tt)*] $key1:ident $(. $key2:ident)*  $(, $($tail:tt)*)?) => {
        $crate::log!([$($a)* ($crate::log_key_string!($key1$(.$key2)*), $key1$(.$key2)*)] $($($tail)*)?)
    };
    ([$($a:tt)*] $key:ident : $value:expr $(, $($tail:tt)*)?) => {
        $crate::log!([$($a)* (::std::stringify!($key), $value)] $($($tail)*)?)
    };
    ([$($a:tt)*] $key:literal : $value:expr $(, $($tail:tt)*)?) => {
        $crate::log!([$($a)* ($key, $value)] $($($tail)*)?)
    };
    // Display-formatted values (with %)
    ([$($a:tt)*] % $key1:ident $(. $key2:ident)* $(, $($tail:tt)*)?) => {{
        let v = &($key1$(.$key2)*); // Do borrow outside of closure
        $crate::log!([$($a)* ($crate::log_key_string!($key1$(.$key2)*), format_args!("{}", v))] $($($tail)*)?)
    }};
    ([$($a:tt)*] $key:ident : % $value:expr $(, $($tail:tt)*)?) => {{
        let v = &$value; // Do borrow outside of closure
        $crate::log!([$($a)* (::std::stringify!($key), format_args!("{}", v))] $($($tail)*)?)
    }};
    ([$($a:tt)*] $key:literal : % $value:expr $(, $($tail:tt)*)?) => {{
        let v = &$value; // Do borrow outside of closure
        $crate::log!([$($a)* ($key, format_args!("{}", v))] $($($tail)*)?)
    }};
    // Debug-formatted values (with ?)
    ([$($a:tt)*] ? $key1:ident $(. $key2:ident)* $(, $($tail:tt)*)?) => {{
        let v = &($key1$(.$key2)*); // Do borrow outside of closure
        $crate::log!([$($a)* ($crate::log_key_string!($key1$(.$key2)*), format_args!("{:?}", v))] $($($tail)*)?)
    }};
    ([$($a:tt)*] $key:ident : ? $value:expr $(, $($tail:tt)*)?) => {{
        let v = &$value; // Do borrow outside of closure
        $crate::log!([$($a)* (::std::stringify!($key), format_args!("{:?}", v))] $($($tail)*)?)
    }};
    ([$($a:tt)*] $key:literal : ? $value:expr $(, $($tail:tt)*)?) => {
        let v = &$value; // Do borrow outside of closure
        $crate::log!([$($a)* ($key, format_args!("{:?}", v))] $($($tail)*)?)
    };
    // Final output
    ([$logid:expr, $core:expr, $level:ident, $target:literal $( ($key:expr, $val:expr) )*] $fmt:literal $(, $($tail:tt)*)?) => {{
        use $crate::Visitable;
        let id = $logid;
        let core = $core.access_core();
        core.log(
            id,
            $crate::stakker::LogLevel::$level,
            $target,
            ::std::format_args!( $fmt $(, $($tail)*)? ),
            |output| {
                $( $val.visit(Some($key), output); )*
            });
    }};
}