Skip to content

Commit 4a5c79d

Browse files
committed
Update documentation for Level and LevelFilter
1 parent 665d25f commit 4a5c79d

File tree

1 file changed

+24
-93
lines changed

1 file changed

+24
-93
lines changed

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

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{cmp, fmt, str::FromStr};
77

88
use flipperzero_sys as sys;
99

10-
/// Describes the level of verbosity of a span or event.
10+
/// Describes the level of verbosity of a log message.
1111
///
1212
/// # Comparing Levels
1313
///
@@ -19,7 +19,7 @@ use flipperzero_sys as sys;
1919
///
2020
/// For example:
2121
/// ```
22-
/// use tracing_core::Level;
22+
/// use flipperzero::furi::log::Level;
2323
///
2424
/// assert!(Level::TRACE > Level::DEBUG);
2525
/// assert!(Level::ERROR < Level::WARN);
@@ -30,25 +30,25 @@ use flipperzero_sys as sys;
3030
/// # Filtering
3131
///
3232
/// `Level`s are typically used to implement filtering that determines which
33-
/// spans and events are enabled. Depending on the use case, more or less
33+
/// log messages are enabled. Depending on the use case, more or less
3434
/// verbose diagnostics may be desired. For example, when running in
35-
/// development, [`DEBUG`]-level traces may be enabled by default. When running in
36-
/// production, only [`INFO`]-level and lower traces might be enabled. Libraries
35+
/// development, [`DEBUG`]-level logs may be enabled by default. When running in
36+
/// production, only [`INFO`]-level and lower logs might be enabled. Libraries
3737
/// may include very verbose diagnostics at the [`DEBUG`] and/or [`TRACE`] levels.
38-
/// Applications using those libraries typically chose to ignore those traces. However, when
38+
/// Applications using those libraries typically chose to ignore those logs. However, when
3939
/// debugging an issue involving said libraries, it may be useful to temporarily
40-
/// enable the more verbose traces.
40+
/// enable the more verbose logs.
4141
///
42-
/// The [`LevelFilter`] type is provided to enable filtering traces by
42+
/// The [`LevelFilter`] type is provided to enable filtering logs by
4343
/// verbosity. `Level`s can be compared against [`LevelFilter`]s, and
4444
/// [`LevelFilter`] has a variant for each `Level`, which compares analogously
4545
/// to that level. In addition, [`LevelFilter`] adds a [`LevelFilter::OFF`]
4646
/// variant, which is considered "less verbose" than every other `Level`. This is
47-
/// intended to allow filters to completely disable tracing in a particular context.
47+
/// intended to allow filters to completely disable logging in a particular context.
4848
///
4949
/// For example:
5050
/// ```
51-
/// use tracing_core::{Level, LevelFilter};
51+
/// use flipperzero::furi::log::{Level, LevelFilter};
5252
///
5353
/// assert!(LevelFilter::OFF < Level::TRACE);
5454
/// assert!(LevelFilter::TRACE > Level::DEBUG);
@@ -59,83 +59,15 @@ use flipperzero_sys as sys;
5959
///
6060
/// ## Examples
6161
///
62-
/// Below is a simple example of how a [collector] could implement filtering through
63-
/// a [`LevelFilter`]. When a span or event is recorded, the [`Collect::enabled`] method
64-
/// compares the span or event's `Level` against the configured [`LevelFilter`].
65-
/// The optional [`Collect::max_level_hint`] method can also be implemented to allow spans
66-
/// and events above a maximum verbosity level to be skipped more efficiently,
67-
/// often improving performance in short-lived programs.
68-
///
69-
/// ```
70-
/// use tracing_core::{span, Event, Level, LevelFilter, Collect, Metadata};
71-
/// # use tracing_core::span::{Id, Record, Current};
72-
///
73-
/// #[derive(Debug)]
74-
/// pub struct MyCollector {
75-
/// /// The most verbose level that this collector will enable.
76-
/// max_level: LevelFilter,
77-
///
78-
/// // ...
79-
/// }
80-
///
81-
/// impl MyCollector {
82-
/// /// Returns a new `MyCollector` which will record spans and events up to
83-
/// /// `max_level`.
84-
/// pub fn with_max_level(max_level: LevelFilter) -> Self {
85-
/// Self {
86-
/// max_level,
87-
/// // ...
88-
/// }
89-
/// }
90-
/// }
91-
/// impl Collect for MyCollector {
92-
/// fn enabled(&self, meta: &Metadata<'_>) -> bool {
93-
/// // A span or event is enabled if it is at or below the configured
94-
/// // maximum level.
95-
/// meta.level() <= &self.max_level
96-
/// }
97-
///
98-
/// // This optional method returns the most verbose level that this
99-
/// // collector will enable. Although implementing this method is not
100-
/// // *required*, it permits additional optimizations when it is provided,
101-
/// // allowing spans and events above the max level to be skipped
102-
/// // more efficiently.
103-
/// fn max_level_hint(&self) -> Option<LevelFilter> {
104-
/// Some(self.max_level)
105-
/// }
106-
///
107-
/// // Implement the rest of the collector...
108-
/// fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
109-
/// // ...
110-
/// # drop(span); Id::from_u64(1)
111-
/// }
112-
113-
/// fn event(&self, event: &Event<'_>) {
114-
/// // ...
115-
/// # drop(event);
116-
/// }
117-
///
118-
/// // ...
119-
/// # fn enter(&self, _: &Id) {}
120-
/// # fn exit(&self, _: &Id) {}
121-
/// # fn record(&self, _: &Id, _: &Record<'_>) {}
122-
/// # fn record_follows_from(&self, _: &Id, _: &Id) {}
123-
/// # fn current_span(&self) -> Current { Current::unknown() }
124-
/// }
125-
/// ```
126-
///
127-
/// It is worth noting that the `tracing-subscriber` crate provides [additional
128-
/// APIs][envfilter] for performing more sophisticated filtering, such as
129-
/// enabling different levels based on which module or crate a span or event is
130-
/// recorded in.
62+
/// `Level` should generally be used with the [`log`] macro via its associated
63+
/// constants. You can also use the helper macros like [`warn`] directly without
64+
/// needing to specify a `Level`.
13165
///
13266
/// [`DEBUG`]: Level::DEBUG
13367
/// [`INFO`]: Level::INFO
13468
/// [`TRACE`]: Level::TRACE
135-
/// [`Collect::enabled`]: crate::collect::Collect::enabled
136-
/// [`Collect::max_level_hint`]: crate::collect::Collect::max_level_hint
137-
/// [collector]: crate::collect::Collect
138-
/// [envfilter]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html
69+
/// [`log`]: crate::log
70+
/// [`warn`]: crate::warn
13971
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
14072
pub struct Level(LevelInner);
14173

