-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
37 lines (30 loc) · 879 Bytes
/
log.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
import datetime
import time
import threading
from config import get_config
class Buffer():
data = []
lastChangedTime = 0
def log(message):
iso = datetime.datetime.now().isoformat()
message = f"[{iso}] {message}"
#print(message)
Buffer.data.append(message)
Buffer.lastChangedTime = time.time()
def write_periodically():
while True:
time.sleep(10)
data = Buffer.data.copy()
Buffer.data.clear()
if len(data) == 0:
print("Nothing in logs to write")
continue
print("Writing to logs")
with open(get_config()["logfile"], "a") as f:
toWrite = ""
for message in data:
toWrite += message.replace("\n", "[NEWLINE]") + "\n"
f.write(toWrite)
def start_logging():
t = threading.Thread(target=write_periodically)
t.start()