1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use core::mem::{size_of};
use core::cmp::min;
use core::default::Default;
use alloc::vec::Vec;
use alloc::string::String;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum LogSeverity {
Trace,
Info,
Warn,
Error,
Fatal,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum LogDataChunkKey {
LogSessionBegin,
LogSessionEnd,
TextLog,
LineNumber,
FileName,
FunctionName,
ModuleName,
ThreadName,
LogPacketDropCount,
UserSystemClock,
ProcessName,
}
pub const LOG_PACKET_FLAG_HEAD: u8 = 1 << 0;
pub const LOG_PACKET_FLAG_TAIL: u8 = 1 << 1;
pub const LOG_PACKET_FLAG_LITTLE_ENDIAN: u8 = 1 << 2;
pub const fn is_head_flag(flag: u8) -> bool {
(flag & LOG_PACKET_FLAG_HEAD) != 0
}
pub const fn is_tail_flag(flag: u8) -> bool {
(flag & LOG_PACKET_FLAG_TAIL) != 0
}
pub const fn is_little_endian_flag(flag: u8) -> bool {
(flag & LOG_PACKET_FLAG_LITTLE_ENDIAN) != 0
}
pub const LOG_BINARY_HEADER_MAGIC: u32 = 0x70687068;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct LogBinaryHeader {
pub magic: u32,
pub version: u32
}
impl LogBinaryHeader {
pub const fn empty() -> Self {
Self { magic: LOG_BINARY_HEADER_MAGIC, version: 0 }
}
pub const fn new(magic: u32, version: u32) -> Self {
Self { magic: magic, version: version }
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct LogPacketHeader {
pub process_id: u64,
pub thread_id: u64,
pub flags: u8,
pub pad: u8,
pub severity: LogSeverity,
pub verbosity: bool,
pub payload_size: u32,
}
impl LogPacketHeader {
pub fn new() -> Self {
Self {
process_id: 0,
thread_id: 0,
flags: 0,
pad: 0,
severity: LogSeverity::Trace,
verbosity: false,
payload_size: 0
}
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct LogDataChunkHeader {
pub key: LogDataChunkKey,
pub len: u8
}
impl LogDataChunkHeader {
pub fn new() -> Self {
Self { key: LogDataChunkKey::LogSessionBegin, len: 0 }
}
pub fn from(key: LogDataChunkKey, len: u8) -> Self {
Self { key: key, len: len }
}
}
pub trait PlainChunkType: Copy + Default + PartialEq {}
fn push_to_vec<T: PlainChunkType>(vec: &mut Vec<u8>, t: T) {
let t_ptr = &t as *const T as *const u8;
for i in 0..size_of::<T>() {
unsafe {
let cur_t_ptr = t_ptr.offset(i as isize);
vec.push(*cur_t_ptr);
}
}
}
pub trait ChunkType {
fn get_len(&self) -> u8;
fn encode(&self, data: &mut Vec<u8>);
fn decode(data: &Vec<u8>, offset: usize, len: u8) -> Self;
fn is_empty(&self) -> bool;
fn write_to(&self, key: LogDataChunkKey, data: &mut Vec<u8>) {
if !self.is_empty() {
let chunk_header = LogDataChunkHeader::from(key, self.get_len());
data.write_plain(chunk_header);
self.encode(data);
}
}
fn get_full_len(&self) -> u32 {
let mut len = self.get_len() as u32;
if len > 0 {
len += size_of::<LogDataChunkHeader>() as u32;
}
len
}
}
impl<T: PlainChunkType> ChunkType for T {
fn get_len(&self) -> u8 {
if self.is_empty() {
0
}
else {
size_of::<T>() as u8
}
}
fn encode(&self, data: &mut Vec<u8>) {
push_to_vec(data, *self)
}
fn decode(data: &Vec<u8>, offset: usize, len: u8) -> Self {
if len == 0 {
Default::default()
}
else {
unsafe {
*(data.as_ptr().offset(offset as isize) as *mut Self)
}
}
}
fn is_empty(&self) -> bool {
*self == Default::default()
}
}
impl PlainChunkType for bool {}
impl PlainChunkType for u32 {}
impl PlainChunkType for u64 {}
pub const MAX_STRING_LEN: u8 = 0x7F;
impl ChunkType for String {
fn get_len(&self) -> u8 {
min(self.len() as u8, MAX_STRING_LEN)
}
fn encode(&self, data: &mut Vec<u8>) {
let bytes = self.as_bytes();
for i in 0..self.get_len() {
data.push(bytes[i as usize]);
}
}
fn decode(data: &Vec<u8>, offset: usize, len: u8) -> Self {
let mut string = String::new();
unsafe {
let ptr = data.as_ptr().offset(offset as isize);
for i in 0..len {
let cur_ptr = ptr.offset(i as isize);
string.push(*cur_ptr as char);
}
}
string
}
fn is_empty(&self) -> bool {
self.is_empty()
}
}
pub trait VecExt {
fn read_plain<T: Copy>(&self, offset: &mut usize) -> Option<T>;
fn write_plain<T: Copy>(&mut self, t: T);
}
impl VecExt for Vec<u8> {
fn read_plain<T: Copy>(&self, offset: &mut usize) -> Option<T> {
let cur_offset = *offset;
if self.len() < (cur_offset + size_of::<T>()) {
return None;
}
unsafe {
*offset += size_of::<T>();
Some(*(self.as_ptr().offset(cur_offset as isize) as *mut T))
}
}
fn write_plain<T: Copy>(&mut self, t: T) {
let t_ptr = &t as *const T as *const u8;
for i in 0..size_of::<T>() {
unsafe {
let cur_t_ptr = t_ptr.offset(i as isize);
self.push(*cur_t_ptr);
}
}
}
}
pub struct LogPacketBody {
pub log_session_begin: bool,
pub log_session_end: bool,
pub text_log: String,
pub line_number: u32,
pub file_name: String,
pub function_name: String,
pub module_name: String,
pub thread_name: String,
pub log_packet_drop_count: u64,
pub user_system_clock: u64,
pub process_name: String
}
impl LogPacketBody {
pub fn new() -> Self {
Self {
log_session_begin: false,
log_session_end: false,
text_log: String::new(),
line_number: 0,
file_name: String::new(),
function_name: String::new(),
module_name: String::new(),
thread_name: String::new(),
log_packet_drop_count: 0,
user_system_clock: 0,
process_name: String::new()
}
}
pub fn compute_size(&self, is_head: bool) -> u32 {
let mut size: u32 = 0;
size += self.text_log.get_full_len();
if is_head {
size += self.log_session_begin.get_full_len();
size += self.log_session_end.get_full_len();
size += self.line_number.get_full_len();
size += self.file_name.get_full_len();
size += self.function_name.get_full_len();
size += self.module_name.get_full_len();
size += self.thread_name.get_full_len();
size += self.log_packet_drop_count.get_full_len();
size += self.user_system_clock.get_full_len();
size += self.process_name.get_full_len();
}
size
}
}