-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
77 lines (57 loc) · 1.89 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
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
# log.py -- Functions and classes for logging behavior of a FARG model
from contextlib import contextmanager
from util import as_iter
logging = set() # LoggingObjects now active, i.e. logging
logging_objects = set() # All LoggingObjects, regardless of whether active
suppressing = False # Is logging currently being suppressed?
# TODO Make it so you don't have to call this as a function; just:
# with SuppressLogging:
@contextmanager
def SuppressLogging():
global suppressing
old_suppressing = suppressing
try:
suppressing = True
yield True
finally:
suppressing = old_suppressing
class LoggingObject:
def __init__(self):
logging_objects.add(self)
def __call__(self, s):
if self:
print(s)
def start_logging(self):
logging.add(self)
def is_logging(self):
global suppressing
return (not suppressing) and (self in logging)
def stop_logging(self):
logging.discard(self)
def __bool__(self):
return self.is_logging()
ShowActiveNodes = LoggingObject()
ShowActiveNodesCollected = LoggingObject()
ShowActionList = LoggingObject()
ShowActionsChosen = LoggingObject()
ShowActionsPerformed = LoggingObject()
ShowPrimitives = LoggingObject()
ShowResponseList = LoggingObject()
ShowResponseResults = LoggingObject()
ShowOperandCandidates = LoggingObject()
ShowAnnotations = LoggingObject()
ShowIsMatch = LoggingObject()
ShowResults = LoggingObject()
# TODO Allow LoggingObjects to be or'ed together, e.g.
# (ShowPrimitives or ShowActionsPerformed)('something happened')
logging.add(ShowResults)
def start_logging(os):
for o in as_iter(os):
logging.add(o)
def stop_all_logging():
logging.clear()
def log_all():
logging.update(logging_objects)
#OAOO Should this or LoggingObject.is_logging() be authoritative?
def is_logging(os):
return any(o in logging for o in as_iter(os))