blob: 046c7899daf0a9e8b4472306f888bde1a5670bc2 [file] [log] [blame]
Austin Schuhd1b28992014-10-26 20:55:06 -07001#!/usr/bin/python3
2
3class LogEntry:
4 """This class provides a way to parse log entries."""
5
6 def __init__(self, line):
7 """Creates a LogEntry from a line."""
8 name_index = line.find('(')
9 self.name = line[0:name_index]
10
11 pid_index = line.find(')', name_index + 1)
12 self.pid = int(line[name_index + 1:pid_index])
13
14 msg_index_index = line.find(')', pid_index + 1)
15 self.msg_index = int(line[pid_index + 2:msg_index_index])
16
17 level_index = line.find(' ', msg_index_index + 3)
18 self.level = line[msg_index_index + 3:level_index]
19
20 time_index_start = line.find(' at ', level_index) + 4
21 time_index_end = line.find('s:', level_index)
22 self.time = float(line[time_index_start:time_index_end])
23
24 filename_end = line.find(':', time_index_end + 3)
25 self.filename = line[time_index_end + 3:filename_end]
26
27 linenumber_end = line.find(':', filename_end + 2)
28 self.linenumber = int(line[filename_end + 2:linenumber_end])
29
30 self.msg = line[linenumber_end+2:]
31
32 def __str__(self):
33 """Formats the data cleanly."""
34 return '%s(%d)(%d): %s at %fs: %s: %d: %s' % (self.name, self.pid, self.msg_index, self.level, self.time, self.filename, self.linenumber, self.msg)
35
36 def __JsonizeTokenArray(self, sub_array, tokens, token_index):
37 """Parses an array from the provided tokens.
38
39 Args:
40 sub_array: list, The list to stick the elements in.
41 tokens: list of strings, The list with all the tokens in it.
42 token_index: int, Where to start in the token list.
43
44 Returns:
45 int, The last token used.
46 """
47 # Make sure the data starts with a '['
48 if tokens[token_index] != '[':
49 print(tokens)
50 print('Expected [ at beginning, found', tokens[token_index + 1])
51 return None
52
53 # Eat the '['
54 token_index += 1
55
56 # Loop through the tokens.
57 while token_index < len(tokens):
58 if tokens[token_index + 1] == ',':
59 # Next item is a comma, so we should just add the element.
60 sub_array.append(tokens[token_index])
61 token_index += 2
62 elif tokens[token_index + 1] == ']':
63 # Next item is a ']', so we should just add the element and finish.
64 sub_array.append(tokens[token_index])
65 token_index += 1
66 return token_index
67 else:
68 # Otherwise, it must be a sub-message.
69 sub_json = dict()
70 token_index = self.JsonizeTokens(sub_json, tokens, token_index + 1)
71 sub_array.append(sub_json)
72 if tokens[token_index] == ',':
73 # Handle there either being another data element.
74 token_index += 1
75 elif tokens[token_index] == ']':
76 # Handle the end of the array.
77 return token_index
78 else:
79 print('Unexpected ', tokens[token_index])
80 return None
81
82 print('Unexpected end')
83 return None
84
85 def JsonizeTokens(self, json, tokens, token_index):
86 """Creates a json-like dictionary from the provided tokens.
87
88 Args:
89 json: dict, The dict to stick the elements in.
90 tokens: list of strings, The list with all the tokens in it.
91 token_index: int, Where to start in the token list.
92
93 Returns:
94 int, The last token used.
95 """
96 # Check that the message starts with a {
97 if tokens[token_index] != '{':
98 print(tokens)
99 print('Expected { at beginning, found', tokens[token_index])
100 return None
101
102 # Eat the {
103 token_index += 1
104
105 # States and state variable for parsing elements.
106 STATE_INIT = 'init'
107 STATE_HAS_NAME = 'name'
108 STATE_HAS_COLON = 'colon'
109 STATE_EXPECTING_SUBMSG = 'submsg'
110 STATE_EXPECTING_COMMA = 'comma'
111 parser_state = STATE_INIT
112
113 while token_index < len(tokens):
114 if tokens[token_index] == '}':
115 # Finish if there is a }
116 return token_index + 1
117 elif tokens[token_index] == '{':
118 if parser_state != STATE_EXPECTING_SUBMSG:
119 print(tokens)
120 print(parser_state)
121 print('Bad input, was not expecting {')
122 return None
123 # Found a submessage, parse it.
124 sub_json = dict()
125 token_index = self.JsonizeTokens(sub_json, tokens, token_index)
126 json[token_name] = sub_json
127 parser_state = STATE_EXPECTING_COMMA
128 else:
129 if parser_state == STATE_INIT:
130 # This token is the name.
131 token_name = tokens[token_index]
132 parser_state = STATE_HAS_NAME
133 elif parser_state == STATE_HAS_NAME:
134 if tokens[token_index] != ':':
135 print(tokens)
136 print(parser_state)
137 print('Bad input, found', tokens[token_index], 'expected :')
138 return None
139 # After a name, comes a :
140 parser_state = STATE_HAS_COLON
141 elif parser_state == STATE_HAS_COLON:
142 # After the colon, figure out what is next.
143 if tokens[token_index] == '[':
144 # Found a sub-array!
145 sub_array = []
146 token_index = self.__JsonizeTokenArray(sub_array, tokens, token_index)
147 json[token_name] = sub_array
148 parser_state = STATE_EXPECTING_COMMA
149 elif tokens[token_index + 1] == '{':
150 # Found a sub-message, trigger parsing it.
151 parser_state = STATE_EXPECTING_SUBMSG
152 else:
153 # This is just an element, move on.
154 json[token_name] = tokens[token_index]
155 parser_state = STATE_EXPECTING_COMMA
156 elif parser_state == STATE_EXPECTING_COMMA:
157 # Complain if there isn't a comma here.
158 if tokens[token_index] != ',':
159 print(tokens)
160 print(parser_state)
161 print('Bad input, found', tokens[token_index], 'expected ,')
162 return None
163 parser_state = STATE_INIT
164 else:
165 print('Bad parser state')
166 return None
167 token_index += 1
168
169 print('Unexpected end')
170 return None
171
172 def ParseStruct(self):
173 """Parses the message as a structure.
174
175 Returns:
176 struct_name, struct_type, json dict.
177 """
178 struct_name_index = self.msg.find(':')
179 struct_name = self.msg[0:struct_name_index]
180
181 struct_body = self.msg[struct_name_index+2:]
182 tokens = []
183 this_token = ''
184 # For the various deliminators, append what we have found so far to the
185 # list and the token.
186 for char in struct_body:
187 if char == '{':
188 if this_token:
189 tokens.append(this_token)
190 this_token = ''
191 tokens.append('{')
192 elif char == '}':
193 if this_token:
194 tokens.append(this_token)
195 this_token = ''
196 tokens.append('}')
197 elif char == '[':
198 if this_token:
199 tokens.append(this_token)
200 this_token = ''
201 tokens.append('[')
202 elif char == ']':
203 if this_token:
204 tokens.append(this_token)
205 this_token = ''
206 tokens.append(']')
207 elif char == ':':
208 if this_token:
209 tokens.append(this_token)
210 this_token = ''
211 tokens.append(':')
212 elif char == ',':
213 if this_token:
214 tokens.append(this_token)
215 this_token = ''
216 tokens.append(',')
217 elif char == ' ':
218 if this_token:
219 tokens.append(this_token)
220 this_token = ''
221 else:
222 this_token += char
223 if this_token:
224 tokens.append(this_token)
225
226 struct_type = tokens[0]
227 json = dict()
228 # Now that we have tokens, parse them.
229 self.JsonizeTokens(json, tokens, 1)
230
231 return (struct_name, struct_type, json)
232
233
234def ParseLine(line):
235 return LogEntry(line)
236
237if __name__ == '__main__':
238 print('motor_writer(2240)(07421): DEBUG at 0000000819.99620s: ../../frc971/output/motor_writer.cc: 105: sending: .aos.controls.OutputCheck{pwm_value:221, pulse_length:2.233333}')
239 line = ParseLine('motor_writer(2240)(07421): DEBUG at 0000000819.99620s: ../../frc971/output/motor_writer.cc: 105: sending: .aos.controls.OutputCheck{pwm_value:221, pulse_length:2.233333}')
240 if '.aos.controls.OutputCheck' in line.msg:
241 print(line)
242 print(line.ParseStruct())
243
244 line = ParseLine('claw(2263)(19404): DEBUG at 0000000820.00000s: ../../aos/common/controls/control_loop-tmpl.h: 104: position: .frc971.control_loops.ClawGroup.Position{top:.frc971.control_loops.HalfClawPosition{position:1.672153, front:.frc971.HallEffectStruct{current:f, posedge_count:0, negedge_count:52}, calibration:.frc971.HallEffectStruct{current:f, posedge_count:6, negedge_count:13}, back:.frc971.HallEffectStruct{current:f, posedge_count:0, negedge_count:62}, posedge_value:0.642681, negedge_value:0.922207}, bottom:.frc971.control_loops.HalfClawPosition{position:1.353539, front:.frc971.HallEffectStruct{current:f, posedge_count:2, negedge_count:150}, calibration:.frc971.HallEffectStruct{current:f, posedge_count:8, negedge_count:18}, back:.frc971.HallEffectStruct{current:f, posedge_count:0, negedge_count:6}, posedge_value:0.434514, negedge_value:0.759491}}')
245 print(line.ParseStruct())
246
247 line = ParseLine('joystick_proxy(2255)(39560): DEBUG at 0000000820.00730s: ../../aos/prime/input/joystick_input.cc: 61: sending: .aos.RobotState{joysticks:[.aos.Joystick{buttons:0, axis:[0.000000, 1.000000, 1.000000, 0.000000]}, .aos.Joystick{buttons:0, axis:[-0.401575, 1.000000, -1.007874, 0.000000]}, .aos.Joystick{buttons:0, axis:[0.007874, 0.000000, 1.000000, -1.007874]}, .aos.Joystick{buttons:0, axis:[0.000000, 0.000000, 0.000000, 0.000000]}], test_mode:f, fms_attached:f, enabled:T, autonomous:f, team_id:971, fake:f}')
248 print(line.ParseStruct())