Don't crash on a malformed log entry. Move lazy parse of struct.
Austin asked to be resilient when the log has a truncated final entry.
The lazy parsing of struct part of an entry belongs in LogEntry, not
in the reader.
Change-Id: I81d76a116609ac73834863fdd15d2d657d24deaf
diff --git a/frc971/analysis/logreader.py b/frc971/analysis/logreader.py
index 34f1b01..c59487b 100644
--- a/frc971/analysis/logreader.py
+++ b/frc971/analysis/logreader.py
@@ -47,7 +47,11 @@
"""
with open(f, 'r') as fd:
for line in fd:
- self.HandleLine(line)
+ try:
+ self.HandleLine(line)
+ except Exception as ex:
+ # It's common for the last line of the file to be malformed.
+ print("Ignoring malformed log entry: ", line)
def HandleLine(self, line):
"""
@@ -60,7 +64,6 @@
None
"""
pline = LogEntry(line)
- pline_data = None
for key in self.signal:
value = self.signal[key]
@@ -79,12 +82,9 @@
# Make sure that we're looking at the right binary structure instance.
if binary == pline.name:
if pline.msg.startswith(struct_instance_name + ': '):
- # Parse the structure once.
- if pline_data is None:
- _, _, pline_data = pline.ParseStruct()
# Traverse the structure as specified in `data_search_path`.
# This lets the user access very deeply nested structures.
- data = pline_data
+ _, _, data = pline.ParseStruct()
for path in data_search_path:
data = data[path]