use core::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u16)]
pub enum DiagnosticCode {
Runtime = 1,
Registration = 2,
ExcelCall = 3,
ThunkFailure = 4,
ReturnFailure = 5,
ReleaseFailure = 6,
OwnershipInvariant = 7,
SinkPanic = 8,
Dispatcher = 9,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum DiagnosticSeverity {
Debug,
Info,
Warn,
Error,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DiagnosticEvent {
pub sequence: u64,
pub correlation_id: u64,
pub code: DiagnosticCode,
pub severity: DiagnosticSeverity,
pub excel_code: i32,
}
impl DiagnosticEvent {
pub const fn new(code: DiagnosticCode, severity: DiagnosticSeverity, excel_code: i32) -> Self {
Self {
sequence: 0,
correlation_id: 0,
code,
severity,
excel_code,
}
}
pub const fn with_correlation(mut self, correlation_id: u64) -> Self {
self.correlation_id = correlation_id;
self
}
}
const CAPACITY: usize = 64;
static NEXT: AtomicU64 = AtomicU64::new(1);
static WRITTEN: AtomicUsize = AtomicUsize::new(0);
static EMITTING: AtomicBool = AtomicBool::new(false);
static RING: std::sync::Mutex<[Option<DiagnosticEvent>; CAPACITY]> =
std::sync::Mutex::new([None; CAPACITY]);
static USER_SINK: std::sync::Mutex<Option<&'static dyn DiagnosticSink>> =
std::sync::Mutex::new(None);
pub trait DiagnosticSink: Send + Sync {
fn record(&self, event: DiagnosticEvent);
}
pub fn set_user_sink(sink: Option<&'static dyn DiagnosticSink>) {
if let Ok(mut slot) = USER_SINK.try_lock() {
*slot = sink;
}
}
pub fn emit(mut event: DiagnosticEvent) {
if EMITTING.swap(true, Ordering::Acquire) {
return;
}
event.sequence = NEXT.fetch_add(1, Ordering::Relaxed);
if event.correlation_id == 0 {
event.correlation_id = event.sequence;
}
if let Ok(mut ring) = RING.try_lock() {
let index = WRITTEN.fetch_add(1, Ordering::Relaxed) % CAPACITY;
ring[index] = Some(event);
}
if let Ok(sink) = USER_SINK.try_lock() {
if let Some(sink) = *sink {
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| sink.record(event)));
}
}
EMITTING.store(false, Ordering::Release);
}
pub fn snapshot() -> Vec<DiagnosticEvent> {
RING.lock()
.ok()
.map(|ring| ring.iter().flatten().copied().collect())
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn saturation_is_bounded_and_preserves_excel_codes() {
let _serial = TEST_LOCK.lock().unwrap();
for code in 0..(CAPACITY as i32 + 4) {
emit(DiagnosticEvent::new(
DiagnosticCode::ExcelCall,
DiagnosticSeverity::Error,
code,
));
}
let events = snapshot();
assert!(events.len() <= CAPACITY);
assert!(
events
.iter()
.any(|event| event.excel_code == CAPACITY as i32 + 3)
);
}
struct PanickingSink;
impl DiagnosticSink for PanickingSink {
fn record(&self, _: DiagnosticEvent) {
panic!("test sink panic");
}
}
static PANICKING: PanickingSink = PanickingSink;
#[test]
fn sink_panic_and_reentrancy_do_not_escape() {
let _serial = TEST_LOCK.lock().unwrap();
set_user_sink(Some(&PANICKING));
emit(DiagnosticEvent::new(
DiagnosticCode::ThunkFailure,
DiagnosticSeverity::Error,
17,
));
set_user_sink(None);
assert!(snapshot().iter().any(|event| event.excel_code == 17));
}
}