-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathloglib.py
More file actions
642 lines (622 loc) · 23.1 KB
/
Copy pathloglib.py
File metadata and controls
642 lines (622 loc) · 23.1 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import re
import math
from datetime import datetime
import logging
import numpy as np
import gzip
def rbktimetodate(rbktime):
""" 将rbk的时间戳转化为datatime """
return datetime.strptime(rbktime, '%Y-%m-%d %H:%M:%S.%f')
def findrange(ts, t1, t2):
""" 在ts中寻找大于t1小于t2对应的下标 """
small_ind = -1
large_ind = len(ts)-1
for i, data in enumerate(ts):
large_ind = i
if(t1 <= data and small_ind < 0):
small_ind = i
if(t2 <= data):
break
return small_ind, large_ind
def polar2xy(angle, dist):
""" 将极坐标angle,dist 转化为xy坐标 """
x , y = [], []
for a, d in zip(angle, dist):
x.append(d * math.cos(a))
y.append(d * math.sin(a))
return x,y
class ReadLog:
""" 读取Log """
def __init__(self, filenames):
""" 支持传入多个文件名称"""
self.filenames = filenames
def _readData(self, f, file, argv):
line_num = 0
for line in f.readlines():
try:
line = line.decode('utf-8')
except UnicodeDecodeError:
try:
line = line.decode('gbk')
except UnicodeDecodeError:
print(file, " L:",line_num+1, " is skipped due to decoding failure!", " ", line)
continue
line_num += 1
break_flag = False
for data in argv:
if type(data).__name__ == 'dict':
for k in data.keys():
if data[k].parse(line):
break_flag = True
break
if break_flag:
break_flag = False
break
elif data.parse(line):
break
def parse(self,*argv):
"""依据输入的正则进行解析"""
for file in self.filenames:
if file.endswith(".log"):
with open(file,'rb') as f:
self._readData(f,file, argv)
else:
with gzip.open(file,'rb') as f:
self._readData(f, file, argv)
class Data:
def __init__(self, info):
self.type = info['type']
self.regex = re.compile("\[(.*?)\].*\["+self.type+"\]\[(.*?)\]")
self.short_regx = re.compile("\["+self.type+"\]\[")
self.info = info['content']
self.data = dict()
self.data['t'] = []
self.description = dict()
self.unit = dict()
self.parse_error = False
for tmp in self.info:
self.data[tmp['name']] = []
if 'unit' in tmp:
self.unit[tmp['name']] = tmp['unit']
else:
self.unit[tmp['name']] = ""
if 'description' in tmp:
self.description[tmp['name']] = tmp['description'] + " " + self.unit[tmp['name']]
else:
self.description[tmp['name']] = self.type + '.' + tmp['name'] + " " + self.unit[tmp['name']]
def _storeData(self, tmp, ind, values):
if tmp['type'] == 'double' or tmp['type'] == 'int64':
try:
self.data[tmp['name']].append(float(values[ind]))
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'mm':
try:
self.data[tmp['name']].append(float(values[ind])/1000.0)
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'cm':
try:
self.data[tmp['name']].append(float(values[ind])/100.0)
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'rad':
try:
self.data[tmp['name']].append(float(values[ind])/math.pi * 180.0)
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'm':
try:
self.data[tmp['name']].append(float(values[ind]))
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'LSB':
try:
self.data[tmp['name']].append(float(values[ind])/16.03556)
except:
self.data[tmp['name']].append(0.0)
elif tmp['type'] == 'bool':
try:
if values[ind] == "true" or values[ind] == "1":
self.data[tmp['name']].append(1.0)
else:
self.data[tmp['name']].append(0.0)
except:
self.data[tmp['name']].append(0.0)
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
datas = out.groups()
values = datas[1].split('|')
self.data['t'].append(rbktimetodate(datas[0]))
for tmp in self.info:
if 'type' in tmp and 'index' in tmp and 'name' in tmp:
if tmp['index'] < len(values):
self._storeData(tmp, int(tmp['index']), values)
else:
self.data[tmp['name']].append(np.nan)
elif 'type' in tmp and 'name' in tmp:
ind = values.index(tmp['name']) if tmp['name'] in values else -1
if ind >= 0 and ind + 1 < len(values):
self._storeData(tmp, ind+1, values)
else:
self.data[tmp['name']].append(np.nan)
else:
if not self.parse_error:
logging.error("Error in {} {} ".format(self.type, tmp.keys()))
self.parse_error = True
return True
return False
return False
def __getitem__(self,k):
return self.data[k]
def __setitem__(self,k,value):
self.data[k] = value
class Laser:
""" 激光雷达的数据
data[0]: t
data[1]: ts 激光点的时间戳
data[2]: angle rad
data[3]: dist m
data[4]: x m
data[5]: y m
data[6]: number
"""
def __init__(self, max_dist):
""" max_dist 为激光点的最远距离,大于此距离激光点无效"""
self.regex = re.compile('\[(.*?)\].*\[Laser:? ?(\d*?)\]\[(.*?)\]')
self.short_regx = re.compile("\[Laser")
#self.data = [[] for _ in range(7)]
self.datas = dict()
self.max_dist = max_dist
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
datas = out.groups()
laser_id = 0
if datas[1] != "":
laser_id = int(datas[1])
if laser_id not in self.datas:
self.datas[laser_id] = [[] for _ in range(7)]
self.datas[laser_id][0].append(rbktimetodate(datas[0]))
tmp_datas = datas[2].split('|')
self.datas[laser_id][1].append(float(tmp_datas[0]))
#min_angle = float(tmp_datas[1])
#max_angle = float(tmp_datas[2])
#step_angle = float(tmp_datas[3])
#data_number = int((max_angle - min_angle) / step_angle)
angle = [float(tmp)/180.0*math.pi for tmp in tmp_datas[4::2]]
dist = [float(tmp) for tmp in tmp_datas[5::2]]
tmp_a, tmp_d = [], []
for a, d in zip(angle,dist):
if d < self.max_dist:
tmp_a.append(a)
tmp_d.append(d)
angle = tmp_a
dist = tmp_d
self.datas[laser_id][2].append(angle)
self.datas[laser_id][3].append(dist)
x , y = polar2xy(angle, dist)
self.datas[laser_id][4].append(x)
self.datas[laser_id][5].append(y)
self.datas[laser_id][6].append(len(x))
return True
return False
return False
def t(self, laser_index):
return self.datas[laser_index][0]
def ts(self, laser_index):
return self.datas[laser_index][1], self.datas[laser_index][0]
def angle(self, laser_index):
return self.datas[laser_index][2], self.datas[laser_index][0]
def dist(self, laser_index):
return self.datas[laser_index][3], self.datas[laser_index][0]
def x(self, laser_index):
return self.datas[laser_index][4], self.datas[laser_index][0]
def y(self, laser_index):
return self.datas[laser_index][5], self.datas[laser_index][0]
def number(self, laser_index):
return self.datas[laser_index][6], self.datas[laser_index][0]
class DepthCamera:
""" 深度摄像头的数据
data[0]: t
data[1]: x m
data[2]: y m
data[3]: z m
data[4]: number
data[5]: ts
"""
def __init__(self):
""" max_dist 为激光点的最远距离,大于此距离激光点无效"""
self.regex = re.compile('\[(.*?)\].* \[DepthCamera\d*?\]\[(.*?)\]')
self.short_regx = re.compile("\[DepthCamera\d*?\]\[")
#self.data = [[] for _ in range(7)]
self.datas = [[] for _ in range(6)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
datas = out.groups()
tmp_datas = datas[1].split('|')
if(len(tmp_datas) < 2):
return True
self.datas[0].append(rbktimetodate(datas[0]))
ts = 0
if len(tmp_datas[1:]) %3 == 0:
dx = [float(tmp) for tmp in tmp_datas[1::3]]
dy = [float(tmp) for tmp in tmp_datas[2::3]]
dz = [float(tmp) for tmp in tmp_datas[3::3]]
ts = float(tmp_datas[0])
elif len(tmp_datas)%2 == 0:
dx = [float(tmp) for tmp in tmp_datas[0::2]]
dy = [float(tmp) for tmp in tmp_datas[1::2]]
dz = [0 for tmp in dx]
else:
dx = [float(tmp) for tmp in tmp_datas[1::2]]
dy = [float(tmp) for tmp in tmp_datas[2::2]]
dz = [0 for tmp in dx]
ts = float(tmp_datas[0])
self.datas[1].append(dx)
self.datas[2].append(dy)
self.datas[3].append(dz)
self.datas[4].append(len(tmp_datas))
self.datas[5].append(ts)
return True
return False
return False
def t(self):
return self.datas[0]
def x(self):
return self.datas[1], self.datas[0]
def y(self):
return self.datas[2], self.datas[0]
def z(self):
return self.datas[3], self.datas[0]
def number(self):
return self.datas[4], self.datas[0]
def ts(self):
return self.datas[5], self.datas[0]
class ParticleState:
""" 粒子滤波数据
data[0]: t
data[1]: x m
data[2]: y m
data[3]: theta m
data[4]: number
data[5]: ts
"""
def __init__(self):
self.regex = re.compile('\[(.*?)\].* \[Particle State: \]\[(.*?)\]')
self.short_regx = re.compile("\[Particle State: \]\[")
#self.data = [[] for _ in range(7)]
self.datas = [[] for _ in range(6)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
datas = out.groups()
tmp_datas = datas[1].split('|')
if(len(tmp_datas) < 2):
return True
dx, dy, dz, ts = [], [], [], 0
if len(tmp_datas[1:]) %3 == 0:
dx = [float(tmp) for tmp in tmp_datas[1::3]]
dy = [float(tmp) for tmp in tmp_datas[2::3]]
dz = [float(tmp) for tmp in tmp_datas[3::3]]
ts = float(tmp_datas[0])
else:
return True
self.datas[0].append(rbktimetodate(datas[0]))
self.datas[1].append(dx)
self.datas[2].append(dy)
self.datas[3].append(dz)
self.datas[4].append(len(dx))
self.datas[5].append(ts)
return True
return False
return False
def t(self):
return self.datas[0]
def x(self):
return self.datas[1], self.datas[0]
def y(self):
return self.datas[2], self.datas[0]
def theta(self):
return self.datas[3], self.datas[0]
def number(self):
return self.datas[4], self.datas[0]
def ts(self):
return self.datas[5], self.datas[0]
class ErrorLine:
""" 错误信息
data[0]: t
data[1]: 错误信息内容
data[2]: Alarm 错误编号
data[3]: Alarm 内容
"""
def __init__(self):
self.general_regex = re.compile("\[(.*?)\].*\[error\].*")
self.regex = re.compile("\[(.*?)\].*\[error\].*\[Alarm\]\[.*?\|(.*?)\|(.*?)\|.*")
self.short_regx = re.compile("\[error\]")
self.data = [[] for _ in range(4)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = out.group(2)
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append(out.group(3))
return True
else:
out = self.general_regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = '00000'
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append('unKnown Error')
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
def alarmnum(self):
return self.data[2], self.data[0]
def alarminfo(self):
return self.data[3], self.data[0]
class WarningLine:
""" 报警信息
data[0]: t
data[1]: 报警信息内容
data[2]: Alarm 错误编号
data[3]: Alarm 内容
"""
def __init__(self):
self.general_regex = re.compile("\[(.*?)\].*\[warning\].*")
self.regex = re.compile("\[(.*?)\].*\[warning\].*\[Alarm\]\[.*?\|(.*?)\|(.*?)\|.*")
self.short_regx = re.compile("\[warning\]")
self.data = [[] for _ in range(4)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = out.group(2)
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append(out.group(3))
return True
else:
out = self.general_regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = '00000'
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append('unKnown Warning')
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
def alarmnum(self):
return self.data[2], self.data[0]
def alarminfo(self):
return self.data[3], self.data[0]
class FatalLine:
""" 错误信息
data[0]: t
data[1]: 报警信息内容
data[2]: Alarm 错误编号
data[3]: Alarm 内容
"""
def __init__(self):
self.regex = re.compile("\[(.*?)\].*\[fatal\].*\[Alarm\]\[.*?\|(.*?)\|(.*?)\|.*")
self.short_regx = re.compile("\[fatal\]")
self.data = [[] for _ in range(4)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = out.group(2)
new_data_flag = True
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append(out.group(3))
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
def alarmnum(self):
return self.data[2], self.data[0]
def alarminfo(self):
return self.data[3], self.data[0]
class NoticeLine:
""" 注意信息
data[0]: t
data[1]: 注意信息内容
data[2]: Alarm 错误编号
data[3]: Alarm 内容
"""
def __init__(self):
self.regex = re.compile("\[(.*?)\].*\[Alarm\]\[Notice\|(.*?)\|(.*?)\|.*")
self.short_regx = re.compile("\[Alarm\]\[Notice\|")
self.data = [[] for _ in range(4)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
new_num = out.group(2)
if not new_num in self.data[2]:
self.data[2].append(new_num)
self.data[3].append(out.group(3))
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
def alarmnum(self):
return self.data[2], self.data[0]
def alarminfo(self):
return self.data[3], self.data[0]
class TaskStart:
""" 任务开始信息
data[0]: t
data[1]: 开始信息内容
"""
def __init__(self):
self.regex = re.compile("\[(.*?)\].*\[Text\]\[cnt:.*")
self.short_regx = re.compile("\[Text\]\[cnt:")
self.data = [[] for _ in range(2)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
class TaskFinish:
""" 任务结束信息
data[0]: t
data[1]: 结束信息内容
"""
def __init__(self):
self.regex = re.compile("\[(.*?)\].*\[Text\]\[Task finished.*")
self.short_regx = re.compile("\[Text\]\[Task finished.")
self.data = [[] for _ in range(2)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
class Service:
""" 服务信息
data[0]: t
data[1]: 服务内容
"""
def __init__(self):
self.regex = re.compile("\[(.*?)\].*\[Service\].*")
self.short_regx = re.compile("\[Service\].")
self.data = [[] for _ in range(2)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
out = self.regex.match(line)
if out:
self.data[0].append(rbktimetodate(out.group(1)))
self.data[1].append(out.group(0))
return True
return False
return False
def t(self):
return self.data[0]
def content(self):
return self.data[1], self.data[0]
class Memory:
""" 内存信息
t[0]:
t[1]:
t[2]:
t[3]:
t[4]:
t[5]:
data[0]: used_sys
data[1]: free_sys
data[2]: rbk_phy
data[3]: rbk_vir
data[4]: rbk_max_phy
data[5]: rbk_max_vir
data[6]: cpu_usage
"""
def __init__(self):
self.regex = [re.compile("\[(.*?)\].*\[Text\]\[Used system memory *: *(.*?) *([MG])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Free system memory *: *(.*?) *([MG])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Robokit physical memory usage *: *(.*?) *([GM])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Robokit virtual memory usage *: *(.*?) *([GM])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Robokit Max physical memory usage *: *(.*?) *([GM])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Robokit Max virtual memory usage *: *(.*?) *([GM])B\]"),
re.compile("\[(.*?)\].*\[Text\]\[Robokit CPU usage *: *(.*?)%\]"),
re.compile("\[(.*?)\].*\[Text\]\[System CPU usage *: *(.*?)%\]"),]
self.short_regx = re.compile("memory|CPU")
self.time = [[] for _ in range(8)]
self.data = [[] for _ in range(8)]
def parse(self, line):
short_out = self.short_regx.search(line)
if short_out:
for iter in range(0,8):
out = self.regex[iter].match(line)
if out:
self.time[iter].append(rbktimetodate(out.group(1)))
if iter == 6:
self.data[iter].append(float(out.group(2)))
else:
if out.group(3) == "G":
self.data[iter].append(float(out.group(2)) * 1024.0)
else:
self.data[iter].append(float(out.group(2)))
return True
return False
return False
def t(self):
return self.time[0]
def used_sys(self):
return self.data[0], self.time[0]
def free_sys(self):
return self.data[1], self.time[1]
def rbk_phy(self):
return self.data[2], self.time[2]
def rbk_vir(self):
return self.data[3], self.time[3]
def rbk_max_phy(self):
return self.data[4], self.time[4]
def rbk_max_vir(self):
return self.data[5], self.time[5]
def rbk_cpu(self):
return self.data[6], self.time[6]
def sys_cpu(self):
return self.data[7], self.time[7]