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#[macro_export]
24macro_rules! log_info {
25    ($($arg:tt)*) => {{
26        #[cfg(debug_assertions)]
27        if $crate::logging::level_enabled($crate::logging::LEVEL_INFO) {
28            eprintln!("[INFO] {}", format_args!($($arg)*));
29        }
30        $crate::__log_keep_used!($($arg)*);
31    }};
32}
33
34#[macro_export]
35macro_rules! log_debug {
36    ($($arg:tt)*) => {{
37        #[cfg(debug_assertions)]
38        if $crate::logging::level_enabled($crate::logging::LEVEL_DEBUG) {
39            eprintln!("[DEBUG] {}", format_args!($($arg)*));
40        }
41        $crate::__log_keep_used!($($arg)*);
42    }};
43}
44
45#[macro_export]
46macro_rules! log_trace {
47    ($($arg:tt)*) => {{
48        #[cfg(debug_assertions)]
49        if $crate::logging::level_enabled($crate::logging::LEVEL_TRACE) {
50            eprintln!("[TRACE] {}", format_args!($($arg)*));
51        }
52        $crate::__log_keep_used!($($arg)*);
53    }};
54}
55
56#[macro_export]
57macro_rules! log_warn {
58    ($($arg:tt)*) => {{
59        if $crate::logging::level_enabled($crate::logging::LEVEL_WARN) {
60            let _secs = std::time::SystemTime::now()
61                .duration_since(std::time::UNIX_EPOCH)
62                .unwrap_or_default()
63                .as_secs();
64            eprintln!("[WARN {:02}:{:02}:{:02}Z] {}", (_secs / 3600) % 24, (_secs / 60) % 60, _secs % 60, format_args!($($arg)*));
65        }
66    }};
67}
68
69/// Network-failure log: a WARN that ALSO lands in the app's persistent log
70/// (Settings > Copy Logs) via the registered sink — for upload/download/
71/// mirror failures the user may need to report long after the console
72/// scrolled away. No toast: fallbacks often succeed right after.
73#[macro_export]
74macro_rules! log_net_fail {
75    ($($arg:tt)*) => {{
76        let msg = format!($($arg)*);
77        // Console print matches log_warn! exactly (level-gated); persistence
78        // is unconditional — the log file exists for after-the-fact diagnosis.
79        if $crate::logging::level_enabled($crate::logging::LEVEL_WARN) {
80            eprintln!("[WARN] {}", &msg);
81        }
82        $crate::logging::persist(&format!(
83            "[{} WARN] {}",
84            $crate::logging::timestamp_utc(),
85            &msg
86        ));
87    }};
88}
89
90/// Network-milestone log: persisted like [`log_net_fail!`] but INFO-toned —
91/// which server won an upload, which fallback served a download, mirror
92/// results. Persisted even in release (where `log_info!` compiles out):
93/// the persistent log exists precisely for release-build diagnosis.
94#[macro_export]
95macro_rules! log_net_info {
96    ($($arg:tt)*) => {{
97        let msg = format!($($arg)*);
98        #[cfg(debug_assertions)]
99        if $crate::logging::level_enabled($crate::logging::LEVEL_INFO) {
100            eprintln!("[INFO] {}", &msg);
101        }
102        $crate::logging::persist(&format!(
103            "[{} INFO] {}",
104            $crate::logging::timestamp_utc(),
105            &msg
106        ));
107    }};
108}