-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.py
55 lines (45 loc) · 1.26 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
import sys
import traceback
import logging
class Frame(object):
def __init__(self, tb):
self.tb = tb
frame = tb.tb_frame
self.locals = {}
self.locals.update(frame.f_locals)
def print_path(self):
return traceback.format_tb(self.tb, limit=1)[0]
def print_local(self):
return "\n".join(["%s=%s" % (k, self.dump_value(self.locals[k])) for k in self.locals])
def dump_value(self, v):
try:
return str(v)
except:
return "value can not serilizable"
def get_debug_detail(e, log_it=True):
"""
get bug details
:param e: Exception
:param log_it: log the error or not
:return:
"""
exc_type, exc_value, exc_traceback = sys.exc_info()
frames = []
tb = exc_traceback
frames.append(tb.tb_frame)
detail = "system error -Exception:\n%s\n\ndetail info is:\n" % e
while tb.tb_next:
tb = tb.tb_next
fm = Frame(tb)
detail += fm.print_path()
detail += "\nlocals variables:\n"
detail += fm.print_local()
detail += "\n" + "-" * 100 + "\n"
if log_it:
logging.error(detail)
return detail
if __name__ == '__main__':
try:
1 / 0
except Exception as e:
get_debug_detail(e)