-
Notifications
You must be signed in to change notification settings - Fork 5
/
excel_logger.py
134 lines (98 loc) · 5.12 KB
/
excel_logger.py
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
import xlsxwriter
from datetime import datetime
import logging
log = logging.getLogger(__name__)
class ExcelLogger:
def __init__(self, out_path='./dump.xlsx'):
self.out_path = out_path
self.excel_workbook = xlsxwriter.Workbook(out_path)
self.format_x1_x10 = self.excel_workbook.add_format()
self.format_x2_x10 = self.excel_workbook.add_format()
self.format_x80_x10 = self.excel_workbook.add_format()
self.format_x1_x10.set_pattern(1)
self.format_x2_x10.set_pattern(1)
self.format_x80_x10.set_pattern(1)
self.format_x1_x10.set_bg_color('edf0e0')
self.format_x2_x10.set_bg_color('f5c9ce')
self.format_x80_x10.set_bg_color('fbeaa5')
self.worksheet_all_ports = self.excel_workbook.add_worksheet("all")
self.worksheet_x1x10 = self.excel_workbook.add_worksheet("x1x10")
self.worksheet_x2x10 = self.excel_workbook.add_worksheet("x2x10")
self.worksheet_x80x10 = self.excel_workbook.add_worksheet("x80x10")
self.xlsx_row_num = 0
self.x1_x10_row_num = 0
self.x2_x10_row_num = 0
self.x80_x10_row_num = 0
self.session_start_time = datetime.now()
self.packet_count = 0
def save(self):
self.excel_workbook.close()
self.excel_workbook = None
log.info("Saved Excel sheet at: " + str(self.out_path))
def log(self, data):
if self.excel_workbook is None:
return
time_offset = datetime.now() - self.session_start_time
full_data_str = ''
for i, d in enumerate(data):
e = hex(d)
if i == 9:
full_data_str += (e + ', ')
else:
full_data_str += (e + ', ')
if full_data_str.endswith(', '):
full_data_str = full_data_str[:-2]
# OUT (Host to Device)
if data[4] == 0x1:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('C' + str(self.xlsx_row_num), full_data_str, self.format_x1_x10)
self.worksheet_x1x10.write('A' + str(self.x1_x10_row_num), self.packet_count)
self.worksheet_x1x10.write('B' + str(self.x1_x10_row_num), str(time_offset))
self.worksheet_x1x10.write('C' + str(self.x1_x10_row_num), full_data_str, self.format_x1_x10)
self.x1_x10_row_num += 1
elif data[4] == 0x2:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('C' + str(self.xlsx_row_num), full_data_str, self.format_x2_x10)
self.worksheet_x2x10.write('A' + str(self.x2_x10_row_num), self.packet_count)
self.worksheet_x2x10.write('B' + str(self.x2_x10_row_num), str(time_offset))
self.worksheet_x2x10.write('C' + str(self.x2_x10_row_num), full_data_str, self.format_x2_x10)
self.x2_x10_row_num += 1
elif data[4] == 0x80:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('C' + str(self.xlsx_row_num), full_data_str, self.format_x80_x10)
self.worksheet_x80x10.write('A' + str(self.x80_x10_row_num), self.packet_count)
self.worksheet_x80x10.write('B' + str(self.x80_x10_row_num), str(time_offset))
self.worksheet_x80x10.write('C' + str(self.x80_x10_row_num), full_data_str, self.format_x80_x10)
self.x80_x10_row_num += 1
# IN (Device to Host)
elif data[6] == 0x1:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('E' + str(self.xlsx_row_num), full_data_str, self.format_x1_x10)
self.worksheet_x1x10.write('A' + str(self.x1_x10_row_num), self.packet_count)
self.worksheet_x1x10.write('B' + str(self.x1_x10_row_num), str(time_offset))
self.worksheet_x1x10.write('E' + str(self.x1_x10_row_num), full_data_str, self.format_x1_x10)
self.x1_x10_row_num += 1
elif data[6] == 0x2:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('E' + str(self.xlsx_row_num), full_data_str, self.format_x2_x10)
self.worksheet_x2x10.write('A' + str(self.x2_x10_row_num), self.packet_count)
self.worksheet_x2x10.write('B' + str(self.x2_x10_row_num), str(time_offset))
self.worksheet_x2x10.write('E' + str(self.x2_x10_row_num), full_data_str, self.format_x2_x10)
self.x2_x10_row_num += 1
elif data[6] == 0x80:
self.worksheet_all_ports.write('A' + str(self.xlsx_row_num), self.packet_count)
self.worksheet_all_ports.write('B' + str(self.xlsx_row_num), str(time_offset))
self.worksheet_all_ports.write('E' + str(self.xlsx_row_num), full_data_str, self.format_x80_x10)
self.worksheet_x80x10.write('A' + str(self.x80_x10_row_num), self.packet_count)
self.worksheet_x80x10.write('B' + str(self.x80_x10_row_num), str(time_offset))
self.worksheet_x80x10.write('E' + str(self.x80_x10_row_num), full_data_str, self.format_x80_x10)
self.x80_x10_row_num += 1
else:
print("WARNING: Unknown communication path in endpoint 0x1")
self.xlsx_row_num += 1
self.packet_count += 1