1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use super::event_type::EventType;
18 use std::fmt;
19 
20 /// Represents a single debug event within the Debug Store system.
21 ///
22 /// It contains all the necessary information for a debug event.
23 #[derive(Clone)]
24 pub struct Event {
25     /// The unique identifier for this event.
26     pub id: u64,
27     /// The optional name of the event.
28     pub name: Option<String>,
29     /// The system uptime when the event occurred.
30     pub timestamp: i64,
31     /// The type of the event.
32     pub event_type: EventType,
33     /// Additional data associated with the event, stored in the given order as key-value pairs.
34     data: Vec<(String, String)>,
35 }
36 
37 impl Event {
38     /// Constructs a new `Event`.
39     ///
40     /// - `id`: The unique identifier for the event.
41     /// - `name`: An optional name for the event.
42     /// - `timestamp`: The system uptime when the event occurred.
43     /// - `event_type`: The type of the event.
44     /// - `data`: Additional data for the event, represented as ordered key-value pairs.
new( id: u64, name: Option<String>, timestamp: i64, event_type: EventType, data: Vec<(String, String)>, ) -> Self45     pub fn new(
46         id: u64,
47         name: Option<String>,
48         timestamp: i64,
49         event_type: EventType,
50         data: Vec<(String, String)>,
51     ) -> Self {
52         Self { id, name, timestamp, event_type, data }
53     }
54 }
55 
56 impl fmt::Display for Event {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result57     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58         write!(f, "ID:{},C:{},T:{}", self.id, self.event_type, self.timestamp)?;
59 
60         if let Some(ref name) = self.name {
61             write!(f, ",N:{}", name)?;
62         }
63 
64         if !self.data.is_empty() {
65             let data_str =
66                 self.data.iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<_>>().join(";");
67             write!(f, ",D:{}", data_str)?;
68         }
69 
70         Ok(())
71     }
72 }
73