-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·72 lines (58 loc) · 2.03 KB
/
run.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
68
69
70
71
72
import os
import sys
import logging
import datetime
from typing import Callable
from lib.view import gui
class StreamLogger:
"""Helper class to redirect stdout and stderr to the same log file as
all python logging messages.
Args:
logfunc (Callable): function of the logger to be used to emit the
message. Recommended to use the root logger, e.g. logging.info.
streamtype (str): display string denoting the stream the message comes
from, e.g. STDERR. This will be visible in the logs.
Returns:
None.
"""
def __init__(self, logfunc: Callable, streamtype: str) -> None:
self.logfunc = logfunc
self.streamtype = streamtype
self._msg = ""
def write(self, msg: str) -> None:
"""Takes the message coming from the stream and logs it without
newlines for better formatting.
Args:
msg (str): stream message; this should never need to be called
explicitly since it overrides sys.<stream>.write.
Returns:
None.
"""
self._msg = self._msg + msg
while "\n" in self._msg:
pos = self._msg.find("\n")
self.logfunc(f"({self.streamtype}) {self._msg[:pos]}")
self._msg = self._msg[pos+1:]
def flush(self) -> None:
"""Dummy flush method put here in case some extra flushing logic
is ever needed.
Args:
None.
Returns:
None.
"""
pass
if __name__ == "__main__":
log_filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
log_filepath = os.path.join("logs", log_filename)
logging.basicConfig(
level=logging.INFO,
filename=log_filepath,
filemode="w",
format="[%(levelname)s|%(filename)s|L%(lineno)s] %(asctime)s: %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z"
)
sys.stdout = StreamLogger(logging.info, "STDOUT")
sys.stderr = StreamLogger(logging.error, "STDERR")
root = gui.App()
root.mainloop()