Skip to content

Commit 8b77a02

Browse files
authored
Merge pull request #105 from RobertZ2011/fmt-fix
embedded-service: refactor logging macros
2 parents e3d3f0e + 74946d7 commit 8b77a02

File tree

3 files changed

+175
-74
lines changed

3 files changed

+175
-74
lines changed

.github/workflows/check.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ jobs:
173173
# intentionally no target specifier; see https://github.com/jonhoo/rust-ci-conf/pull/4
174174
# --feature-powerset runs for every combination of features
175175
- name: cargo hack
176-
run: cargo hack --feature-powerset check
176+
run: cargo hack --feature-powerset --mutually-exclusive-features=log,defmt check
177177
working-directory: ${{ matrix.workdir }}
178178

179179
deny:
@@ -230,5 +230,7 @@ jobs:
230230
with:
231231
toolchain: ${{ matrix.msrv }}
232232
- name: cargo +${{ matrix.msrv }} check
233-
run: cargo check
233+
run: |
234+
cargo check -F log
235+
cargo check -F defmt
234236
working-directory: ${{ matrix.workdir }}

embedded-service/src/fmt.rs

Lines changed: 170 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,179 @@
11
//! Logging macro implementations and other formating functions
22
3-
/// Logs a trace message using the underlying logger
4-
#[macro_export]
5-
#[collapse_debuginfo(yes)]
6-
macro_rules! trace {
7-
($s:literal $(, $x:expr)* $(,)?) => {
8-
{
9-
#[cfg(feature = "defmt")]
10-
::defmt::trace!($s $(, $x)*);
11-
#[cfg(feature = "log")]
12-
::log::trace!($s $(, $x)*);
13-
#[cfg(not(any(feature = "defmt", feature = "log")))]
14-
let _ = ($( & $x ),*);
15-
}
16-
};
17-
}
3+
#[cfg(all(feature = "log", feature = "defmt", not(doc)))]
4+
compile_error!("features `log` and `defmt` are mutually exclusive");
185

