Skip to content

Commit 665d25f

Browse files
committed
Add logging macros
1 parent 4305d2f commit 665d25f

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

crates/flipperzero/src/furi/log/metadata.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ impl Level {
198198
Level::ERROR => "ERROR",
199199
}
200200
}
201+
202+
pub(crate) fn to_furi(self) -> sys::FuriLogLevel {
203+
self.0 as u8
204+
}
201205
}
202206

203207
impl fmt::Display for Level {
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,166 @@
11
//! Furi Logging system.
22
33
mod metadata;
4+
pub use metadata::{Level, LevelFilter};
5+
6+
/// The standard logging macro.
7+
///
8+
/// This macro will generically log with the specified `Level` and C-style format argument
9+
/// list.
10+
///
11+
/// # Examples
12+
///
13+
/// ```
14+
/// use flipperzero::{error, furi::log::Level};
15+
///
16+
/// # fn main() {
17+
/// let error_code = 42;
18+
/// log!(Level::ERROR, "Failed to handle the florp: %d", error_code);
19+
/// log!(target: "events", Level::INFO, "Finished the documentation!");
20+
/// # }
21+
/// ```
22+
#[macro_export]
23+
macro_rules! log {
24+
(target: $target:expr, $lvl:expr, $msg:expr $(, $arg:expr)*) => ({
25+
if $lvl <= $crate::furi::log::LevelFilter::current() {
26+
unsafe {
27+
$crate::__macro_support::__sys::furi_log_print_format(
28+
$crate::__macro_support::__level_to_furi($lvl),
29+
$crate::__macro_support::__sys::c_string!($target),
30+
$crate::__macro_support::__sys::c_string!($msg),
31+
$($arg,)*
32+
);
33+
}
34+
}
35+
});
36+
37+
($lvl:expr, $msg:expr $(, $arg:expr)*) => (
38+
$crate::log!(target: module_path!(), $lvl, $msg $(, $arg)*)
39+
);
40+
}
41+
42+
/// Logs a message at the error level.
43+
///
44+
/// # Examples
45+
///
46+
/// ```
47+
/// use flipperzero::error;
48+
///
49+
/// # fn main() {
50+
/// let error_code = 42;
51+
/// let name = "Flipper";
52+
///
53+
/// error!("Failed to handle the florp: %d", error_code);
54+
/// error!(target: "events", "Missed birthday party for %s", name);
55+
/// # }
56+
/// ```
57+
#[macro_export]
58+
macro_rules! error {
59+
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
60+
$crate::log!(target: $target, $crate::furi::log::Level::ERROR, $msg $(, $arg)*)
61+
);
62+
63+
($msg:expr $(, $arg:expr)*) => (
64+
$crate::log!($crate::furi::log::Level::ERROR, $msg $(, $arg)*)
65+
);
66+
}
67+
68+
/// Logs a message at the warn level.
69+
///
70+
/// # Examples
71+
///
72+
/// ```
73+
/// use flipperzero::warn;
74+
///
75+
/// # fn main() {
76+
/// let name = "Flipper";
77+
///
78+
/// warn!("Event almost started!");
79+
/// warn!(target: "events", "About to miss the birthday party for %s", name);
80+
/// # }
81+
/// ```
82+
#[macro_export]
83+
macro_rules! warn {
84+
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
85+
$crate::log!(target: $target, $crate::furi::log::Level::WARN, $msg $(, $arg)*)
86+
);
87+
88+
($msg:expr $(, $arg:expr)*) => (
89+
$crate::log!($crate::furi::log::Level::WARN, $msg $(, $arg)*)
90+
);
91+
}
92+
93+
/// Logs a message at the info level.
94+
///
95+
/// # Examples
96+
///
97+
/// ```
98+
/// use flipperzero::info;
99+
///
100+
/// # fn main() {
101+
/// let name = "Flipper";
102+
///
103+
/// info!("It's %s's birthday today!", name);
104+
/// info!(target: "events", "Birthday party today: %s", name);
105+
/// # }
106+
/// ```
107+
#[macro_export]
108+
macro_rules! info {
109+
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
110+
$crate::log!(target: $target, $crate::furi::log::Level::INFO, $msg $(, $arg)*)
111+
);
112+
113+
($msg:expr $(, $arg:expr)*) => (
114+
$crate::log!($crate::furi::log::Level::INFO, $msg $(, $arg)*)
115+
);
116+
}
117+
118+
/// Logs a message at the debug level.
119+
///
120+
/// # Examples
121+
///
122+
/// ```
123+
/// use flipperzero::debug;
124+
///
125+
/// # fn main() {
126+
/// let name = "Flipper";
127+
///
128+
/// debug!("Creating %d event", 1);
129+
/// debug!(target: "events", "New event created: birthday party for %s", name);
130+
/// # }
131+
/// ```
132+
#[macro_export]
133+
macro_rules! debug {
134+
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
135+
$crate::log!(target: $target, $crate::furi::log::Level::DEBUG, $msg $(, $arg)*)
136+
);
137+
138+
($msg:expr $(, $arg:expr)*) => (
139+
$crate::log!($crate::furi::log::Level::DEBUG, $msg $(, $arg)*)
140+
);
141+
}
142+
143+
/// Logs a message at the trace level.
144+
///
145+
/// # Examples
146+
///
147+
/// ```
148+
/// use flipperzero::trace;
149+
///
150+
/// # fn main() {
151+
/// let name = "Flipper";
152+
///
153+
/// trace!("About to show how the target field works");
154+
/// trace!(target: "events", "Scanning for events involving %s", name);
155+
/// # }
156+
/// ```
157+
#[macro_export]
158+
macro_rules! trace {
159+
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
160+
$crate::log!(target: $target, $crate::furi::log::Level::TRACE, $msg $(, $arg)*)
161+
);
162+
163+
($msg:expr $(, $arg:expr)*) => (
164+
$crate::log!($crate::furi::log::Level::TRACE, $msg $(, $arg)*)
165+
);
166+
}

crates/flipperzero/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,26 @@ pub mod dolphin;
1010
pub mod furi;
1111
pub mod gui;
1212
pub mod macros;
13+
14+
#[doc(hidden)]
15+
pub mod __macro_support {
16+
pub use crate::furi::log::Level;
17+
18+
/// ⚠️ WARNING: This is *not* a stable API! ⚠️
19+
///
20+
/// This module, and all code contained in the `__macro_support` module, is a
21+
/// *private* API of `flipperzero`. It is exposed publicly because it is used by the
22+
/// `flipperzero` macros, but it is not part of the stable versioned API. Breaking
23+
/// changes to this module may occur in small-numbered versions without warning.
24+
pub use flipperzero_sys as __sys;
25+
26+
/// ⚠️ WARNING: This is *not* a stable API! ⚠️
27+
///
28+
/// This function, and all code contained in the `__macro_support` module, is a
29+
/// *private* API of `flipperzero`. It is exposed publicly because it is used by the
30+
/// `flipperzero` macros, but it is not part of the stable versioned API. Breaking
31+
/// changes to this module may occur in small-numbered versions without warning.
32+
pub fn __level_to_furi(level: Level) -> __sys::FuriLogLevel {
33+
level.to_furi()
34+
}
35+
}

0 commit comments

Comments
 (0)