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
/// The standard logging macro.
///
/// This macro will generically log with the specified `Level` and `format!`
/// based argument list.
///
/// # Examples
///
/// ```no_run
/// use wasmcloud_component::log;
/// use wasmcloud_component::wasi::logging::logging::Level;
///
/// # fn main() {
/// let data = (42, "Forty-two");
/// let private_data = "private";
///
/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
/// log!(context: "app_events", Level::Warn, "App warning: {}, {}, {}",
///     data.0, data.1, private_data);
/// # }
/// ```
#[macro_export]
macro_rules! log {
    // log!(context: "my_context", Level::Info, "a {} event", "log");
    (context: $context:expr, $lvl:expr, $($arg:tt)+) => ({
        $crate::wasi::logging::logging::log(
            $lvl,
            $context,
            &std::fmt::format(format_args!($($arg)*)),
        );
    });

    // log!(context: "my_context", Level::Info; "a {} event", "log");
    (context: $context:expr, $lvl:expr; $($arg:tt)+) => ({
        ($crate::log!(context: $context, $lvl, $($arg)+));
    });

    // log!(Level::Info, "a log event")
    ($lvl:expr, $($arg:tt)+) => ($crate::log!(context: "", $lvl, $($arg)+));
}

#[macro_export]
macro_rules! trace {
    // trace!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Trace, $($arg)+));

    // trace!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Trace; $($arg)+));

    // trace!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Trace, $($arg)+))
}

#[macro_export]
macro_rules! debug {
    // debug!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Debug, $($arg)+));

    // debug!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Debug; $($arg)+));

    // debug!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Debug, $($arg)+))
}

#[macro_export]
macro_rules! info {
    // info!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Info, $($arg)+));

    // info!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Info; $($arg)+));

    // info!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Info, $($arg)+))
}

#[macro_export]
macro_rules! warn {
    // warn!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Warn, $($arg)+));

    // warn!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Warn; $($arg)+));

    // warn!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Warn, $($arg)+))
}

#[macro_export]
macro_rules! error {
    // error!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Error, $($arg)+));

    // error!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Error; $($arg)+));

    // error!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Error, $($arg)+))
}

#[macro_export]
macro_rules! critical {
    // critical!(context: "context", "a {} event", "log")
    (context: $context:expr, $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Critical, $($arg)+));

    // critical!(context: "context"; "a {} event", "log")
    (context: $context:expr; $($arg:tt)+) => ($crate::log!(context: $context, $crate::wasi::logging::logging::Level::Critical; $($arg)+));

    // critical!("a {} event", "log")
    ($($arg:tt)+) => ($crate::log!($crate::wasi::logging::logging::Level::Critical, $($arg)+))
}