use std::cell::RefCell;
use crate::signal::Sig;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code, reason = "each platform and later unit feeds a subset")]
pub(crate) enum Via {
Pidfd,
Pid,
Handle,
Identity,
}
pub(crate) type Entry = (u32, Sig, Via);
pub(crate) type IdentityEntry = (u32, i32);
thread_local! {
static LOG: RefCell<Option<Vec<Entry>>> = const { RefCell::new(None) };
static BY_IDENTITY: RefCell<Option<Vec<IdentityEntry>>> = const { RefCell::new(None) };
}
#[cfg(unix)]
pub(crate) fn record_by_identity(pid: u32, signo: i32) {
BY_IDENTITY.with(|log| {
if let Some(entries) = log.borrow_mut().as_mut() {
entries.push((pid, signo));
}
});
}
pub(crate) fn record(pid: u32, sig: Sig, via: Via) {
LOG.with(|log| {
if let Some(entries) = log.borrow_mut().as_mut() {
entries.push((pid, sig, via));
}
});
}
pub(crate) struct Capture(());
impl Capture {
pub(crate) fn start() -> Capture {
LOG.with(|log| {
let mut log = log.borrow_mut();
assert!(log.is_none(), "a send-log capture is already live on this thread");
*log = Some(Vec::new());
});
BY_IDENTITY.with(|log| *log.borrow_mut() = Some(Vec::new()));
Capture(())
}
pub(crate) fn entries(&self) -> Vec<Entry> {
LOG.with(|log| log.borrow().clone().expect("a live capture has a log"))
}
}
impl Capture {
#[cfg(target_os = "macos")]
pub(crate) fn by_identity(&self) -> Vec<IdentityEntry> {
BY_IDENTITY.with(|log| log.borrow().clone().expect("a live capture has a log"))
}
}
impl Drop for Capture {
fn drop(&mut self) {
LOG.with(|log| *log.borrow_mut() = None);
BY_IDENTITY.with(|log| *log.borrow_mut() = None);
}
}