Skip to main content

elfo_core/dumping/
dumper.rs

1use std::sync::Arc;
2
3use crate::Message;
4
5use super::{
6    dump::*,
7    recorder::{self, Recorder},
8};
9
10#[derive(Clone)]
11#[instability::unstable]
12pub struct Dumper {
13    recorder: Option<Arc<dyn Recorder>>,
14}
15
16impl Dumper {
17    #[instability::unstable]
18    pub fn new(class: &'static str) -> Self {
19        Self {
20            recorder: recorder::make_recorder(class),
21        }
22    }
23
24    #[inline]
25    #[instability::unstable]
26    pub fn acquire(&self) -> Option<DumpingPermit<'_>> {
27        let r = self.recorder.as_deref().filter(|r| r.enabled())?;
28        Some(DumpingPermit { recorder: r })
29    }
30
31    pub(crate) fn acquire_m<M: Message>(&self, message: &M) -> Option<DumpingPermit<'_>> {
32        if !message.dumping_allowed() {
33            return None;
34        }
35
36        self.acquire()
37    }
38}
39
40#[must_use]
41#[instability::unstable]
42pub struct DumpingPermit<'a> {
43    recorder: &'a dyn Recorder,
44}
45
46impl DumpingPermit<'_> {
47    #[instability::unstable]
48    pub fn record(self, dump: Dump) {
49        self.recorder.record(dump);
50    }
51}