pw_log_backend_printf/lib.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` backend that calls `libc`'s `printf` to emit log messages. This
16//! module is useful when you have a mixed C/C++ and Rust code base and want a
17//! simple logging system that leverages an existing `printf` implementation.
18//!
19//! *Note*: This uses FFI to call `printf`. This has two implications:
20//! 1. C/C++ macro processing is not done. If a system's `printf` relies on
21//! macros, this backend will likely need to be forked to make work.
22//! 2. FFI calls use `unsafe`. Attempts are made to use `printf` in sound ways
23//! such as bounding the length of strings explicitly but this is still an
24//! off-ramp from Rust's safety guarantees.
25//!
26//! Varargs marshaling for call to printf is handled through a type expansion
27//! of a series of traits. It is documented in the [`varargs`] module.
28//!
29//! TODO: <pwbug.dev/311232605> - Document how to configure facade backends.
30#![deny(missing_docs)]
31
32pub mod varargs;
33
34// Re-export dependences of backend proc macro to be accessed via `$crate::__private`.
35#[doc(hidden)]
36pub mod __private {
37 use core::ffi::{c_int, c_uchar};
38
39 pub use pw_bytes::concat_static_strs;
40 pub use pw_format_core::{PrintfHexFormatter, PrintfUpperHexFormatter};
41 use pw_log_backend_api::LogLevel;
42 pub use pw_log_backend_printf_macro::{_pw_log_backend, _pw_logf_backend};
43
44 pub use crate::varargs::{Arguments, VarArgs};
45
46 pub const fn log_level_tag(level: LogLevel) -> &'static str {
47 match level {
48 LogLevel::Debug => "DBG\0",
49 LogLevel::Info => "INF\0",
50 LogLevel::Warn => "WRN\0",
51 LogLevel::Error => "ERR\0",
52 LogLevel::Critical => "CRT\0",
53 LogLevel::Fatal => "FTL\0",
54 }
55 }
56
57 macro_rules! extend_args {
58 ($head:ty; $next:ty $(,$rest:ty)* $(,)?) => {
59 extend_args!(<$head as VarArgs>::OneMore<$next>; $($rest,)*)
60 };
61 ($head:ty;) => {
62 $head
63 };
64 }
65
66 /// The printf uses its own formatter trait because it needs strings to
67 /// resolve to `%.*s` instead of `%s`.
68 ///
69 /// The default [`PrintfHexFormatter`] and [`PrintfUpperHexFormatter`] are
70 /// used since they are not supported by strings.
71 pub trait PrintfFormatter {
72 /// The format specifier for this type.
73 const FORMAT_ARG: &'static str;
74 }
75
76 /// A helper to declare a [`PrintfFormatter`] trait for a given type.
77 macro_rules! declare_formatter {
78 ($ty:ty, $specifier:literal) => {
79 impl PrintfFormatter for $ty {
80 const FORMAT_ARG: &'static str = $specifier;
81 }
82 };
83 }
84
85 declare_formatter!(i32, "d");
86 declare_formatter!(u32, "u");
87 declare_formatter!(&str, ".*s");
88
89 /// A helper to declare an [`Argument<T>`] trait for a given type.
90 ///
91 /// Useful for cases where `Argument::push_args()` appends a single
92 /// argument of type `T`.
93 macro_rules! declare_simple_argument {
94 ($ty:ty) => {
95 impl Arguments<$ty> for $ty {
96 type PushArg<Head: VarArgs> = Head::OneMore<$ty>;
97 fn push_arg<Head: VarArgs>(head: Head, arg: &$ty) -> Self::PushArg<Head> {
98 // Try expanding `CHECK` which should fail if we've exceeded
99 // 12 arguments in our args tuple.
100 let _ = Self::PushArg::<Head>::CHECK;
101 head.append(*arg)
102 }
103 }
104 };
105 }
106
107 declare_simple_argument!(i32);
108 declare_simple_argument!(u32);
109 declare_simple_argument!(char);
110
111 // &str needs a more complex implementation of [`Argument<T>`] since it needs
112 // to append two arguments.
113 impl Arguments<&str> for &str {
114 type PushArg<Head: VarArgs> = extend_args!(Head; c_int, *const c_uchar);
115 fn push_arg<Head: VarArgs>(head: Head, arg: &&str) -> Self::PushArg<Head> {
116 // Try expanding `CHECK` which should fail if we've exceeded 12
117 // arguments in our args tuple.
118 #[allow(clippy::let_unit_value)]
119 let _ = Self::PushArg::<Head>::CHECK;
120 let arg = *arg;
121 head.append(arg.len() as c_int).append(arg.as_ptr().cast())
122 }
123 }
124}
125
126/// Implements the `pw_log` backend api.
127#[macro_export]
128macro_rules! pw_log_backend {
129 ($log_level:expr, $format_string:literal $(, $args:expr)* $(,)?) => {{
130 use $crate::__private as __pw_log_backend_crate;
131 $crate::__private::_pw_log_backend!($log_level, $format_string, $($args),*)
132 }};
133}
134
135/// Implements the `pw_log` backend api.
136#[macro_export]
137macro_rules! pw_logf_backend {
138 ($log_level:expr, $format_string:literal $(, $args:expr)* $(,)?) => {{
139 use $crate::__private as __pw_log_backend_crate;
140 $crate::__private::_pw_logf_backend!($log_level, $format_string, $($args),*)
141 }};
142}
143
144#[cfg(test)]
145mod tests {
146 use core::ffi::c_int;
147
148 use super::__private::*;
149
150 #[test]
151 fn pushed_args_produce_correct_tuple() {
152 let string = "test";
153 let args = ();
154 let args = <&str as Arguments<&str>>::push_arg(args, &(string as &str));
155 let args = <u32 as Arguments<u32>>::push_arg(args, &2u32);
156 assert_eq!(args, (string.len() as c_int, string.as_ptr().cast(), 2u32));
157 }
158}