Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 1ddd8f5

Browse files
committed
Improve datalog reader API to be more pythonic
- Add bulk of wpilib printlog example to examples
1 parent 16ca13a commit 1ddd8f5

File tree

3 files changed

+290
-23
lines changed

3 files changed

+290
-23
lines changed

examples/printlog.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) FIRST and other WPILib contributors.
4+
# Open Source Software; you can modify and/or share it under the terms of
5+
# the WPILib BSD license file in the root directory of this project.
26

37
import argparse
4-
import pathlib
8+
import datetime
59

610
from wpiutil.log import DataLogReader
711

@@ -11,8 +15,78 @@
1115
args = parser.parse_args()
1216

1317
reader = DataLogReader(args.infile)
18+
19+
entries = {}
1420
for record in reader:
21+
timestamp = record.getTimestamp() / 1000000
1522
if record.isStart():
16-
print(record.getStartData())
23+
try:
24+
data = record.getStartData()
25+
print(f"{data} [{timestamp}]")
26+
if data.entry in entries:
27+
print("...DUPLICATE entry ID, overriding")
28+
entries[data.entry] = data
29+
except TypeError as e:
30+
print("Start(INVALID)")
31+
elif record.isFinish():
32+
try:
33+
entry = record.getFinishEntry()
34+
print(f"Finish({entry}) [{timestamp}]")
35+
if entry not in entries:
36+
print("...ID not found")
37+
else:
38+
del entries[entry]
39+
except TypeError as e:
40+
print("Finish(INVALID)")
41+
elif record.isSetMetadata():
42+
try:
43+
data = record.getSetMetadataData()
44+
print(f"{data} [{timestamp}]")
45+
if data.entry not in entries:
46+
print("...ID not found")
47+
except TypeError as e:
48+
print("SetMetadata(INVALID)")
49+
elif record.isControl():
50+
print("Unrecognized control record")
1751
else:
18-
print(record.getBoolean())
52+
print(f"Data({record.getEntry()}, size={record.getSize()}) ", end="")
53+
entry = entries.get(record.getEntry(), None)
54+
if entry is None:
55+
print("<ID not found>")
56+
continue
57+
print(f"<name='{entry.name}', type='{entry.type}'> [{timestamp}]")
58+
59+
try:
60+
# handle systemTime specially
61+
if entry.name == "systemTime" and entry.type == "int64":
62+
dt = datetime.fromtimestamp(record.getInteger() / 1000000)
63+
print(" {:%Y-%m-%d %H:%M:%S.%f}".format(dt))
64+
continue
65+
66+
if entry.type == "double":
67+
print(f" {record.getDouble()}")
68+
elif entry.type == "int64":
69+
print(f" {record.getInteger()}")
70+
elif entry.type == "string" or entry.type == "json":
71+
print(f" '{record.getString()}'")
72+
elif entry.type == "boolean":
73+
print(f" {record.getBoolean()}")
74+
elif entry.type == "boolean[]":
75+
arr = record.getBooleanArray()
76+
print(f" {arr}")
77+
elif entry.type == "double[]":
78+
arr = record.getDoubleArray()
79+
print(f" {arr}")
80+
elif entry.type == "float[]":
81+
arr = record.getFloatArray()
82+
print(f" {arr}")
83+
elif entry.type == "int64[]":
84+
arr = record.getIntegerArray()
85+
print(f" {arr}")
86+
elif entry.type == "string[]":
87+
arr = record.getStringArray()
88+
print(f" {arr}")
89+
elif entry.type == "raw":
90+
print(f" {record.getRaw()}")
91+
except TypeError as e:
92+
print(" invalid", e)

examples/writelog.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import pathlib
55

6-
from wpiutil.log import DataLog, BooleanLogEntry
6+
from wpiutil.log import DataLog, BooleanLogEntry, StringArrayLogEntry, RawLogEntry
77

88

99
if __name__ == "__main__":
@@ -20,3 +20,11 @@
2020
bools = BooleanLogEntry(datalog, "/bools")
2121
bools.append(True)
2222
bools.append(False)
23+
24+
strings = StringArrayLogEntry(datalog, "/strings")
25+
strings.append(["a", "b", "c"])
26+
strings.append(["d", "e", "f"])
27+
28+
raw = RawLogEntry(datalog, "/raws")
29+
raw.append(b'\x01\x02\x03')
30+
raw.append(b'\x04\x05\x06')

0 commit comments

Comments
 (0)