19-
/// Logs a debug message using the underlying logger
20-
#[macro_export]
21-
#[collapse_debuginfo(yes)]
22-
macro_rules! debug {
23-
($s:literal $(, $x:expr)* $(,)?) => {
24-
{
25-
#[cfg(feature = "defmt")]
26-
::defmt::debug!($s $(, $x)*);
27-
#[cfg(feature = "log")]
28-
::log::debug!($s $(, $x)*);
29-
#[cfg(not(any(feature = "defmt", feature = "log")))]
30-
let _ = ($( & $x ),*);
31-
}
32-
};
33-
}
6+
#[cfg(all(not(doc), feature = "defmt"))]
7+
mod defmt {
8+
/// Logs a trace message using the underlying logger
9+
#[macro_export]
10+
#[collapse_debuginfo(yes)]
11+
macro_rules! trace {
12+
($s:literal $(, $x:expr)* $(,)?) => {
13+
{
14+
::defmt::trace!($s $(, $x)*);
15+
}
16+
};
17+
}
18+
19+
/// Logs a debug message using the underlying logger
20+
#[macro_export]
21+
#[collapse_debuginfo(yes)]
22+
macro_rules! debug {
23+
($s:literal $(, $x:expr)* $(,)?) => {
24+
{
25+
::defmt::debug!($s $(, $x)*);
26+
}
27+
};
28+
}
29+
30+
/// Logs an info message using the underlying logger
31+
#[macro_export]
32+
#[collapse_debuginfo(yes)]
33+
macro_rules! info {
34+
($s:literal $(, $x:expr)* $(,)?) => {
35+
{
36+
::defmt::info!($s $(, $x)*);
37+
}
38+
};
39+
}
3440

35-
/// Logs a info message using the underlying logger
36-
#[macro_export]
37-
#[collapse_debuginfo(yes)]
38-
macro_rules! info {
39-
($s:literal $(, $x:expr)* $(,)?) => {
40-
{
41-
#[cfg(feature = "defmt")]
42-
::defmt::info!($s $(, $x)*);
43-
#[cfg(feature = "log")]
44-
::log::info!($s $(, $x)*);
45-
#[cfg(not(any(feature = "defmt", feature = "log")))]
46-
let _ = ($( & $x ),*);
47-
}
48-
};
41+
/// Logs a warning using the underlying logger
42+
#[macro_export]
43+
#[collapse_debuginfo(yes)]
44+
macro_rules! warn {
45+
($s:literal $(, $x:expr)* $(,)?) => {
46+
{
47+
::defmt::warn!($s $(, $x)*);
48+
}
49+
};
50+
}
51+
52+
/// Logs an error using the underlying logger
53+
#[macro_export]
54+
#[collapse_debuginfo(yes)]
55+
macro_rules! error {
56+
($s:literal $(, $x:expr)* $(,)?) => {
57+
{
58+
::defmt::error!($s $(, $x)*);
59+
}
60+
};
61+
}
4962
}
5063

51-
/// Logs a warning using the underlying logger
52-
#[macro_export]
53-
#[collapse_debuginfo(yes)]
54-
macro_rules! warn {
55-
($s:literal $(, $x:expr)* $(,)?) => {
56-
{
57-
#[cfg(feature = "defmt")]
58-
::defmt::warn!($s $(, $x)*);
59-
#[cfg(feature = "log")]
60-
::log::warn!($s $(, $x)*);
61-
#[cfg(not(any(feature = "defmt", feature = "log")))]
62-
let _ = ($( & $x ),*);
63-
}
64-
};
64+
#[cfg(all(not(doc), feature = "log"))]
65+
mod log {
66+
/// Logs a trace message using the underlying logger
67+
#[macro_export]
68+
#[collapse_debuginfo(yes)]
69+
macro_rules! trace {
70+
($s:literal $(, $x:expr)* $(,)?) => {
71+
{
72+
::log::trace!($s $(, $x)*);
73+
}
74+
};
75+
}
76+
77+
/// Logs a debug message using the underlying logger
78+
#[macro_export]
79+
#[collapse_debuginfo(yes)]
80+
macro_rules! debug {
81+
($s:literal $(, $x:expr)* $(,)?) => {
82+
{
83+
::log::debug!($s $(, $x)*);
84+
}
85+
};
86+
}
87+
88+
/// Logs an info message using the underlying logger
89+
#[macro_export]
90+
#[collapse_debuginfo(yes)]
91+
macro_rules! info {
92+
($s:literal $(, $x:expr)* $(,)?) => {
93+
{
94+
::log::info!($s $(, $x)*);
95+
}
96+
};
97+
}
98+
99+
/// Logs a warning using the underlying logger
100+
#[macro_export]
101+
#[collapse_debuginfo(yes)]
102+
macro_rules! warn {
103+
($s:literal $(, $x:expr)* $(,)?) => {
104+
{
105+
::log::warn!($s $(, $x)*);
106+
}
107+
};
108+
}
109+
110+
/// Logs an error using the underlying logger
111+
#[macro_export]
112+
#[collapse_debuginfo(yes)]
113+
macro_rules! error {
114+
($s:literal $(, $x:expr)* $(,)?) => {
115+
{
116+
::log::error!($s $(, $x)*);
117+
}
118+
};
119+
}
65120
}
66121

67-
/// Logs an error using the underlying logger
68-
#[macro_export]
69-
#[collapse_debuginfo(yes)]
70-
macro_rules! error {
71-
($s:literal $(, $x:expr)* $(,)?) => {
72-
{
73-
#[cfg(feature = "defmt")]
74-
::defmt::error!($s $(, $x)*);
75-
#[cfg(feature = "log")]
76-
::log::error!($s $(, $x)*);
77-
#[cfg(not(any(feature = "defmt", feature = "log")))]
78-
let _ = ($( & $x ),*);
79-
}
80-
};
122+
// Provide this implementation for `cargo doc`
123+
#[cfg(any(doc, not(any(feature = "defmt", feature = "log"))))]
124+
mod none {
125+
/// Logs a trace message using the underlying logger
126+
#[macro_export]
127+
#[collapse_debuginfo(yes)]
128+
macro_rules! trace {
129+
($s:literal $(, $x:expr)* $(,)?) => {
130+
{
131+
let _ = ($( & $x ),*);
132+
}
133+
};
134+
}
135+
136+
/// Logs a debug message using the underlying logger
137+
#[macro_export]
138+
#[collapse_debuginfo(yes)]
139+
macro_rules! debug {
140+
($s:literal $(, $x:expr)* $(,)?) => {
141+
{
142+
let _ = ($( & $x ),*);
143+
}
144+
};
145+
}
146+
147+
/// Logs an info message using the underlying logger
148+
#[macro_export]
149+
#[collapse_debuginfo(yes)]
150+
macro_rules! info {
151+
($s:literal $(, $x:expr)* $(,)?) => {
152+
{
153+
let _ = ($( & $x ),*);
154+
}
155+
};
156+
}
157+
158+
/// Logs a warning using the underlying logger
159+
#[macro_export]
160+
#[collapse_debuginfo(yes)]
161+
macro_rules! warn {
162+
($s:literal $(, $x:expr)* $(,)?) => {
163+
{
164+
let _ = ($( & $x ),*);
165+
}
166+
};
167+
}
168+
169+
/// Logs an error using the underlying logger
170+
#[macro_export]
171+
#[collapse_debuginfo(yes)]
172+
macro_rules! error {
173+
($s:literal $(, $x:expr)* $(,)?) => {
174+
{
175+
let _ = ($( & $x ),*);
176+
}
177+
};
178+
}
81179
}

power-button-service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ rust-version = "1.83"
99

1010
[dependencies]
1111
defmt = { version = "0.3", optional = true }
12+
log = { version = "0.4.14", optional = true }
1213
embassy-time = { git = "https://github.com/embassy-rs/embassy" }
1314
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
1415
embedded-hal-async = { version = "1.0" }

0 commit comments

Comments
 (0)