mln-rs-engine 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! `test/src/mln/test/fixture_log_observer.hpp`'s `FixtureLog`, over the
//! `tracing` crate and for the calling thread.

use std::fmt::Write as _;
use std::sync::Arc;

use parking_lot::Mutex;
use tracing::field::{Field, Visit};
use tracing::subscriber::DefaultGuard;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::Layer;
use tracing_subscriber::layer::{Context, SubscriberExt as _};

/// A caught record: its level, target and rendered line, and whether a
/// `count` matched it.
type Records = Arc<Mutex<Vec<((Level, String, String), bool)>>>;

/// Renders an event as its message followed by each other field as
/// ` name=value`.
#[derive(Default)]
struct Message {
    message: String,
    fields: String,
}

impl Message {
    fn line(self) -> String {
        self.message + &self.fields
    }
}

impl Visit for Message {
    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        if field.name() == "message" {
            self.message = format!("{value:?}");
        } else {
            let _ = write!(self.fields, " {}={value:?}", field.name());
        }
    }

    fn record_str(&mut self, field: &Field, value: &str) {
        if field.name() == "message" {
            self.message = value.to_owned();
        } else {
            let _ = write!(self.fields, " {}={value}", field.name());
        }
    }
}

/// Keeps the library's records of Info and above.
struct Catcher(Records);

impl<S: Subscriber> Layer<S> for Catcher {
    fn on_event(&self, event: &Event<'_>, _: Context<'_, S>) {
        let meta = event.metadata();
        if !meta.target().starts_with("mln_") || *meta.level() > Level::INFO {
            return;
        }
        let mut message = Message::default();
        event.record(&mut message);
        self.0.lock().push(((*meta.level(), meta.target().to_owned(), message.line()), false));
    }
}

/// Captures the calling thread's log records while it lives.
pub(crate) struct FixtureLog {
    records: Records,
    _guard: DefaultGuard,
}

impl FixtureLog {
    pub(crate) fn new() -> Self {
        let records = Records::default();
        let subscriber = tracing_subscriber::registry().with(Catcher(Arc::clone(&records)));
        FixtureLog { records, _guard: tracing::subscriber::set_default(subscriber) }
    }

    /// Counts the matching records, and marks them checked.
    pub(crate) fn count(&self, level: Level, target: &str, msg: &str) -> usize {
        let mut count = 0;
        for ((l, t, m), checked) in self.records.lock().iter_mut() {
            if *l == level && t == target && m == msg {
                *checked = true;
                count += 1;
            }
        }
        count
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.records.lock().is_empty()
    }

    /// The number of records no `count` matched.
    pub(crate) fn unchecked_count(&self) -> usize {
        self.records.lock().iter().filter(|(_, checked)| !checked).count()
    }
}