Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. src
  6. log_if/log_if.rs

log_if/
log_if.rs

1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#![no_std]
15
16pub use pw_log_backend_api::LogLevel;
17
18// Re-export dependencies of `pw_log` macros to be accessed via `$crate::__private`.
19#[doc(hidden)]
20pub mod __private {
21    pub use pw_log_backend::{pw_log_backend, pw_logf_backend};
22
23    pub use crate::*;
24}
25
26/// Emit a log message using `core::fmt` format string semantics if condition is true.
27///
28/// `log_if` takes an `expr` condition, a [`LogLevel`], a `core::fmt` style format string,
29/// and necessary arguments to that string and emits a log message to the logging backend.
30///
31/// ```
32/// use pw_log::LogLevel;
33/// use log_if::log_if;
34///
35/// const LOG_FACTS: bool = true;
36/// log_if!(LOG_FACTS, LogLevel::Info, "Log fact: A {} log has a Janka hardness of {} lbf.",
37///     "Spruce Pine" as &str, 700 as i32);
38/// ```
39#[macro_export]
40macro_rules! log_if {
41  ($condition:expr, $log_level:expr, $format_string:literal $(,)?) => {{
42    if $condition {
43      use $crate::__private as __pw_log_crate;
44      $crate::__private::pw_log_backend!($log_level, $format_string)
45    }
46  }};
47
48  ($condition:expr, $log_level:expr, $format_string:literal, $($args:expr),* $(,)?) => {{
49    if $condition {
50      use $crate::__private as __pw_log_crate;
51      $crate::__private::pw_log_backend!($log_level, $format_string, $($args),*)
52    }
53  }};
54}
55
56/// Emit a log message using `printf` format string semantics if condition is true.
57///
58/// `logf_if` takes an `expr` condition, a [`LogLevel`], a `printf` style format string,
59/// and necessary arguments to that string and emits a log message to the logging backend.
60///
61/// ```
62/// use pw_log::LogLevel;
63/// use log_if::logf_if;
64///
65/// const LOG_FACTS: bool = true;
66/// logf_if!(LOG_FACTS, LogLevel::Info, "Log fact: A %s log has a Janka hardness of %d lbf.",
67///     "Spruce Pine", 700);
68/// ```
69#[macro_export]
70macro_rules! logf_if {
71  ($condition:expr, $log_level:expr, $format_string:literal $(,)?) => {{
72    if $condition {
73      use $crate::__private as __pw_log_crate;
74      $crate::__private::pw_logf_backend!($log_level, $format_string)
75    }
76  }};
77
78  ($condition:expr, $log_level:expr, $format_string:literal, $($args:expr),* $(,)?) => {{
79    if $condition {
80      use $crate::__private as __pw_log_crate;
81      $crate::__private::pw_logf_backend!($log_level, $format_string, $($args),*)
82    }
83  }};
84}
85
86/// Emit a debug level log message using `core:fmt` format string semantics if condition
87/// is true.
88///
89/// ```
90/// use log_if::debug_if;
91///
92/// const LOG_FACTS: bool = true;
93/// debug_if!(LOG_FACTS, "Log Fact: The American toy Lincoln Logs were inspired by the {} in {}.",
94///     "Imperial Hotel" as &str, "Tokyo" as &str);
95/// ```
96#[macro_export]
97macro_rules! debug_if {
98  ($condition:expr, $($args:expr),* $(,)?) => {{
99    use $crate::__private as __pw_log_crate;
100    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Debug, $($args),*)
101  }};
102}
103
104/// Emit a debug level log message using `printf` format string semantics if condition
105/// is true.
106///
107/// ```
108/// use log_if::debugf_if;
109///
110/// const LOG_FACTS: bool = true;
111/// debugf_if!(LOG_FACTS, "Log Fact: The American toy Lincoln Logs were inspired by the %s in %s.",
112///     "Imperial Hotel", "Tokyo");
113/// ```
114#[macro_export]
115macro_rules! debugf_if {
116  ($condition:expr, $($args:expr),* $(,)?) => {{
117      use $crate::__private as __pw_log_crate;
118      __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Debug, $($args),*)
119  }};
120}
121
122/// Emit an info level log message using `core:fmt` format string semantics if condition is true.
123///
124/// ```
125/// use log_if::info_if;
126///
127/// const LOG_FACTS: bool = true;
128/// info_if!(
129///     LOG_FACTS,
130///     "Log Fact: The American president Abraham Lincoln (born {:x}) once lived in a log cabin.",
131///     0x1809 as u32);
132/// ```
133#[macro_export]
134macro_rules! info_if {
135  ($condition:expr, $($args:expr),* $(,)?) => {{
136    use $crate::__private as __pw_log_crate;
137    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Info, $($args),*)
138  }};
139}
140
141/// Emit an info level log message using `printf` format string semantics if condition is true.
142///
143/// ```
144/// use log_if::infof_if;
145///
146/// const LOG_FACTS: bool = true;
147/// infof_if!(
148///     LOG_FACTS,
149///     "Log Fact: The American president Abraham Lincoln (born %x) once lived in a log cabin.",
150///     0x1809);
151/// ```
152#[macro_export]
153macro_rules! infof_if {
154  ($condition:expr, $($args:expr),* $(,)?) => {{
155    use $crate::__private as __pw_log_crate;
156    __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Info, $($args),*)
157  }};
158}
159
160/// Emit a warn level log message using `core::fmt` format string semantics if condition is true.
161///
162/// ```
163/// use log_if::warn_if;
164///
165/// const LOG_FACTS: bool = true;
166/// warn_if!(
167///     LOG_FACTS,
168///     "Log Fact: Made from a log, an {} year old dugout canoe is the oldest discovered boat in {}.",
169///     8000 as i32, "Africa" as &str);
170/// ```
171#[macro_export]
172macro_rules! warn_if {
173  ($condition:expr, $($args:expr),* $(,)?) => {{
174    use $crate::__private as __pw_log_crate;
175    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Warn, $($args),*)
176  }};
177}
178
179/// Emit a warn level log message using `printf` format string semantics if condition is true.
180///
181/// ```
182/// use log_if::warnf_if;
183///
184/// const LOG_FACTS: bool = true;
185/// warnf_if!(
186///     LOG_FACTS,
187///     "Log Fact: Made from a log, an %d year old dugout canoe is the oldest discovered boat in %s.",
188///     8000, "Africa");
189/// ```
190#[macro_export]
191macro_rules! warnf_if {
192  ($condition:expr, $($args:expr),* $(,)?) => {{
193    use $crate::__private as __pw_log_crate;
194    __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Warn, $($args),*)
195  }};
196}
197
198/// Emit an error level log message using `core::fmt` format string semantics if
199/// condition is true.
200///
201/// ```
202/// use log_if::error_if;
203///
204/// const LOG_FACTS: bool = true;
205/// error_if!(
206///     LOG_FACTS,
207///     "Log Fact: Before saws were invented, the {} was used prepare logs for use.",
208///     "adze" as &str);
209/// ```
210#[macro_export]
211macro_rules! error_if {
212  ($condition:expr, $($args:expr),* $(,)?) => {{
213    use $crate::__private as __pw_log_crate;
214    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Error, $($args),*)
215  }};
216}
217
218/// Emit an error level log message using `printf` format string semantics if condition
219/// is true.
220///
221/// ```
222/// use log_if::errorf_if;
223///
224/// const LOG_FACTS: bool = true;
225/// errorf_if!(
226///     LOG_FACTS,
227///     "Log Fact: Before saws were invented, the %s was used prepare logs for use.",
228///     "adze");
229/// ```
230#[macro_export]
231macro_rules! errorf_if {
232  ($condition:expr, $($args:expr),* $(,)?) => {{
233    use $crate::__private as __pw_log_crate;
234    __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Error, $($args),*)
235  }};
236}
237
238/// Emit a critical level log message using `core::fmt` format string semantics if condition
239/// is true.
240///
241/// ```
242/// use log_if::critical_if;
243///
244/// const LOG_FACTS: bool = true;
245/// critical_if!(
246///     LOG_FACTS,
247///     "Log Fact: Until the {}th century, all ships' masts were made from a single log.",
248///     19 as u32);
249/// ```
250#[macro_export]
251macro_rules! critical_if {
252  ($condition:expr, $($args:expr),* $(,)?) => {{
253    use $crate::__private as __pw_log_crate;
254    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Critical, $($args),*)
255  }};
256}
257
258/// Emit a critical level log message using `printf` format string semantics if condition
259/// is true.
260///
261/// ```
262/// use log_if::criticalf_if;
263///
264/// const LOG_FACTS: bool = true;
265/// criticalf_if!(
266///     LOG_FACTS,
267///     "Log Fact: Until the %dth century, all ships' masts were made from a single log.",
268///     19);
269/// ```
270#[macro_export]
271macro_rules! criticalf_if {
272  ($condition:expr, $($args:expr),* $(,)?) => {{
273    use $crate::__private as __pw_log_crate;
274    __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Critical, $($args),*)
275  }};
276}
277
278/// Emit a fatal level log message using `core::fmt` format string semantics if
279/// condition is true.
280///
281/// *Note*: `fatal` only emits a log message and does not cause a `panic!()`
282///
283/// ```
284/// use log_if::fatal_if;
285///
286/// const LOG_FACTS: bool = true;
287/// fatal_if!(LOG_FACTS, "Log Fact: All out of log facts! Timber!");
288/// ```
289#[macro_export]
290macro_rules! fatal_if {
291  ($condition:expr, $($args:expr),* $(,)?) => {{
292    use $crate::__private as __pw_log_crate;
293    __pw_log_crate::log_if!($condition, __pw_log_crate::LogLevel::Fatal, $($args),*)
294  }};
295}
296
297/// Emit a fatal level log message using `printf` format string semantics if
298/// condition is true.
299///
300/// *Note*: `fatalf` only emits a log message and does not cause a `panic!()`
301///
302/// ```
303/// use log_if::fatalf_if;
304///
305/// const LOG_FACTS: bool = true;
306/// fatalf_if!(LOG_FACTS, "Log Fact: All out of log facts! Timber!");
307/// ```
308#[macro_export]
309macro_rules! fatalf_if {
310  ($condition:expr, $($args:expr),* $(,)?) => {{
311      use $crate::__private as __pw_log_crate;
312      __pw_log_crate::logf_if!($condition, __pw_log_crate::LogLevel::Fatal, $($args),*)
313  }};
314}