-
Notifications
You must be signed in to change notification settings - Fork 8
/
mux_reader.py
executable file
·67 lines (56 loc) · 1.96 KB
/
mux_reader.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
#!/usr/bin/python3
"""Reader for stdio-mux output files."""
import argparse
import collections
import struct
import sys
Packet = collections.namedtuple('Packet',
['stream_id', 'comm', 'timestamp', 'message'])
def _main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'file',
type=argparse.FileType('rb'),
default=sys.stdin.buffer,
help='The output file from stdio-mux')
args = parser.parse_args()
buffers = {}
process_names = {}
packets = []
last_timestamp = 0
while True:
header = args.file.read(12)
if not header:
break
stream_id, timestamp = struct.unpack('=IQ', header)
message_length = timestamp & 0xFFFF
timestamp >>= 16
last_timestamp = max(last_timestamp, timestamp)
message = args.file.read(message_length)
if stream_id not in process_names:
process_names[stream_id] = message.decode(errors='replace').strip()
buffers[stream_id] = ''
continue
buffers[stream_id] += message.decode(errors='replace')
while True:
newline = buffers[stream_id].find('\n')
if newline == -1:
break
packets.append(
Packet(stream_id, process_names[stream_id], timestamp,
buffers[stream_id][:newline]))
buffers[stream_id] = buffers[stream_id][newline + 1:]
for stream_id, contents in buffers.items():
if not contents:
continue
packets.append(
Packet(stream_id, process_names[stream_id], last_timestamp,
contents))
packets.sort(key=lambda packet: packet.timestamp)
for packet in packets:
color = '\033[0m'
if packet.stream_id % 2 == 0:
color = '\033[91m'
print('%s[%s]: %s' % (color, packet.comm, packet.message))
if __name__ == '__main__':
_main()