Struct Diagnostics

pub struct Diagnostics { /* private fields */ }

Implementations§

§

impl Diagnostics

pub fn new(capacity: usize, minimum: Severity) -> Diagnostics

pub fn set_minimum(&mut self, minimum: Severity)

pub fn record( &mut self, tick: u64, severity: Severity, code: &str, message: &str, ) -> Result<bool, DiagnosticError>

False means filtered out or disabled (zero capacity); invalid text is rejected.

pub fn record_with_context( &mut self, tick: u64, severity: Severity, code: &str, message: &str, context: DiagnosticContext<'_>, ) -> Result<bool, DiagnosticInputError>

Record a diagnostic with bounded structured context and detailed errors. Validation occurs even when filtered out or disabled, before any mutation. Consecutive retained records deduplicate only when severity, code, message and context match and tick is nondecreasing. Filtered/rejected calls do not interrupt that sequence. Repeat and eviction counts saturate at u64::MAX. See the module example for source coordinates and suggested fixes.

pub fn iter(&self) -> impl Iterator<Item = &Diagnostic>

pub fn iter_with_context( &self, ) -> impl Iterator<Item = (&Diagnostic, DiagnosticContext<'_>)>

Retained records with their context, in insertion order (not sorted by tick).

pub fn evicted(&self) -> u64

pub fn clear(&mut self)

pub fn summary(&self) -> DiagnosticSummary

Count retained records and occurrences in one pass, without allocation.

Per-severity and total occurrences saturate independently at u64::MAX. Evicted records are excluded from retained counts. The eviction counter counts records removed since Self::clear, not lost occurrences. Changing the minimum affects future recording only; old severities still appear in the summary. The returned value remains a snapshot after edits.

use ferravex_engine::diagnostics::{DiagnosticCounts, Diagnostics, Severity};
let mut diagnostics = Diagnostics::new(2, Severity::Warning);
for tick in 1..=3 {
    diagnostics.record(tick, Severity::Warning, "frame.slow", "Over budget")?;
}
let summary = diagnostics.summary();
assert_eq!(summary.retained, DiagnosticCounts { records: 1, occurrences: 3 });
assert_eq!(summary.for_severity(Severity::Warning), summary.retained);
assert_eq!(summary.for_severity(Severity::Error), DiagnosticCounts::default());
assert_eq!(summary.evicted, 0);

pub fn write_ndjson<W>(&self, output: &mut W) -> Result<(), Error>
where W: Write + ?Sized,

Append an NDJSON snapshot: one line per retained record, then a summary.

The stream format is versioned independently of Self::write_json. Each diagnostic line has fixed fields stream_version (1), type ("diagnostic"), and record (the unchanged version 1 snapshot record). The final line has stream_version (1), type ("summary"), capacity, minimum, retained, evicted, and by_severity, in that order. retained and each by_severity value contain records and occurrences; all four severities appear in trace/info/warning/error order, even if zero. Counters have the semantics of Self::summary. An empty or disabled collector still writes the summary line.

Every object ends with a single LF, including the summary. Text escaping, exact decimal integers and insertion order match Self::write_json. Export neither allocates a snapshot, drains records nor flushes the writer. This is a complete retained snapshot, not an incremental event subscription: repeated calls include existing records and their updated repeat counts. A summary line marks the end of each snapshot when appending multiple calls. On failure the writer may contain complete lines and an incomplete last line; the first error is propagated immediately, without collector mutation.

use ferravex_engine::diagnostics::{Diagnostics, Severity};
let mut diagnostics = Diagnostics::new(2, Severity::Info);
diagnostics.record(9, Severity::Error, "scene.invalid", "First line\nSecond line")?;
let mut text = String::new();
diagnostics.write_ndjson(&mut text)?;
assert_eq!(text.lines().count(), 2); // Diagnostic followed by summary.
assert!(text.contains("First line\\nSecond line"));
assert!(text.ends_with('\n'));

pub fn to_ndjson(&self) -> String

Allocate the NDJSON snapshot documented by Self::write_ndjson.

use ferravex_engine::diagnostics::{Diagnostics, Severity};
let diagnostics = Diagnostics::new(0, Severity::Info);
let text = diagnostics.to_ndjson();
assert_eq!(text.lines().count(), 1); // Empty collectors still have counters.
assert!(text.contains("\"type\":\"summary\""));

pub fn ndjson(&self) -> DiagnosticsNdjson<'_>

Borrowing NDJSON display adapter; no intermediate snapshot allocation. This also streams to std::io::Write through its write! support.

use std::io::Write;
use ferravex_engine::diagnostics::{Diagnostics, Severity};
let mut diagnostics = Diagnostics::new(2, Severity::Info);
diagnostics.record(1, Severity::Info, "scene.ready", "Loaded")?;
let mut bytes = Vec::new(); // A file or socket writer also works.
write!(&mut bytes, "{}", diagnostics.ndjson())?;
assert_eq!(bytes, diagnostics.to_ndjson().as_bytes());

pub fn write_json<W>(&self, output: &mut W) -> Result<(), Error>
where W: Write + ?Sized,

Stream a deterministic, compact JSON snapshot to a text writer.

Schema version 1 has fixed field order: schema_version, capacity, minimum, record_count, evicted, records. Records contain first_tick, last_tick, severity, code, message, repeats, context; context contains source (null or {file,line,column}), operation and help (strings or null). Severity labels are lowercase. repeats includes the first occurrence; evicted counts removed records, not occurrences. Self::clear resets both records and the eviction count.

Records retain insertion order, even if ticks go backwards. Integer values are emitted exactly in decimal; consumers needing all 64 bits must use a lossless JSON integer reader. Unicode is preserved; quotes, backslashes, control characters and U+2028/U+2029 are escaped. Output has no newline. No snapshot allocation or mutation occurs. On writer failure, the error is propagated and the writer may contain a partial document.

use ferravex_engine::diagnostics::{Diagnostics, Severity};
let mut diagnostics = Diagnostics::new(4, Severity::Info);
diagnostics.record(1, Severity::Warning, "asset.missing", "Missing player mesh")?;
let mut buffer = String::new();
diagnostics.write_json(&mut buffer)?;
assert_eq!(buffer, diagnostics.to_json());

pub fn to_json(&self) -> String

Allocate a JSON snapshot in the schema documented by Self::write_json.

pub fn json(&self) -> DiagnosticsJson<'_>

A borrowing JSON fmt::Display adapter; see the module usage example.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.