@@ -147,7 +79,7 @@ pub struct Level(LevelInner);
14779
/// details.
14880
///
14981
/// Note that this is essentially identical to the `Level` type, but with the
150-
/// addition of an [`OFF`] level that completely disables all trace
82+
/// addition of an [`OFF`] level that completely disables all logging
15183
/// instrumentation.
15284
///
15385
/// See the documentation for the [`Level`] type to see how `Level`s
@@ -308,7 +240,7 @@ impl LevelFilter {
308240
/// Designates very low priority, often extremely verbose, information.
309241
pub const TRACE: LevelFilter = LevelFilter::from_level(Level::TRACE);
310242

311-
/// Returns a `LevelFilter` that enables spans and events with verbosity up
243+
/// Returns a `LevelFilter` that enables log messages with verbosity up
312244
/// to and including `level`.
313245
pub const fn from_level(level: Level) -> Self {
314246
Self(match level.0 {
@@ -336,22 +268,21 @@ impl LevelFilter {
336268
}
337269
}
338270

339-
/// Returns a `LevelFilter` that matches the most verbose [`Level`] that any
340-
/// currently active [collector] will enable.
271+
/// Returns a `LevelFilter` that matches the most verbose [`Level`] that the
272+
/// Furi Logging system will enable.
341273
///
342-
/// User code should treat this as a *hint*. If a given span or event has a
274+
/// User code should treat this as a *hint*. If a given log message has a
343275
/// level *higher* than the returned `LevelFilter`, it will not be enabled.
344-
/// However, if the level is less than or equal to this value, the span or
345-
/// event is *not* guaranteed to be enabled; the collector will still
346-
/// filter each callsite individually.
276+
/// However, if the level is less than or equal to this value, the log
277+
/// message is *not* guaranteed to be enabled; the Furi Logging system may
278+
/// perform additional filtering.
347279
///
348-
/// Therefore, comparing a given span or event's level to the returned
280+
/// Therefore, comparing a given log message's level to the returned
349281
/// `LevelFilter` **can** be used for determining if something is
350282
/// *disabled*, but **should not** be used for determining if something is
351283
/// *enabled*.
352284
///
353285
/// [`Level`]: super::Level
354-
/// [collector]: super::Collect
355286
#[inline(always)]
356287
pub fn current() -> Self {
357288
match unsafe { sys::furi_log_get_level() } {

0 commit comments

Comments
 (0)