trace_recorder_parser/streaming/
recorder_data.rs

1use crate::streaming::event::{Event, EventCode, EventId, EventParser};
2use crate::streaming::{EntryTable, Error, HeaderInfo, TimestampInfo};
3use crate::types::{Endianness, Heap, Protocol};
4use std::io::Read;
5use tracing::debug;
6
7/// Encapsulates all of the startup data needed to materialize the events
8#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
9pub struct RecorderData {
10    pub protocol: Protocol,
11    pub header: HeaderInfo,
12    pub timestamp_info: TimestampInfo,
13    pub entry_table: EntryTable,
14    parser: EventParser,
15}
16
17impl RecorderData {
18    pub fn find<R: Read>(r: &mut R) -> Result<Self, Error> {
19        debug!("Finding header info");
20        let header = HeaderInfo::find(r)?;
21
22        Self::read_common(header, r)
23    }
24
25    pub fn read<R: Read>(r: &mut R) -> Result<Self, Error> {
26        debug!("Reading header info");
27        let header = HeaderInfo::read(r)?;
28
29        Self::read_common(header, r)
30    }
31
32    /// Assumes the PSF word (u32) has already been read from the input
33    pub fn read_with_endianness<R: Read>(endianness: Endianness, r: &mut R) -> Result<Self, Error> {
34        debug!("Reading header info");
35        let header = HeaderInfo::read_with_endianness(endianness, r)?;
36
37        Self::read_common(header, r)
38    }
39
40    fn read_common<R: Read>(header: HeaderInfo, r: &mut R) -> Result<Self, Error> {
41        debug!("Reading timestamp info");
42        let timestamp_info = TimestampInfo::read(r, header.endianness, header.format_version)?;
43
44        debug!("Reading entry table");
45        let entry_table = EntryTable::read(r, header.endianness)?;
46
47        let parser = EventParser::new(
48            header.endianness,
49            entry_table.system_heap().unwrap_or_default(),
50        );
51
52        Ok(Self {
53            protocol: Protocol::Streaming,
54            header,
55            timestamp_info,
56            entry_table,
57            parser,
58        })
59    }
60
61    pub fn system_heap(&self) -> &Heap {
62        self.parser.system_heap()
63    }
64
65    pub fn set_custom_printf_event_id(&mut self, custom_printf_event_id: EventId) {
66        self.parser
67            .set_custom_printf_event_id(custom_printf_event_id);
68    }
69
70    pub fn read_event<R: Read>(&mut self, r: &mut R) -> Result<Option<(EventCode, Event)>, Error> {
71        self.parser.next_event(r, &mut self.entry_table)
72    }
73}