-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.py
178 lines (146 loc) · 6.17 KB
/
debug.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import datetime
import threading
import inspect
import logging
import socket
import pudb
# pfmisc local dependencies
from pfmisc.message import Message
from pfmisc._colors import Colors
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s')
class debug(object):
"""
A simple class that provides some helper debug functions. Mostly
printing function/thread names and checking verbosity level
before printing.
"""
def log(self, *args):
"""
get/set the log object.
Caller can further manipulate the log object with object-specific
calls.
"""
if len(args):
self._log = args[0]
else:
return self._log
def name(self, *args):
"""
get/set the descriptive name text of this object.
"""
if len(args):
self.__name = args[0]
else:
return self.__name
def __init__(self, **kwargs):
"""
Constructor
"""
self.verbosity = 1
self.level = 1
self.b_colorize = True
self.b_useDebug = False
self.str_debugDirFile = '/tmp'
self.within = 'debug'
self.hostnamecol = 15
self.methodcol = 45
self.b_syslog = True
for k, v in kwargs.items():
if k == 'verbosity': self.verbosity = v
if k == 'level': self.level = v
if k == 'debugToFile': self.b_useDebug = v
if k == 'debugFile': self.str_debugDirFile = v
if k == 'within': self.within = v
if k == 'colorize': self.b_colorize = v
if k == 'hostnamecol': self.hostnamecol = int(v)
if k == 'methodcol': self.methodcol = int(v)
if k == 'syslog': self.b_syslog = bool(v)
if self.b_useDebug:
str_debugDir = os.path.dirname(self.str_debugDirFile)
str_debugName = os.path.basename(self.str_debugDirFile)
if not os.path.exists(str_debugDir):
os.makedirs(str_debugDir)
self.str_debugFile = '%s/%s' % (str_debugDir, str_debugName)
self.debug = Message(logTo = self.str_debugFile)
self.debug._b_syslog = False
self.debug._b_flushNewLine = True
self._log = Message()
self._log._b_syslog = True
self.__name = "pfmisc"
self.str_hostname = socket.gethostname()
def __call__(self, *args, **kwargs):
self.qprint(*args, **kwargs)
def qprint(self, msg, **kwargs):
str_teeFile = ''
str_teeMode = 'w+'
str_comms = "normal"
self.level = 1
self.msg = ""
stackDepth = 1
b_syslog = self.b_syslog
b_end = False
b_colorize = self.b_colorize
methodcol = self.methodcol
end = ""
for k, v in kwargs.items():
if k == 'level' : self.level = v
if k == 'msg' : self.msg = v
if k == 'comms' : str_comms = v
if k == 'teeFile' : str_teeFile = v
if k == 'teeMode' : str_teeMode = v
if k == 'stackDepth': stackDepth = v
if k == 'methodcol' : methodcol = int(v)
if k == 'syslog' : b_syslog = bool(v)
if k == 'colorize' : b_colorize = bool(v)
if k == 'end' :
b_end = True
end = v
if msg != None:
self.msg = msg
if self.b_useDebug:
write = self.debug
else:
write = lambda *m, **k: print(*m, **k, flush=True)
if len(str_teeFile):
tf = open(str_teeFile, str_teeMode)
stack = inspect.stack()
str_callerFile = os.path.split(stack[1][1])[1]
str_callerMethod = inspect.stack()[stackDepth][3]
if self.level <= self.verbosity:
if b_syslog:
if b_colorize: write(Colors.CYAN, end="")
write('%s' %
datetime.datetime.now().replace(microsecond=0)
+ " │ ", end="")
if b_colorize: write(Colors.LIGHT_CYAN, end="")
write('%*s │ ' % (
self.hostnamecol, self.str_hostname
), end="")
if b_colorize: write(Colors.LIGHT_BLUE, end="")
write('%*s' % (
methodcol, str_callerFile + ':' +
self.within + "." + str_callerMethod + '()') +
' │ ', end="")
if b_colorize:
if str_comms == 'normal': write(Colors.WHITE, end="")
if str_comms == 'status': write(Colors.PURPLE, end="")
if str_comms == 'error': write(Colors.RED, end="")
if str_comms == "tx": write(Colors.YELLOW, end="")
if str_comms == "rx": write(Colors.GREEN, end="")
if str_comms == "tx": write("\n---->")
if str_comms == "rx": write("\n<----")
if b_end:
write(msg, end = end )
else:
write(msg)
if len(str_teeFile):
tf.write(msg)
tf.close()
if not self.b_colorize:
if str_comms == "tx": write(Colors.YELLOW, end="")
if str_comms == "rx": write(Colors.GREEN, end="")
if str_comms == "tx": write("---->")
if str_comms == "rx": write("<----")
if b_colorize: write(Colors.NO_COLOUR, end="")