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

pw_log/
pw_log.rs

1// Copyright 2023 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
15//! `pw_log` is an extensible logging system that can delegate to
16//! pre-existing logging APIs without upstream changes.
17//!
18//! Clients of `pw_log` simply import and use the logging API, and
19//! log invocations will be handled by the provided logging backend.
20//!
21//! This flexibility is accomplished using Pigweed's
22//! [facade pattern](https://pigweed.dev/docs/facades.html),
23//! which uses build-system redirection to forward log invocations to the
24//! configured backend implementation.
25//!
26//! ```
27//! use pw_log::{log, info, LogLevel};
28//!
29//! log!(LogLevel::Info, "Thank you for signing up for Log Facts!");
30//! info!("Log Fact: Logs can be either {}, {}, or {} sawn.",
31//!   "flat" as &str, "quarter" as &str, "rift" as &str);
32//! ```
33//!
34//! Today `printf` style format strings are well supported with Rust
35//! [`core::fmt`]/`println!()` style strings partially supported
36//! ([b/311232607](https://issues.pigweed.dev/issues/311232607)).
37//!
38//! Currently, when using a `stable` toolchain, dynamically typed arguments (i.e.
39//! `{}` style) without type-level format specifiers use `as` casts when needed.
40//! Users with nightly toolchains can enable the `nightly_tait` feature to
41//! remove this restriction.
42//!
43//! TODO: <pwbug.dev/311266298> - Document `pw_log`'s backend API.
44//!
45//! TODO: <pwbug.dev/311232605> - Document how to configure facade backends.
46#![cfg_attr(not(feature = "std"), no_std)]
47#![deny(missing_docs)]
48
49pub use pw_log_backend_api::LogLevel;
50
51// Re-export dependencies of `pw_log` macros to be accessed via `$crate::__private`.
52#[doc(hidden)]
53pub mod __private {
54    pub use pw_log_backend::{pw_log_backend, pw_logf_backend};
55
56    pub use crate::*;
57}
58
59/// Emit a log message using `core::fmt` format string semantics.
60///
61/// `log` takes a [`LogLevel`], a `core::fmt` style format string, and necessary
62/// arguments to that string and emits a log message to the logging backend.
63///
64/// ```
65/// use pw_log::{log, LogLevel};
66///
67/// log!(LogLevel::Info, "Log fact: A {} log has a Janka hardness of {} lbf.",
68///      "Spruce Pine" as &str, 700 as i32);
69/// ```
70///
71/// Multiple string literals can be concatenated using the `PW_FMT_CONCAT` operator.
72///
73/// This is particularly useful when writing wrapper macros that need to prepend
74/// a prefix to a format string.
75///
76/// ```
77/// use pw_log::{log, LogLevel};
78///
79/// macro_rules! my_info_log {
80///     ($format_string:literal $(, $args:expr)* $(,)?) => {
81///         log!(LogLevel::Info, "[MyModule] " PW_FMT_CONCAT $format_string $(, $args)*)
82///     };
83/// }
84///
85/// my_info_log!("The answer is {}.", 42 as i32);
86/// ```
87#[macro_export]
88macro_rules! log {
89  ($log_level:expr, $($format_string:literal)PW_FMT_CONCAT+ $(, $args:expr)* $(,)?) => {{
90    use $crate::__private as __pw_log_crate;
91    $crate::__private::pw_log_backend!($log_level, $($format_string)PW_FMT_CONCAT+, $($args),*)
92  }};
93
94  ($log_level:expr, $format_string:literal $(, $args:expr)* $(,)?) => {{
95    use $crate::__private as __pw_log_crate;
96    $crate::__private::pw_log_backend!($log_level, $format_string, $($args),*)
97  }};
98}
99
100/// Emit a log message using `printf` format string semantics.
101///
102/// `logf` takes a [`LogLevel`], a `printf` style format string, and necessary
103/// arguments to that string and emits a log message to the logging backend.
104///
105/// ```
106/// use pw_log::{logf, LogLevel};
107///
108/// logf!(LogLevel::Info, "Log fact: A %s log has a Janka hardness of %d lbf.",
109///     "Spruce Pine", 700);
110/// ```
111#[macro_export]
112macro_rules! logf {
113  ($log_level:expr, $format_string:literal $(,)?) => {{
114    use $crate::__private as __pw_log_crate;
115    $crate::__private::pw_logf_backend!($log_level, $format_string)
116  }};
117
118  ($log_level:expr, $format_string:literal, $($args:expr),* $(,)?) => {{
119    use $crate::__private as __pw_log_crate;
120    $crate::__private::pw_logf_backend!($log_level, $format_string, $($args),*)
121  }};
122}
123
124/// Deprecated alias for [`logf!`].
125#[macro_export]
126macro_rules! pw_logf {
127  ($($args:expr),* $(,)?) => {{
128    logf!($($args),*)
129  }}
130}
131
132/// Emit a debug level log message using `core:fmt` format string semantics.
133///
134/// ```
135/// use pw_log::debug;
136///
137/// debug!("Log Fact: The American toy Lincoln Logs were inspired by the {} in {}.",
138///     "Imperial Hotel" as &str, "Tokyo" as &str);
139/// ```
140#[macro_export]
141macro_rules! debug {
142  ($($args:tt)*) => {{
143    use $crate::__private as __pw_log_crate;
144    __pw_log_crate::log!(__pw_log_crate::LogLevel::Debug, $($args)*)
145  }};
146}
147
148/// Emit a debug level log message using `printf` format string semantics.
149///
150/// ```
151/// use pw_log::debugf;
152///
153/// debugf!("Log Fact: The American toy Lincoln Logs were inspired by the %s in %s.",
154///     "Imperial Hotel", "Tokyo");
155/// ```
156#[macro_export]
157macro_rules! debugf {
158  ($($args:expr),* $(,)?) => {{
159    use $crate::__private as __pw_log_crate;
160    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Debug, $($args),*)
161  }};
162}
163
164/// Deprecated alias for [`debugf!`].
165#[macro_export]
166macro_rules! pw_log_debugf {
167  ($($args:expr),* $(,)?) => {{
168    debugf!($($args),*)
169  }}
170}
171
172/// Emit an info level log message using `core:fmt` format string semantics.
173///
174/// ```
175/// use pw_log::info;
176///
177/// info!(
178///     "Log Fact: The American president Abraham Lincoln (born {:x}) once lived in a log cabin.",
179///     0x1809 as u32);
180/// ```
181#[macro_export]
182macro_rules! info {
183  ($($args:tt)*) => {{
184    use $crate::__private as __pw_log_crate;
185    __pw_log_crate::log!(__pw_log_crate::LogLevel::Info, $($args)*)
186  }};
187}
188
189/// Emit an info level log message using `printf` format string semantics.
190///
191/// ```
192/// use pw_log::infof;
193///
194/// infof!(
195///     "Log Fact: The American president Abraham Lincoln (born %x) once lived in a log cabin.",
196/// 0x1809);
197/// ```
198#[macro_export]
199macro_rules! infof {
200  ($($args:expr),* $(,)?) => {{
201    use $crate::__private as __pw_log_crate;
202    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Info, $($args),*)
203  }};
204}
205
206/// Deprecated alias for [`infof!`].
207#[macro_export]
208macro_rules! pw_log_infof {
209  ($($args:expr),* $(,)?) => {{
210    infof!($($args),*)
211  }}
212}
213
214/// Emit a warn level log message using `core::fmt` format string semantics.
215///
216/// ```
217/// use pw_log::warn;
218///
219/// warn!(
220///     "Log Fact: Made from a log, an {} year old dugout canoe is the oldest discovered boat in {}.",
221///     8000 as i32, "Africa" as &str);
222/// ```
223#[macro_export]
224macro_rules! warn {
225  ($($args:tt)*) => {{
226    use $crate::__private as __pw_log_crate;
227    __pw_log_crate::log!(__pw_log_crate::LogLevel::Warn, $($args)*)
228  }};
229}
230
231/// Emit a warn level log message using `printf` format string semantics.
232///
233/// ```
234/// use pw_log::warnf;
235///
236/// warnf!(
237///     "Log Fact: Made from a log, an %d year old dugout canoe is the oldest discovered boat in %s.",
238///     8000, "Africa");
239/// ```
240#[macro_export]
241macro_rules! warnf {
242  ($($args:expr),* $(,)?) => {{
243    use $crate::__private as __pw_log_crate;
244    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Warn, $($args),*)
245  }};
246}
247
248/// Deprecated alias for [`warnf!`].
249#[macro_export]
250macro_rules! pw_log_warnf {
251  ($($args:expr),* $(,)?) => {{
252    warnf!($($args),*)
253  }}
254}
255
256/// Emit an error level log message using `core::fmt` format string semantics.
257///
258/// ```
259/// use pw_log::error;
260///
261/// error!(
262///     "Log Fact: Before saws were invented, the {} was used prepare logs for use.",
263///     "adze" as &str);
264/// ```
265#[macro_export]
266macro_rules! error {
267  ($($args:tt)*) => {{
268    use $crate::__private as __pw_log_crate;
269    __pw_log_crate::log!(__pw_log_crate::LogLevel::Error, $($args)*)
270  }};
271}
272
273/// Emit an error level log message using `printf` format string semantics.
274///
275/// ```
276/// use pw_log::errorf;
277///
278/// errorf!(
279///     "Log Fact: Before saws were invented, the %s was used prepare logs for use.",
280///     "adze");
281/// ```
282#[macro_export]
283macro_rules! errorf {
284  ($($args:expr),* $(,)?) => {{
285    use $crate::__private as __pw_log_crate;
286    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Error, $($args),*)
287  }};
288}
289
290/// Deprecated alias for [`errorf!`].
291#[macro_export]
292macro_rules! pw_log_errorf {
293  ($($args:expr),* $(,)?) => {{
294    errorf!($($args),*)
295  }}
296}
297
298/// Emit a critical level log message using `core::fmt` format string semantics.
299///
300/// ```
301/// use pw_log::critical;
302///
303/// critical!(
304///     "Log Fact: Until the {}th century, all ships' masts were made from a single log.",
305///     19 as u32);
306/// ```
307#[macro_export]
308macro_rules! critical {
309  ($($args:tt)*) => {{
310    use $crate::__private as __pw_log_crate;
311    __pw_log_crate::log!(__pw_log_crate::LogLevel::Critical, $($args)*)
312  }};
313}
314
315/// Emit a critical level log message using `printf` format string semantics.
316///
317/// ```
318/// use pw_log::criticalf;
319///
320/// criticalf!(
321///     "Log Fact: Until the %dth century, all ships' masts were made from a single log.",
322///     19);
323/// ```
324#[macro_export]
325macro_rules! criticalf {
326  ($($args:expr),* $(,)?) => {{
327    use $crate::__private as __pw_log_crate;
328    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Critical, $($args),*)
329  }};
330}
331
332/// Deprecated alias for [`criticalf!`].
333#[macro_export]
334macro_rules! pw_log_criticalf {
335  ($($args:expr),* $(,)?) => {{
336    criticalf!($($args),*)
337  }}
338}
339
340/// Emit a fatal level log message using `core::fmt` format string semantics.
341///
342/// *Note*: `fatal` only emits a log message and does not cause a `panic!()`
343///
344/// ```
345/// use pw_log::fatal;
346///
347/// fatal!("Log Fact: All out of log facts! Timber!");
348/// ```
349#[macro_export]
350macro_rules! fatal {
351  ($($args:tt)*) => {{
352    use $crate::__private as __pw_log_crate;
353    __pw_log_crate::log!(__pw_log_crate::LogLevel::Fatal, $($args)*)
354  }};
355}
356
357/// Emit a fatal level log message using `printf` format string semantics.
358///
359/// *Note*: `fatalf` only emits a log message and does not cause a `panic!()`
360///
361/// ```
362/// use pw_log::fatalf;
363///
364/// fatalf!("Log Fact: All out of log facts! Timber!");
365/// ```
366#[macro_export]
367macro_rules! fatalf {
368  ($($args:expr),* $(,)?) => {{
369    use $crate::__private as __pw_log_crate;
370    __pw_log_crate::logf!(__pw_log_crate::LogLevel::Fatal, $($args),*)
371  }};
372}
373
374/// Deprecated alias for [`fatalf!`].
375#[macro_export]
376macro_rules! pw_log_fatalf {
377  ($($args:expr),* $(,)?) => {{
378    fatalf!($($args),*)
379  }}
380}
381
382#[cfg(test)]
383mod tests {
384    // TODO(b/311262163): Add infrastructure for testing behavior of `pw_log` API.
385    // The syntax of that API is verified through doctests.
386}