Logger.py
3.44 KB
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
from __future__ import print_function
import sys
import os.path
import inspect
class NopLogger:
def __init__(self):
pass
def write(self, data):
pass
def flush(self):
pass
def close(self):
pass
class StdoutLogger:
def __init__(self):
pass
def write(self, data):
print(data)
def flush(self):
pass
def close(self):
pass
class FileLogger:
def __init__(self, name):
self.file = None
try:
name = os.path.abspath(name)
self.file = open(name, 'a')
except:
try:
self.file = open('formatters.log', 'a')
except:
pass
def write(self, data):
if self.file is not None:
print(data, file=self.file)
else:
print(data)
def flush(self):
if self.file is not None:
self.file.flush()
def close(self):
if self.file is not None:
self.file.close()
self.file = None
# to enable logging:
# define lldb.formatters.Logger._lldb_formatters_debug_level to any number greater than 0
# if you define it to any value greater than 1, the log will be automatically flushed after each write (slower but should make sure most of the stuff makes it to the log even if we crash)
# if you define it to any value greater than 2, the calling function's details will automatically be logged (even slower, but provides additional details)
# if you need the log to go to a file instead of on screen, define
# lldb.formatters.Logger._lldb_formatters_debug_filename to a valid
# filename
class Logger:
def __init__(self, autoflush=False, logcaller=False):
global _lldb_formatters_debug_level
global _lldb_formatters_debug_filename
self.autoflush = autoflush
want_log = False
try:
want_log = (_lldb_formatters_debug_level > 0)
except:
pass
if not (want_log):
self.impl = NopLogger()
return
want_file = False
try:
want_file = (_lldb_formatters_debug_filename is not None and _lldb_formatters_debug_filename !=
'' and _lldb_formatters_debug_filename != 0)
except:
pass
if want_file:
self.impl = FileLogger(_lldb_formatters_debug_filename)
else:
self.impl = StdoutLogger()
try:
self.autoflush = (_lldb_formatters_debug_level > 1)
except:
self.autoflush = autoflush
want_caller_info = False
try:
want_caller_info = (_lldb_formatters_debug_level > 2)
except:
pass
if want_caller_info:
self._log_caller()
def _log_caller(self):
caller = inspect.stack()[2]
try:
if caller is not None and len(caller) > 3:
self.write('Logging from function ' + str(caller))
else:
self.write(
'Caller info not available - Required caller logging not possible')
finally:
del caller # needed per Python docs to avoid keeping objects alive longer than we care
def write(self, data):
self.impl.write(data)
if self.autoflush:
self.flush()
def __rshift__(self, data):
self.write(data)
def flush(self):
self.impl.flush()
def close(self):
self.impl.close()