Skip to main content

vector_core/
macros.rs

1//! Log macros shared across all Vector clients.
2//!
3//! `log_info!`, `log_debug!`, `log_trace!` compile to no-ops in release builds.
4//! `log_warn!` always compiles in (with UTC timestamps). In ALL builds each
5//! macro is gated at runtime by the active level (see `crate::logging`): default
6//! WARN, override with `VECTOR_LOG=trace|debug|info|warn|error|off`. The level
7//! check is cheap and the message args aren't formatted when suppressed.
8
9// Release builds strip the info/debug/trace bodies entirely, so a variable
10// referenced only inside one of these macros would go unused. `keep_used!`
11// re-references the args in a dead `if false` block: DCE removes it (zero
12// runtime cost, args never evaluated) while the borrow checker still counts
13// the args as used, so call sites stay warning-free in every profile.
14#[macro_export]
15#[doc(hidden)]
16macro_rules! __log_keep_used {
17    ($($arg:tt)*) => {{
18        #[cfg(not(debug_assertions))]
19        if false { let _ = format_args!($($arg)*); }
20    }};
21}
22
23/// stderr natively; the browser console on wasm32, where stderr is a sink.
24#[macro_export]
25#[doc(hidden)]
26macro_rules! __log_line {
27    ($($arg:tt)*) => {{
28        #[cfg(not(target_arch = "wasm32"))]
29        eprintln!($($arg)*);
30        #[cfg(target_arch = "wasm32")]
31        $crate::logging::console_line(&format!($($arg)*));
32    }};
33}
34
35#[macro_export]
36macro_rules! log_info {
37    ($($arg:tt)*) => {{
38        #[cfg(debug_assertions)]
39        if $crate::logging::level_enabled($crate::logging::LEVEL_INFO) {
40            $crate::__log_line!("[INFO] {}", format_args!($($arg)*));
41        }
42        $crate::__log_keep_used!($($arg)*);
43    }};
44}
45
46#[macro_export]
47macro_rules! log_debug {
48    ($($arg:tt)*) => {{
49        #[cfg(debug_assertions)]
50        if $crate::logging::level_enabled($crate::logging::LEVEL_DEBUG) {
51            $crate::__log_line!("[DEBUG] {}", format_args!($($arg)*));
52        }
53        $crate::__log_keep_used!($($arg)*);
54    }};
55}
56
57#[macro_export]
58macro_rules! log_trace {
59    ($($arg:tt)*) => {{
60        #[cfg(debug_assertions)]
61        if $crate::logging::level_enabled($crate::logging::LEVEL_TRACE) {
62            $crate::__log_line!("[TRACE] {}", format_args!($($arg)*));
63        }
64        $crate::__log_keep_used!($($arg)*);
65    }};
66}
67
68#[macro_export]
69macro_rules! log_warn {
70    ($($arg:tt)*) => {{
71        if $crate::logging::level_enabled($crate::logging::LEVEL_WARN) {
72            // device-clock: log lines keep the time the device showed.
73            let _secs = $crate::rt::time_std::SystemTime::now()
74                .duration_since($crate::rt::time_std::UNIX_EPOCH)
75                .unwrap_or_default()
76                .as_secs();
77            $crate::__log_line!("[WARN {:02}:{:02}:{:02}Z] {}", (_secs / 3600) % 24, (_secs / 60) % 60, _secs % 60, format_args!($($arg)*));
78        }
79    }};
80}
81
82/// Network-failure log: a WARN that ALSO lands in the app's persistent log
83/// (Settings > Copy Logs) via the registered sink — for upload/download/
84/// mirror failures the user may need to report long after the console
85/// scrolled away. No toast: fallbacks often succeed right after.
86#[macro_export]
87macro_rules! log_net_fail {
88    ($($arg:tt)*) => {{
89        let msg = format!($($arg)*);
90        // Console print matches log_warn! exactly (level-gated); persistence
91        // is unconditional — the log file exists for after-the-fact diagnosis.
92        if $crate::logging::level_enabled($crate::logging::LEVEL_WARN) {
93            $crate::__log_line!("[WARN] {}", &msg);
94        }
95        $crate::logging::persist(&format!(
96            "[{} WARN] {}",
97            $crate::logging::timestamp_utc(),
98            &msg
99        ));
100    }};
101}
102
103/// Network-milestone log: persisted like [`log_net_fail!`] but INFO-toned —
104/// which server won an upload, which fallback served a download, mirror
105/// results. Persisted even in release (where `log_info!` compiles out):
106/// the persistent log exists precisely for release-build diagnosis.
107#[macro_export]
108macro_rules! log_net_info {
109    ($($arg:tt)*) => {{
110        let msg = format!($($arg)*);
111        #[cfg(debug_assertions)]
112        if $crate::logging::level_enabled($crate::logging::LEVEL_INFO) {
113            $crate::__log_line!("[INFO] {}", &msg);
114        }
115        $crate::logging::persist(&format!(
116            "[{} INFO] {}",
117            $crate::logging::timestamp_utc(),
118            &msg
119        ));
120    }};
121}