Skip to main content

micromegas_tracing/logs/
log_events.rs

1use crate::{
2    property_set::PropertySetId,
3    static_string_ref::{BlockStringRef, StaticStringRef},
4    string_id::{DynStringId, StringId},
5};
6use micromegas_transit::{
7    DynString, UserDefinedType, prelude::*, read_advance_string, read_consume_pod,
8};
9use std::sync::Arc;
10
11use super::LogMetadata;
12
13// Ensure we're on a 64-bit platform since we store pointers as u64
14const _: () = assert!(std::mem::size_of::<usize>() == 8);
15
16#[derive(Debug, TransitReflect)]
17pub struct LogStaticStrEvent {
18    pub desc: &'static LogMetadata<'static>,
19    pub time: i64,
20}
21
22impl InProcSerialize for LogStaticStrEvent {}
23
24/// A log with a property set attached, either interned or built from dynamic values.
25/// Its message is `desc.fmt_str`.
26#[derive(Debug, TransitReflect)]
27pub struct TaggedLogStaticStrEvent {
28    pub desc: &'static LogMetadata<'static>,
29    #[transit(reference)]
30    pub properties: PropertySetId,
31    pub time: i64,
32}
33
34impl InProcSerialize for TaggedLogStaticStrEvent {}
35
36/// A log whose message is `desc.fmt_str` rendered with `properties`.
37///
38/// It has the layout of `TaggedLogStaticStrEvent` but a distinct type name: a stored static
39/// event means "verbatim" and must never be rendered again.
40#[derive(Debug, TransitReflect)]
41pub struct TaggedLogTemplateEvent {
42    pub desc: &'static LogMetadata<'static>,
43    #[transit(reference)]
44    pub properties: PropertySetId,
45    pub time: i64,
46}
47
48impl InProcSerialize for TaggedLogTemplateEvent {}
49
50#[derive(Debug)]
51pub struct LogStringEvent {
52    pub desc: &'static LogMetadata<'static>,
53    pub time: i64,
54    pub msg: DynString,
55}
56
57impl InProcSerialize for LogStringEvent {
58    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
59
60    fn get_value_size(&self) -> Option<u32> {
61        Some(
62            std::mem::size_of::<usize>() as u32 //desc reference
63                + std::mem::size_of::<i64>() as u32 //time
64                + self.msg.get_value_size().unwrap(), //dyn string
65        )
66    }
67
68    fn write_value(&self, buffer: &mut Vec<u8>) {
69        let desc_id = self.desc as *const _ as usize;
70        write_any(buffer, &desc_id);
71        write_any(buffer, &self.time);
72        self.msg.write_value(buffer);
73    }
74
75    unsafe fn read_value(mut window: &[u8]) -> Self {
76        // it does no good to parse this object when looking for dependencies, we should skip this code
77        let desc_id: usize = read_consume_pod(&mut window);
78        let desc = unsafe { &*(desc_id as *const LogMetadata) };
79        let time: i64 = read_consume_pod(&mut window);
80        let msg = DynString(read_advance_string(&mut window).unwrap());
81        assert_eq!(window.len(), 0);
82        Self { desc, time, msg }
83    }
84}
85
86//todo: change this interface to make clear that there are two serialization
87// strategies: pod and custom
88impl Reflect for LogStringEvent {
89    fn reflect() -> UserDefinedType {
90        lazy_static::lazy_static! {
91            static ref TYPE_NAME: Arc<String> = Arc::new("LogStringEventV2".into());
92        }
93        UserDefinedType {
94            name: TYPE_NAME.clone(),
95            size: 0,
96            members: vec![],
97            is_reference: false,
98            secondary_udts: vec![],
99        }
100    }
101}
102
103#[derive(Debug, TransitReflect)]
104pub struct LogStaticStrInteropEvent {
105    pub time: i64,
106    pub level: u32,
107    pub target: StringId,
108    pub msg: StringId,
109}
110
111impl InProcSerialize for LogStaticStrInteropEvent {}
112
113#[derive(Debug, TransitReflect)]
114pub struct LogStaticStrInteropEventV2 {
115    pub time: i64,
116    pub level: u32,
117    pub target: DynStringId,
118    pub msg: StringId,
119}
120
121impl InProcSerialize for LogStaticStrInteropEventV2 {}
122
123#[derive(Debug)]
124pub struct LogStringInteropEvent {
125    pub time: i64,
126    pub level: u8,
127    // The id of a string record in the block's queue, laid out as a `StaticStringRef` for
128    // unreal compatibility.
129    pub target: BlockStringRef,
130    pub msg: DynString,
131}
132
133impl InProcSerialize for LogStringInteropEvent {
134    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
135
136    fn get_value_size(&self) -> Option<u32> {
137        Some(
138            std::mem::size_of::<i64>() as u32 //time
139                + std::mem::size_of::<u8>() as u32 //level
140                + std::mem::size_of::<BlockStringRef>() as u32 //target
141                + self.msg.get_value_size().unwrap(), //message
142        )
143    }
144
145    fn write_value(&self, buffer: &mut Vec<u8>) {
146        write_any(buffer, &self.time);
147        write_any(buffer, &self.level);
148        write_any(buffer, &self.target);
149        self.msg.write_value(buffer);
150    }
151
152    unsafe fn read_value(mut window: &[u8]) -> Self {
153        let time: i64 = read_consume_pod(&mut window);
154        let level: u8 = read_consume_pod(&mut window);
155        let target: BlockStringRef = read_consume_pod(&mut window);
156        let msg = DynString(read_advance_string(&mut window).unwrap());
157        Self {
158            time,
159            level,
160            target,
161            msg,
162        }
163    }
164}
165
166//todo: change this interface to make clear that there are two serialization
167// strategies: pod and custom
168impl Reflect for LogStringInteropEvent {
169    fn reflect() -> UserDefinedType {
170        lazy_static::lazy_static! {
171            static ref TYPE_NAME: Arc<String> = Arc::new("LogStringInteropEventV3".into());
172        }
173        UserDefinedType {
174            name: TYPE_NAME.clone(),
175            size: 0,
176            members: vec![],
177            is_reference: false,
178            secondary_udts: vec![StaticStringRef::reflect()],
179        }
180    }
181}
182
183/// Interop log with properties. Mirrors the event of the same name in the Unreal
184/// instrumentation so that one analytics parser reads both.
185#[derive(Debug)]
186pub struct TaggedLogInteropEvent {
187    pub time: i64,
188    pub level: u8,
189    pub target: BlockStringRef,
190    pub properties: PropertySetId,
191    pub msg: DynString,
192}
193
194impl InProcSerialize for TaggedLogInteropEvent {
195    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
196
197    fn get_value_size(&self) -> Option<u32> {
198        Some(
199            std::mem::size_of::<i64>() as u32 // time
200                + std::mem::size_of::<u8>() as u32 // level
201                + std::mem::size_of::<BlockStringRef>() as u32 // target
202                + std::mem::size_of::<u64>() as u32 // properties ptr
203                + self.msg.get_value_size().unwrap(), // message
204        )
205    }
206
207    fn write_value(&self, buffer: &mut Vec<u8>) {
208        write_any(buffer, &self.time);
209        write_any(buffer, &self.level);
210        write_any(buffer, &self.target);
211        write_any(buffer, &self.properties.0);
212        self.msg.write_value(buffer);
213    }
214
215    unsafe fn read_value(mut window: &[u8]) -> Self {
216        let time: i64 = read_consume_pod(&mut window);
217        let level: u8 = read_consume_pod(&mut window);
218        let target: BlockStringRef = read_consume_pod(&mut window);
219        let properties_id: u64 = read_consume_pod(&mut window);
220        let msg = DynString(read_advance_string(&mut window).unwrap());
221        Self {
222            time,
223            level,
224            target,
225            properties: PropertySetId(properties_id),
226            msg,
227        }
228    }
229}
230
231impl Reflect for TaggedLogInteropEvent {
232    fn reflect() -> UserDefinedType {
233        lazy_static::lazy_static! {
234            static ref TYPE_NAME: Arc<String> = Arc::new("TaggedLogInteropEvent".into());
235        }
236        UserDefinedType {
237            name: TYPE_NAME.clone(),
238            size: 0,
239            members: vec![],
240            is_reference: false,
241            secondary_udts: vec![StaticStringRef::reflect()],
242        }
243    }
244}
245
246#[derive(Debug, TransitReflect)]
247pub struct LogMetadataRecord {
248    pub id: u64,
249    pub fmt_str: *const u8,
250    pub target: *const u8,
251    pub module_path: *const u8,
252    pub file: *const u8,
253    pub line: u32,
254    pub level: u32,
255}
256
257impl InProcSerialize for LogMetadataRecord {}
258
259/// A log with a property set attached, either interned or built from dynamic values.
260#[derive(Debug)]
261pub struct TaggedLogString {
262    pub desc: &'static LogMetadata<'static>,
263    pub properties: PropertySetId,
264    pub time: i64,
265    pub msg: DynString,
266}
267
268impl Reflect for TaggedLogString {
269    fn reflect() -> UserDefinedType {
270        lazy_static::lazy_static! {
271            static ref TYPE_NAME: Arc<String> = Arc::new("TaggedLogString".into());
272        }
273        UserDefinedType {
274            name: TYPE_NAME.clone(),
275            size: 0,
276            members: vec![],
277            is_reference: false,
278            secondary_udts: vec![],
279        }
280    }
281}
282
283impl InProcSerialize for TaggedLogString {
284    const IN_PROC_SIZE: InProcSize = InProcSize::Dynamic;
285
286    fn get_value_size(&self) -> Option<u32> {
287        Some(
288            std::mem::size_of::<u64>() as u32 // desc ptr
289            + std::mem::size_of::<u64>() as u32 // properties ptr
290            + std::mem::size_of::<i64>() as u32 // time
291                + self.msg.get_value_size().unwrap(), // message
292        )
293    }
294
295    fn write_value(&self, buffer: &mut Vec<u8>) {
296        // Exposing the provenance is what allows read_value to rebuild a valid reference from the
297        // bare address stored in the buffer.
298        let desc_id = (self.desc as *const LogMetadata<'static>).expose_provenance() as u64;
299        write_any(buffer, &desc_id);
300        write_any(buffer, &self.properties.0);
301        write_any(buffer, &self.time);
302        self.msg.write_value(buffer);
303    }
304
305    unsafe fn read_value(mut window: &[u8]) -> Self {
306        let desc_id: u64 = read_consume_pod(&mut window);
307        let properties_id: u64 = read_consume_pod(&mut window);
308        let time: i64 = read_consume_pod(&mut window);
309        let msg = DynString(read_advance_string(&mut window).unwrap());
310        Self {
311            desc: unsafe {
312                &*std::ptr::with_exposed_provenance::<LogMetadata<'static>>(desc_id as usize)
313            },
314            properties: PropertySetId(properties_id),
315            time,
316            msg,
317        }
318    }
319}