-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathlog_monitor.py
More file actions
78 lines (57 loc) · 1.69 KB
/
log_monitor.py
File metadata and controls
78 lines (57 loc) · 1.69 KB
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
68
69
70
71
72
73
74
75
76
77
import glob
import os
import time
disqualified_racers = set()
finished_racers = set()
def open_file():
list_of_files = glob.glob("*.log")
latest_file = max(list_of_files, key=os.path.getctime)
print("Opened file: " + latest_file)
return open(latest_file, "r+")
def follow(filename):
filename.seek(0, 2)
while True:
line = filename.readline()
if not line:
time.sleep(0.1)
continue
yield line
def process(line):
tokens = line.split()
if len(tokens) != 3 and len(tokens) != 5:
print("ERROR Bad line: " + line)
print("Tokens: " + str(tokens))
return
if (
tokens[3] == "disqualified"
and tokens[4] == "1"
and tokens[0] not in disqualified_racers
):
disqualified_racers.add(tokens[0])
handle_disqualified_racer(tokens[0])
return
if (
tokens[3] == "finished"
and tokens[4] == "1"
and tokens[0] not in finished_racers
):
finished_racers.add(tokens[0])
handle_finished_racer(tokens[0])
return
if tokens[3] == "gates_passed":
handle_gate_passed(tokens[0], tokens[4])
def handle_disqualified_racer(racer_name):
print(racer_name + " has been disqualified!")
# Start a new race.
def handle_finished_racer(racer_name):
print(racer_name + " has finished!")
# Start a new race.
def handle_gate_passed(racer_name, gate_idx_passed):
# log file gate indices are 1-indexed, not 0-indexed
print("{} passed gate idx {}".format(racer_name, gate_idx_passed - 1))
def main():
f = open_file()
for line in follow(f):
process(line)
if __name__ == "__main__":
main()