zune_core/log.rs
1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9// Re-export macros under nicer names
10pub use crate::{
11 __debug as debug, __error as error, __info as info, __log_enabled as log_enabled,
12 __trace as trace, __warn as warn
13};
14
15#[repr(usize)]
16#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
17pub enum Level {
18 Error = 1,
19 Warn,
20 Info,
21 Debug,
22 Trace
23}
24
25//
26// log_enabled (unchanged)
27//
28#[doc(hidden)]
29#[macro_export]
30macro_rules! __log_enabled {
31 ($lvl:expr) => {{
32 let _ = $lvl;
33 false
34 }};
35}
36
37//
38// ERROR
39//
40#[cfg(feature = "std")]
41#[doc(hidden)]
42#[macro_export]
43macro_rules! __error {
44 ($($arg:tt)+) => {{
45 // Expand to a block so the macro is usable in expression position.
46 // `{}` alone expands to *nothing*, which is a hard error for callers
47 // that use the macro as a trailing expression.
48 let _ = ::core::format_args!($($arg)+);
49 }};
50}
51
52#[cfg(not(feature = "std"))]
53#[doc(hidden)]
54#[macro_export]
55macro_rules! __error {
56 ($($arg:tt)+) => {{
57 // Expand to a block so the macro is usable in expression position.
58 // `{}` alone expands to *nothing*, which is a hard error for callers
59 // that use the macro as a trailing expression.
60 let _ = ::core::format_args!($($arg)+);
61 }};
62}
63
64//
65// WARN
66//
67#[cfg(feature = "std")]
68#[doc(hidden)]
69#[macro_export]
70macro_rules! __warn {
71 ($($arg:tt)+) => {{
72 // Expand to a block so the macro is usable in expression position.
73 // `{}` alone expands to *nothing*, which is a hard error for callers
74 // that use the macro as a trailing expression.
75 let _ = ::core::format_args!($($arg)+);
76 }};
77}
78
79#[cfg(not(feature = "std"))]
80#[doc(hidden)]
81#[macro_export]
82macro_rules! __warn {
83 ($($arg:tt)+) => {{
84 // Expand to a block so the macro is usable in expression position.
85 // `{}` alone expands to *nothing*, which is a hard error for callers
86 // that use the macro as a trailing expression.
87 let _ = ::core::format_args!($($arg)+);
88 }};
89}
90
91//
92// INFO
93//
94#[cfg(feature = "std")]
95#[doc(hidden)]
96#[macro_export]
97macro_rules! __info {
98 ($($arg:tt)+) => {{
99 // Expand to a block so the macro is usable in expression position.
100 // `{}` alone expands to *nothing*, which is a hard error for callers
101 // that use the macro as a trailing expression.
102 let _ = ::core::format_args!($($arg)+);
103 }};
104}
105
106#[cfg(not(feature = "std"))]
107#[doc(hidden)]
108#[macro_export]
109macro_rules! __info {
110 ($($arg:tt)+) => {{
111 // Expand to a block so the macro is usable in expression position.
112 // `{}` alone expands to *nothing*, which is a hard error for callers
113 // that use the macro as a trailing expression.
114 let _ = ::core::format_args!($($arg)+);
115 }};
116}
117
118//
119// DEBUG
120//
121#[cfg(feature = "std")]
122#[doc(hidden)]
123#[macro_export]
124macro_rules! __debug {
125 ($($arg:tt)+) => {{
126 // Expand to a block so the macro is usable in expression position.
127 // `{}` alone expands to *nothing*, which is a hard error for callers
128 // that use the macro as a trailing expression.
129 let _ = ::core::format_args!($($arg)+);
130 }};
131}
132
133#[cfg(not(feature = "std"))]
134#[doc(hidden)]
135#[macro_export]
136macro_rules! __debug {
137 ($($arg:tt)+) => {{
138 // Expand to a block so the macro is usable in expression position.
139 // `{}` alone expands to *nothing*, which is a hard error for callers
140 // that use the macro as a trailing expression.
141 let _ = ::core::format_args!($($arg)+);
142 }};
143}
144
145//
146// TRACE
147//
148#[cfg(feature = "std")]
149#[doc(hidden)]
150#[macro_export]
151macro_rules! __trace {
152 ($($arg:tt)+) => {{
153 // Expand to a block so the macro is usable in expression position.
154 // `{}` alone expands to *nothing*, which is a hard error for callers
155 // that use the macro as a trailing expression.
156 let _ = ::core::format_args!($($arg)+);
157 }};
158}
159
160#[cfg(not(feature = "std"))]
161#[doc(hidden)]
162#[macro_export]
163macro_rules! __trace {
164 ($($arg:tt)+) => {{
165 // Expand to a block so the macro is usable in expression position.
166 // `{}` alone expands to *nothing*, which is a hard error for callers
167 // that use the macro as a trailing expression.
168 let _ = ::core::format_args!($($arg)+);
169 }};
170}
171
172#[cfg(test)]
173mod tests {
174 use crate::log::{debug, error, info, trace, warn};
175
176 /// The no-op logging macros must expand to a valid expression of type `()`,
177 /// not to nothing at all.
178 ///
179 /// Downstream crates call these as the trailing expression of a block, e.g.
180 /// `else { warn!("..") }`. A macro with an empty transcriber still works as
181 /// a statement, so this crate keeps compiling standalone and the breakage
182 /// only shows up in dependents.
183 #[test]
184 fn macros_expand_in_expression_position() {
185 let value = 42;
186
187 let _: () = { error!("error {}", value) };
188 let _: () = { warn!("warn {}", value) };
189 let _: () = { info!("info {}", value) };
190 let _: () = { debug!("debug {}", value) };
191 let _: () = { trace!("trace {}", value) };
192 }
193
194 /// Statement position must keep working too.
195 #[test]
196 fn macros_expand_in_statement_position() {
197 warn!("no args");
198 warn!("with args {} {:?}", 1, "two");
199 }
200}