-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
50 lines (37 loc) · 983 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
38
39
40
41
42
43
44
45
46
47
48
49
50
from enum import Flag, auto
class LogLevels(Flag):
NONE = 0
ERROR = auto()
WARN = auto()
INFO = auto()
OPERATIONS = auto()
log_level = LogLevels.INFO | LogLevels.WARN | LogLevels.ERROR
def set_log_level(lv):
global log_level
log_level = lv
def add_log_level(lv):
global log_level
log_level |= lv
def info(s, *args):
if log_level & LogLevels.INFO:
print(s.format(*args))
def warn(s, *args):
if log_level & LogLevels.WARN:
print(s.format(*args))
def error(s, *args):
if log_level & LogLevels.ERROR:
print(s.format(*args))
def log_op(s, *args, **kwargs):
if log_level & LogLevels.OPERATIONS:
print(s.format(*args))
def make_table(rows):
longest = 0
for k, v in rows:
longest = max(longest, len(k))
s = ""
for k, v in rows:
if s != "":
s += "\n"
f = "{:<" + str(longest) + "} = {}"
s += (" . " + f).format(k, str(v))
return s