nros_platform_cffi/log.rs
1//! The `LogSink` that speaks the platform ABI.
2//!
3//! ## Why it lives here and not in the facade
4//!
5//! `nros-log` is a facade: `LogSink` + `init` exist so delivery is PLUGGABLE.
6//! `PlatformSink` is one bridge among possible many — it is the only one that
7//! needs `nros_platform_log_write`, which is a LINK-TIME requirement on the
8//! final binary.
9//!
10//! While it lived in `nros-log`, "does this binary need the platform log ABI?"
11//! was answerable only by a Cargo feature, and that cannot work:
12//! `nros-platform-cffi` and `nros-rmw-bridge` enable `nros-node/rmw-cffi`
13//! unconditionally, so workspace feature unification turns any forwarded gate
14//! back ON for every member of the build. A feature is a property of the BUILD.
15//!
16//! A DEPENDENCY is a property of the binary. Here, the question answers itself:
17//! a binary that links this crate has the ABI (that is what this crate is), and
18//! one that does not cannot accidentally acquire the requirement. The extern is
19//! also declared exactly once now, in `generated.rs` — bindgen output from
20//! `<nros/platform.h>`, the single source of truth — rather than a second time
21//! by hand in the facade.
22//!
23//! ## Using it
24//!
25//! ```ignore
26//! nros_log::init(nros_platform_cffi::log::default_sinks());
27//! // or, equivalently:
28//! nros_platform_cffi::log::init_default();
29//! ```
30//!
31//! Apps wanting fan-out (e.g. platform + `/rosout`) compose their own
32//! `&'static [&dyn LogSink]` and pass it to `nros_log::init`, exactly as before.
33
34use nros_log::{FormatBuffer, LogSink, Record};
35
36/// Forwards every record to the platform port's `nros_platform_log_write`.
37///
38/// Zero-sized. Threading + ISR safety inherit from the linked
39/// `nros-platform-<rtos>` impl.
40pub struct PlatformSink;
41
42impl LogSink for PlatformSink {
43 fn log(&self, record: &Record<'_>) {
44 // Issue #503 — prefix the rendered line with the record's monotonic
45 // stamp as `[sssss.uuuuuu]`. Done by message rewrite because the
46 // `nros_platform_log_write` ABI has no timestamp parameter and widening
47 // it would touch every platform port; the prefix is additive, not a
48 // re-format of the caller's text.
49 //
50 // Keyed on the STAMP, not on a Cargo feature. In `nros-log` this was
51 // `#[cfg(feature = "platform-clock")]`, which cannot follow the sink
52 // across a crate boundary without inventing a second feature that means
53 // the same thing. A record with no stamp (`0`) is already the "no clock"
54 // case, so the runtime check subsumes the cfg — and one branch on a u64
55 // is not what a log path is spending its time on.
56 if record.timestamp_ns != 0 {
57 use core::fmt::Write as _;
58 let secs = record.timestamp_ns / 1_000_000_000;
59 let micros = (record.timestamp_ns % 1_000_000_000) / 1_000;
60 let mut buf = FormatBuffer::new();
61 let _ = core::write!(buf, "[{secs:5}.{micros:06}] {}", record.message);
62 emit(record.severity.as_u8(), record.logger_name, buf.as_str());
63 return;
64 }
65 emit(record.severity.as_u8(), record.logger_name, record.message);
66 }
67
68 fn flush(&self) {
69 // SAFETY: no args, no preconditions.
70 unsafe { crate::generated::nros_platform_log_flush() };
71 }
72
73 /// Issue 2762 -- a property of the linked PORT, stated by the feature
74 /// that port's build enables (`reentrant-log-write`).
75 fn reentrant(&self) -> bool {
76 cfg!(feature = "reentrant-log-write")
77 }
78}
79
80fn emit(severity: u8, name: &str, msg: &str) {
81 let name = name.as_bytes();
82 let msg = msg.as_bytes();
83 // SAFETY: pointers come from `&str` references that outlive the call;
84 // lengths match.
85 unsafe {
86 crate::generated::nros_platform_log_write(
87 severity,
88 name.as_ptr(),
89 name.len(),
90 msg.as_ptr(),
91 msg.len(),
92 );
93 }
94}
95
96static PLATFORM_SINK: PlatformSink = PlatformSink;
97
98/// The default sink list: just [`PlatformSink`].
99#[must_use]
100pub fn default_sinks() -> &'static [&'static dyn LogSink] {
101 static SINKS: &[&dyn LogSink] = &[&PLATFORM_SINK];
102 SINKS
103}
104
105/// Install [`default_sinks`] as the global sink list.
106///
107/// Convenience for the common boot funnel; equivalent to
108/// `nros_log::init(default_sinks())`, and it drains anything
109/// `nros_log::early` held before this point.
110pub fn init_default() {
111 nros_log::init(default_sinks());
112}
113
114/// Route the `log` FACADE (`log::info!` in an application body) into the same
115/// sink list [`init_default`] installs, so one funnel carries both.
116///
117/// A Zephyr Rust image used `zephyr::set_logger()` for this, and with
118/// `CONFIG_LOG` in full mode that logger exists only under `CONFIG_RUST_ALLOC`
119/// — the zephyr-lang-rust allocator that put a SECOND, unpriced heap under
120/// every Zephyr Rust image (host glibc on an XRCE native_sim image). That
121/// allocator is now refused; this is what replaces the logger it carried.
122/// Idempotent; a second `log::Log` already installed is left in place, which is
123/// the `log` crate's own rule.
124#[cfg(feature = "log-compat")]
125pub fn install_log_crate_bridge() {
126 let _ = nros_log::log_compat::install_log_crate_bridge();
127}