byteflow-actors 0.9.28

Embeddable flow runtime: M:N scheduler, Atomic Hop (Message+Cap), supervisor — use byteflow::
Documentation
//! Host-side scheduler diagnostics on stderr, gated by `BYTEFLOW_LOG`.
//!
//! # Why this is separate from the `print` native
//!
//! Actor-visible logging (`CallNative print`) writes to **stdout** and is part
//! of the program's observable behaviour — samples and demos use it to show
//! envelopes crossing the mailbox. Scheduler diagnostics (spawn / send /
//! park / finish) are an **operator** concern: they must not pollute stdout
//! when an embedder pipes Flow output, and they must stay off by default
//! so a production run is silent unless asked.
//!
//! ```text
//! BYTEFLOW_LOG unset / 0 / off / false  → silent
//! BYTEFLOW_LOG=1 / info / true          → spawn, send, receive, finish
//! BYTEFLOW_LOG=debug / 2                → also park, deliver queued/handoff
//! ```
//!
//! # `tracing` integration
//!
//! With the optional `tracing` feature, every diagnostic is also emitted as a
//! [`tracing`](https://docs.rs/tracing) event with target `byteflow` (INFO or
//! DEBUG), independent of `BYTEFLOW_LOG`. Filter it like any other target,
//! e.g. `RUST_LOG=byteflow=debug` with `tracing-subscriber`. The default build
//! does not depend on `tracing`.
//!
//! # Cost when disabled
//!
//! Call sites pass `format_args!(..)`, not `format!(..)`: nothing is
//! formatted or allocated unless stderr, a [`LogSink`] or a `tracing`
//! subscriber will actually receive the line. Message delivery logs on every
//! hop, so this keeps the hot path allocation-free.
//!
//! # Lazy init
//!
//! The level is read from the environment once (first call) and cached in
//! atomics. That avoids re-parsing `std::env` on every send in a hot worker
//! loop. Tests can override via [`set_level_for_tests`].

// Docs ratchet: public items here predate `missing_docs`. Delete this
// allow once they are documented; new files get the lint by default.
#![allow(missing_docs)]

use std::sync::atomic::{AtomicU8, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

const OFF: u8 = 0;
const INFO: u8 = 1;
const DEBUG: u8 = 2;

static LEVEL: AtomicU8 = AtomicU8::new(OFF);
static INIT: AtomicU8 = AtomicU8::new(0);

fn level() -> u8 {
    if INIT.load(Ordering::Acquire) == 0 {
        let parsed = match std::env::var("BYTEFLOW_LOG") {
            Ok(v) => {
                let v = v.trim().to_ascii_lowercase();
                match v.as_str() {
                    "" | "0" | "off" | "false" | "no" => OFF,
                    "debug" | "trace" | "2" => DEBUG,
                    // "1", "info", "true", "yes", or any other non-empty token
                    _ => INFO,
                }
            }
            Err(_) => OFF,
        };
        LEVEL.store(parsed, Ordering::Release);
        INIT.store(1, Ordering::Release);
    }
    LEVEL.load(Ordering::Acquire)
}

fn stamp_ms() -> u64 {
    // Logging must never panic: a broken clock just prints epoch-ish 0.
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(d) => d.as_millis() as u64,
        Err(_) => 0,
    }
}

/// Override the cached level (tests only). `0` off, `1` info, `2` debug.
pub fn set_level_for_tests(level: u8) {
    LEVEL.store(level.min(DEBUG), Ordering::Release);
    INIT.store(1, Ordering::Release);
}

/// INFO-level line when [`BYTEFLOW_LOG`](crate::log) is at least `info`.
#[inline]
pub fn info(msg: impl std::fmt::Display) {
    forward(None, INFO, "INFO ", msg);
}

/// DEBUG-level line when [`BYTEFLOW_LOG`](crate::log) is `debug`.
#[inline]
pub fn debug(msg: impl std::fmt::Display) {
    forward(None, DEBUG, "DEBUG", msg);
}

/// Is a `tracing` subscriber interested in this level for target `byteflow`?
#[cfg(feature = "tracing")]
fn tracing_enabled(min: u8) -> bool {
    if min >= DEBUG {
        tracing::enabled!(target: "byteflow", tracing::Level::DEBUG)
    } else {
        tracing::enabled!(target: "byteflow", tracing::Level::INFO)
    }
}

#[cfg(not(feature = "tracing"))]
#[inline(always)]
fn tracing_enabled(_min: u8) -> bool {
    false
}

#[cfg(feature = "tracing")]
fn emit_tracing(min: u8, msg: &dyn std::fmt::Display) {
    if min >= DEBUG {
        tracing::debug!(target: "byteflow", "{msg}");
    } else {
        tracing::info!(target: "byteflow", "{msg}");
    }
}

#[cfg(not(feature = "tracing"))]
#[inline(always)]
fn emit_tracing(_min: u8, _msg: &dyn std::fmt::Display) {}

/// A collector refused or dropped one diagnostic line.
///
/// The runtime ignores this. The flow keeps its original outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LogRecordError;

impl std::fmt::Display for LogRecordError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("log sink rejected the line")
    }
}

impl std::error::Error for LogRecordError {}

/// Receives the diagnostic line that would be written to stderr.
///
/// `try_record` runs on the worker that emitted the event. It must return
/// promptly and must not panic. [`Err`] is ignored: a collector failure does
/// not fail the flow. There is no queue inside the runtime.
pub trait LogSink: Send + Sync + std::fmt::Debug + 'static {
    fn try_record(&self, line: &str) -> Result<(), LogRecordError>;
}

pub(crate) fn info_sink(sink: Option<&std::sync::Arc<dyn LogSink>>, msg: impl std::fmt::Display) {
    forward(sink, INFO, "INFO ", msg);
}

pub(crate) fn debug_sink(sink: Option<&std::sync::Arc<dyn LogSink>>, msg: impl std::fmt::Display) {
    forward(sink, DEBUG, "DEBUG", msg);
}

fn forward(
    sink: Option<&std::sync::Arc<dyn LogSink>>,
    min: u8,
    label: &str,
    msg: impl std::fmt::Display,
) {
    let show = level() >= min;
    let traced = tracing_enabled(min);
    if !show && sink.is_none() && !traced {
        // Common case: nobody listens, so `msg` is never formatted.
        return;
    }
    if traced {
        emit_tracing(min, &msg);
    }
    if !show && sink.is_none() {
        return;
    }
    let line = format!("[{}] byteflow {label} {msg}", stamp_ms());
    if show {
        eprintln!("{line}");
    }
    if let Some(sink) = sink {
        let _ = sink.try_record(&line);
    }
}