Stefan Schuermans commited on 2020-05-23 11:15:35
Showing 3 changed files, with 33 additions and 19 deletions.
| ... | ... |
@@ -1,5 +1,4 @@ |
| 1 |
-import uproctrace.uproctrace_pb2 as pb2 |
|
| 2 |
-import struct |
|
| 1 |
+import uproctrace.parse |
|
| 3 | 2 |
|
| 4 | 3 |
|
| 5 | 4 |
def dump_event(f, out): |
| ... | ... |
@@ -7,23 +6,10 @@ def dump_event(f, out): |
| 7 | 6 |
Read the first event from f and dump it to out. |
| 8 | 7 |
Return True if an event could be found and dumped, False otherwise. |
| 9 | 8 |
""" |
| 10 |
- # skip till after magic |
|
| 11 |
- magic = f.read(4) |
|
| 12 |
- while magic != b'upt0': |
|
| 13 |
- if len(magic) < 4: |
|
| 14 |
- return False # EOF |
|
| 15 |
- magic = magic[1:] + f.read(1) # search for magic byte for byte |
|
| 16 |
- # read size of next event (32 bit network byte order) |
|
| 17 |
- size = f.read(4) |
|
| 18 |
- if len(size) < 4: |
|
| 19 |
- return False # EOF |
|
| 20 |
- size = struct.unpack('!L', size)[0]
|
|
| 21 |
- # read event data |
|
| 22 |
- data = f.read(size) |
|
| 23 |
- if len(data) < size: |
|
| 24 |
- return False # EOF |
|
| 25 |
- # unpack event |
|
| 26 |
- event = pb2.event.FromString(data) |
|
| 9 |
+ # read event |
|
| 10 |
+ event = uproctrace.parse.read_event(f) |
|
| 11 |
+ if event is None: |
|
| 12 |
+ return False |
|
| 27 | 13 |
# dump event |
| 28 | 14 |
print('event {', file=out)
|
| 29 | 15 |
for line in repr(event).split('\n'):
|
| ... | ... |
@@ -0,0 +1,27 @@ |
| 1 |
+import uproctrace.uproctrace_pb2 as pb2 |
|
| 2 |
+import struct |
|
| 3 |
+ |
|
| 4 |
+ |
|
| 5 |
+def read_event(f): |
|
| 6 |
+ """ |
|
| 7 |
+ Read the first event from f and return it. |
|
| 8 |
+ Return None if no event could be found. |
|
| 9 |
+ """ |
|
| 10 |
+ # skip till after magic |
|
| 11 |
+ magic = f.read(4) |
|
| 12 |
+ while magic != b'upt0': |
|
| 13 |
+ if len(magic) < 4: |
|
| 14 |
+ return None # EOF |
|
| 15 |
+ magic = magic[1:] + f.read(1) # search for magic byte for byte |
|
| 16 |
+ # read size of next event (32 bit network byte order) |
|
| 17 |
+ size = f.read(4) |
|
| 18 |
+ if len(size) < 4: |
|
| 19 |
+ return None # EOF |
|
| 20 |
+ size = struct.unpack('!L', size)[0]
|
|
| 21 |
+ # read event data |
|
| 22 |
+ data = f.read(size) |
|
| 23 |
+ if len(data) < size: |
|
| 24 |
+ return None # EOF |
|
| 25 |
+ # unpack event |
|
| 26 |
+ event = pb2.event.FromString(data) |
|
| 27 |
+ return event |
|
| 0 | 28 |