forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
425 lines (323 loc) · 14.8 KB
/
log.py
File metadata and controls
425 lines (323 loc) · 14.8 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Copyright (c) 2015-2025 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from typing import Optional, Union, Any
# Binary Ninja components
from . import _binaryninjacore as core
from .enums import LogLevel
import threading
import traceback
_output_to_log = False
def redirect_output_to_log():
global _output_to_log
_output_to_log = True
def is_output_redirected_to_log():
global _output_to_log
return _output_to_log
def log(level: LogLevel, text: Any, logger: str = "", session: int = 0):
"""
``log`` writes messages to the log console for the given log level.
============ ======== =======================================================================
LogLevelName LogLevel Description
============ ======== =======================================================================
DebugLog 0 Logs debugging information messages to the console.
InfoLog 1 Logs general information messages to the console.
WarningLog 2 Logs message to console with **Warning** icon.
ErrorLog 3 Logs message to console with **Error** icon, focusing the error console.
AlertLog 4 Logs message to pop up window.
============ ======== =======================================================================
:param LogLevel level: Log level to use
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(session, level, logger, threading.current_thread().ident, text)
def log_debug(text: Any, logger: str = ""):
"""
``log_debug`` Logs debugging information messages to the console.
:param str text: message to print
:rtype: None
:Example:
>>> log_to_stdout(LogLevel.DebugLog)
>>> log_debug("Hotdogs!")
Hotdogs!
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(0, LogLevel.DebugLog, logger, threading.current_thread().ident, text)
def log_info(text: Any, logger: str = ""):
"""
``log_info`` Logs general information messages to the console.
:param str text: message to print
:rtype: None
:Example:
>>> log_info("Saucisson!")
Saucisson!
>>>
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(0, LogLevel.InfoLog, logger, threading.current_thread().ident, text)
def log_warn(text: Any, logger: str = ""):
"""
``log_warn`` Logs message to console, if run through the GUI it logs with **Warning** icon.
:param str text: message to print
:rtype: None
:Example:
>>> log_to_stdout(LogLevel.DebugLog)
>>> log_warn("Chilidogs!")
Chilidogs!
>>>
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(0, LogLevel.WarningLog, logger, threading.current_thread().ident, text)
def log_error(text: Any, logger: str = ""):
"""
``log_error`` Logs message to console, if run through the GUI it logs with **Error** icon, focusing the error console.
:param str text: message to print
:rtype: None
:Example:
>>> log_to_stdout(LogLevel.DebugLog)
>>> log_error("Spanferkel!")
Spanferkel!
>>>
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(0, LogLevel.ErrorLog, logger, threading.current_thread().ident, text)
def log_alert(text: Any, logger: str = ""):
"""
``log_alert`` Logs message console and to a pop up window if run through the GUI.
:param str text: message to print
:rtype: None
:Example:
>>> log_to_stdout(LogLevel.DebugLog)
>>> log_alert("Kielbasa!")
Kielbasa!
>>>
"""
if not isinstance(text, str):
text = str(text)
core.BNLogString(0, LogLevel.AlertLog, logger, threading.current_thread().ident, text)
def log_for_exception(level: LogLevel, text: Any, logger: str = "", session: int = 0):
"""
``log_for_exception`` writes messages to the log console for the given log level, including a stack trace for the current exception.
============ ======== =======================================================================
LogLevelName LogLevel Description
============ ======== =======================================================================
DebugLog 0 Logs debugging information messages to the console.
InfoLog 1 Logs general information messages to the console.
WarningLog 2 Logs message to console with **Warning** icon.
ErrorLog 3 Logs message to console with **Error** icon, focusing the error console.
AlertLog 4 Logs message to pop up window.
============ ======== =======================================================================
:param LogLevel level: Log level to use
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(session, level, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_debug_for_exception(text: Any, logger: str = ""):
"""
``log_debug_for_exception`` Logs debugging information messages to the console, including a stack trace for the current exception.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.DebugLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_info_for_exception(text: Any, logger: str = ""):
"""
``log_info_for_exception`` Logs general information messages to the console, including a stack trace for the current exception.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.InfoLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_warn_for_exception(text: Any, logger: str = ""):
"""
``log_warn_for_exception`` Logs message to console, including a stack trace for the current exception. When run through the GUI it logs with **Warning** icon.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.WarningLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_error_for_exception(text: Any, logger: str = ""):
"""
``log_error_for_exception`` Logs message to console, including a stack trace for the current exception. When run through the GUI it logs with **Error** icon, focusing the error console.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.ErrorLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_alert_for_exception(text: Any, logger: str = ""):
"""
``log_alert_for_exception`` Logs message console, including a stack trace for the current exception. A pop up window is created if run through the GUI.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.AlertLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
def log_with_traceback(level: LogLevel, text: Any, logger: str = "", session: int = 0):
"""
``log_with_traceback`` writes messages to the log console for the given log level, including the current stack trace.
============ ======== =======================================================================
LogLevelName LogLevel Description
============ ======== =======================================================================
DebugLog 0 Logs debugging information messages to the console.
InfoLog 1 Logs general information messages to the console.
WarningLog 2 Logs message to console with **Warning** icon.
ErrorLog 3 Logs message to console with **Error** icon, focusing the error console.
AlertLog 4 Logs message to pop up window.
============ ======== =======================================================================
:param LogLevel level: Log level to use
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(session, level, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_debug_with_traceback(text: Any, logger: str = ""):
"""
``log_debug_with_traceback`` Logs debugging information messages to the console, including the current stack trace.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.DebugLog, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_info_with_traceback(text: Any, logger: str = ""):
"""
``log_info_with_traceback`` Logs general information messages to the console, including the current stack trace.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.InfoLog, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_warn_with_traceback(text: Any, logger: str = ""):
"""
``log_warn_with_traceback`` Logs message to console, including the current stack trace. When run through the GUI it logs with **Warning** icon.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.WarningLog, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_error_with_traceback(text: Any, logger: str = ""):
"""
``log_error_with_traceback`` Logs message to console, including the current stack trace. When run through the GUI it logs with **Error** icon, focusing the error console.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.ErrorLog, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_alert_with_traceback(text: Any, logger: str = ""):
"""
``log_alert_with_traceback`` Logs message console, including the current stack trace. A pop up window is created if run through the GUI.
:param str text: message to print
:rtype: None
"""
if not isinstance(text, str):
text = str(text)
core.BNLogStringWithStackTrace(0, LogLevel.AlertLog, logger, threading.current_thread().ident, ''.join(traceback.format_stack()), text)
def log_to_stdout(min_level: LogLevel = LogLevel.InfoLog):
"""
``log_to_stdout`` redirects minimum log level to standard out.
:param enums.LogLevel min_level: minimum level to log to
:rtype: None
:Example:
>>> log_debug("Hotdogs!")
>>> log_to_stdout(LogLevel.DebugLog)
>>> log_debug("Hotdogs!")
Hotdogs!
>>>
"""
core.BNLogToStdout(min_level)
def log_to_stderr(min_level: LogLevel):
"""
``log_to_stderr`` redirects minimum log level to standard error.
:param enums.LogLevel min_level: minimum level to log to
:rtype: None
"""
core.BNLogToStderr(min_level)
def log_to_file(min_level: LogLevel, path: str, append: bool=False):
"""
``log_to_file`` redirects minimum log level to a file named ``path``, optionally appending rather than overwriting.
:param enums.Log_Level min_level: minimum level to log
:param str path: path to log to
:param bool append: optional flag for specifying appending. True = append, False = overwrite.
:rtype: None
"""
core.BNLogToFile(min_level, str(path), append)
def close_logs():
"""
``close_logs`` close all log files.
:rtype: None
"""
core.BNCloseLogs()
class Logger:
def __init__(self, session_id: int, logger_name: str):
self.session_id = session_id
self.logger_name = logger_name
self.handle = core.BNLogCreateLogger(logger_name, session_id)
def log(self, level: LogLevel, message: str) -> None:
log(level, message, self.logger_name, self.session_id)
def log_debug(self, message: str) -> None:
log(LogLevel.DebugLog, message, self.logger_name, self.session_id)
def log_info(self, message: str) -> None:
log(LogLevel.InfoLog, message, self.logger_name, self.session_id)
def log_warn(self, message: str) -> None:
log(LogLevel.WarningLog, message, self.logger_name, self.session_id)
def log_error(self, message: str) -> None:
log(LogLevel.ErrorLog, message, self.logger_name, self.session_id)
def log_alert(self, message: str) -> None:
log(LogLevel.AlertLog, message, self.logger_name, self.session_id)
def log_for_exception(self, level: LogLevel, message: str) -> None:
log_for_exception(level, message, self.logger_name, self.session_id)
def log_debug_for_exception(self, message: str) -> None:
log_for_exception(LogLevel.DebugLog, message, self.logger_name, self.session_id)
def log_info_for_exception(self, message: str) -> None:
log_for_exception(LogLevel.InfoLog, message, self.logger_name, self.session_id)
def log_warn_for_exception(self, message: str) -> None:
log_for_exception(LogLevel.WarningLog, message, self.logger_name, self.session_id)
def log_error_for_exception(self, message: str) -> None:
log_for_exception(LogLevel.ErrorLog, message, self.logger_name, self.session_id)
def log_alert_for_exception(self, message: str) -> None:
log_for_exception(LogLevel.AlertLog, message, self.logger_name, self.session_id)
def log_with_traceback(self, level: LogLevel, message: str) -> None:
log_with_traceback(level, message, self.logger_name, self.session_id)
def log_debug_with_traceback(self, message: str) -> None:
log_with_traceback(LogLevel.DebugLog, message, self.logger_name, self.session_id)
def log_info_with_traceback(self, message: str) -> None:
log_with_traceback(LogLevel.InfoLog, message, self.logger_name, self.session_id)
def log_warn_with_traceback(self, message: str) -> None:
log_with_traceback(LogLevel.WarningLog, message, self.logger_name, self.session_id)
def log_error_with_traceback(self, message: str) -> None:
log_with_traceback(LogLevel.ErrorLog, message, self.logger_name, self.session_id)
def log_alert_with_traceback(self, message: str) -> None:
log_with_traceback(LogLevel.AlertLog, message, self.logger_name, self.session_id)