-
Notifications
You must be signed in to change notification settings - Fork 1
/
thlog.py
83 lines (70 loc) · 2.67 KB
/
thlog.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
73
74
75
76
77
78
79
80
81
82
83
# Threedle Logger: Thlogger
import polyscope as ps
import os
LOG_NONE = 0
LOG_INFO = 1
LOG_DEBUG = 3
LOG_TRACE = 5
VIZ_NONE = 7
VIZ_INFO = 10
VIZ_DEBUG = 13
VIZ_TRACE = 15
thlog_prefixes = {
LOG_INFO: "INFO "
, LOG_DEBUG: "DEBUG"
, LOG_TRACE: "TRACE"
}
class Thlogger:
def __init__(self, loglevel, vizlevel, moduleprefix, imports=[], propagate_to_imports=True):
self.loglevel = loglevel
self.vizlevel = vizlevel if os.environ.get("NO_POLYSCOPE") is None else VIZ_NONE
self.moduleprefix = moduleprefix
self.imported_thloggers = imports
self.ps_initialized = False
if propagate_to_imports:
for thlogger in self.imported_thloggers:
thlogger.loglevel = self.loglevel
thlogger.vizlevel = self.vizlevel
def set_levels(self, loglevel, vizlevel, propagate_to_imports=False):
self.loglevel = loglevel
self.vizlevel = vizlevel
if propagate_to_imports:
for thlogger in self.imported_thloggers:
thlogger.loglevel = self.loglevel
thlogger.vizlevel = self.vizlevel
def init_polyscope(self, propagate_to_imports=True):
if self.vizlevel == VIZ_NONE:
# when NO_POLYSCOPE=1 env var is used, that often also means
# polyscope is not available so don't init at all
return
if not self.ps_initialized:
ps.init()
self.ps_initialized = True
# broadcast to other thloggers that ps has been initialized
for thlogger in self.imported_thloggers:
thlogger.ps_initialized = True
def log(self, loglevel, message):
if self.loglevel >= loglevel:
if loglevel not in thlog_prefixes:
prefix = " ! "
else:
prefix = thlog_prefixes[loglevel]
print(f"[{self.moduleprefix} | {prefix}] {message}")
def info(self, msg):
return self.log(LOG_INFO, msg)
def debug(self, msg):
return self.log(LOG_DEBUG, msg)
def trace(self, msg):
return self.log(LOG_TRACE, msg)
def __call__(self, *args):
return self.log(*args)
def do(self, vizlevel, fn, needs_polyscope = False):
if self.vizlevel >= vizlevel:
if (not needs_polyscope) or \
(needs_polyscope and self.ps_initialized):
return fn()
def err(self, message):
print(f"[{self.moduleprefix} | ERROR] {message}")
def guard(self, vizlevel, needs_polyscope = False):
return (self.vizlevel >= vizlevel) and \
((not needs_polyscope) or (needs_polyscope and self.ps_initialized))