ifc_lite_core/parser/report_sink.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! The one host-installed destination every scan diagnostic in this crate
6//! writes to (issue #3395's oversized-id refusals, and the malformed-record
7//! stop this module now shares the channel with).
8//!
9//! One sink, not one per diagnostic kind: [`super::oversized_ids`] and
10//! [`super::malformed_records`] both report a scan that came back short of
11//! what the file declares, for a different reason each, but a host that
12//! wants to see one wants to see the other the same way — installing two
13//! sinks (or forgetting to install the second) would silently drop half the
14//! warnings on whichever target adds a diagnostic later. `set_report_sink`
15//! stays exported from [`super::oversized_ids`] (its original, still public
16//! name) so no caller-visible API changes.
17
18use std::sync::atomic::{AtomicU64, Ordering};
19use std::sync::OnceLock;
20
21/// Set once, because a swappable sink invites a reset race between two loads
22/// on different threads and nothing here needs one.
23pub(super) static REPORT_SINK: OnceLock<fn(&str)> = OnceLock::new();
24
25/// Every diagnostic emitted so far in this process, from any thread.
26static EMITTED: AtomicU64 = AtomicU64::new(0);
27
28/// The number of scan diagnostics emitted so far in this process. A result
29/// cache must not store a pass that emitted one: a replay never scans, so the
30/// host would silently lose the "this file may be incomplete" warning.
31pub fn scan_report_events() -> u64 {
32 EMITTED.load(Ordering::SeqCst)
33}
34
35/// Route a diagnostic `message` to the installed sink, or stderr by default.
36pub(super) fn emit(message: &str) {
37 EMITTED.fetch_add(1, Ordering::SeqCst);
38 match REPORT_SINK.get() {
39 Some(sink) => sink(&format!("[ifc-lite] {message}")),
40 None => eprintln!("[ifc-lite] {message}"),
41 }
42}