-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.py
41 lines (31 loc) · 1.11 KB
/
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
38
39
40
41
"""Log messages to stderr with a prefix that,
can be used by the host application to filter them out. """
import sys
class Logger:
"""Log messages to stderr with a prefix that,
can be used by the host application to filter them out. """
def __prefix(self, levelchar):
startlevelchar = b'\x01'
endlevelchar = b'\x02'
ret = startlevelchar + levelchar + endlevelchar
return ret.decode()
def __log(self, levelchar, error_code):
if levelchar == "":
return
if error_code == "":
return
print(self.__prefix(levelchar) + error_code +
"\n", file=sys.stderr, flush=True)
def trace(self, error_code):
self.__log(b't', error_code)
def debug(self, error_code):
self.__log(b'd', error_code)
def info(self, error_code):
self.__log(b'i', error_code)
def warning(self, error_code):
self.__log(b'w', error_code)
def error(self, error_code):
self.__log(b'e', error_code)
def progress(self, p):
progress = min(max(0, p), 1)
self.__log(b'p', str(progress))