Diff of /app/LogWatcher.py [000000] .. [4de1c7]

Switch to unified view

a b/app/LogWatcher.py
1
import os
2
import time
3
from threading import Thread
4
5
6
class LogWatcher:
7
    def __init__(self):
8
        self.active = True
9
        self.t = None
10
        self.filename = None
11
        self.file_error = False
12
13
    def follow(self, thefile):
14
        thefile.seek(0, 2)  # Go to the end of the file
15
        while self.active:
16
            line = thefile.readline()
17
            if not line:
18
                time.sleep(0.1)  # Sleep briefly
19
                continue
20
            yield line
21
22
    def run(self):
23
        while not os.path.exists(self.filename):
24
            time.sleep(1)
25
            if not self.active:
26
                self.file_error = True
27
                return
28
        logfile = open(self.filename)
29
        print(logfile.read())
30
        loglines = self.follow(logfile)
31
        for line in loglines:
32
            print(line, end='')
33
34
    def start(self, filename):
35
        self.filename = filename
36
        self.t = Thread(target=self.run)
37
        self.t.start()
38
39
    def stop(self):
40
        self.active = False
41
        self.t.join()
42
        if not self.file_error:
43
            # delete log file
44
            os.remove(self.filename)
45
46
47
log_watcher = LogWatcher()