-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_tailer.py
48 lines (40 loc) · 1.24 KB
/
log_tailer.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
import re
import time
class LogTail:
def __init__(self, file):
self.f = file
self.buffer = ""
def read_line(self):
try:
temp = self.f.readline()
except UnicodeDecodeError:
return None
if temp != "":
self.buffer += temp
if self.buffer.endswith("\n"):
line_out = self.buffer
self.buffer = ""
return line_out
else:
return None
def read(self, pred=None):
temp = ""
if pred is not None:
while line := self.read_line():
if pred(line):
temp += line
else:
while line := self.read_line():
temp += line
return temp
def is_kill(line):
return re.match(
"""\d\d\/\d\d\/\d\d\d\d - \d\d:\d\d:\d\d: ([^\n]{0,32}) killed ([^\n]{0,32}) with (\w+)(\.|(\. \(crit\)))""",
line) is not None
if __name__ == '__main__':
with open("E:\\Programs\\Steam\\steamapps\\common\\Team Fortress 2\\tf\\console.log", 'r') as file:
log = LogTail(file)
while True:
while line := log.read(is_kill):
print(line, end='')
time.sleep(0.5)