Skip to content

Commit 24fef31

Browse files
committed
Add log_for_exception family of APIs to the Python API
1 parent afccc4b commit 24fef31

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

python/log.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from . import _binaryninjacore as core
2525
from .enums import LogLevel
2626
import threading
27+
import traceback
2728

2829
_output_to_log = False
2930

@@ -149,6 +150,89 @@ def log_alert(text: Any, logger: str = ""):
149150
core.BNLogString(0, LogLevel.AlertLog, logger, threading.current_thread().ident, text)
150151

151152

153+
def log_for_exception(level: LogLevel, text: Any, logger: str = "", session: int = 0):
154+
"""
155+
``log_for_exception`` writes messages to the log console for the given log level, including a stack trace for the current exception.
156+
157+
============ ======== =======================================================================
158+
LogLevelName LogLevel Description
159+
============ ======== =======================================================================
160+
DebugLog 0 Logs debugging information messages to the console.
161+
InfoLog 1 Logs general information messages to the console.
162+
WarningLog 2 Logs message to console with **Warning** icon.
163+
ErrorLog 3 Logs message to console with **Error** icon, focusing the error console.
164+
AlertLog 4 Logs message to pop up window.
165+
============ ======== =======================================================================
166+
167+
:param LogLevel level: Log level to use
168+
:param str text: message to print
169+
:rtype: None
170+
"""
171+
if not isinstance(text, str):
172+
text = str(text)
173+
core.BNLogStringWithStackTrace(session, level, logger, threading.current_thread().ident, traceback.format_exc(), text)
174+
175+
176+
def log_debug_for_exception(text: Any, logger: str = ""):
177+
"""
178+
``log_debug_for_exception`` Logs debugging information messages to the console, including a stack trace for the current exception.
179+
180+
:param str text: message to print
181+
:rtype: None
182+
"""
183+
if not isinstance(text, str):
184+
text = str(text)
185+
core.BNLogStringWithStackTrace(0, LogLevel.DebugLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
186+
187+
188+
def log_info_for_exception(text: Any, logger: str = ""):
189+
"""
190+
``log_info_for_exception`` Logs general information messages to the console, including a stack trace for the current exception.
191+
192+
:param str text: message to print
193+
:rtype: None
194+
"""
195+
if not isinstance(text, str):
196+
text = str(text)
197+
core.BNLogStringWithStackTrace(0, LogLevel.InfoLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
198+
199+
200+
def log_warn_for_exception(text: Any, logger: str = ""):
201+
"""
202+
``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.
203+
204+
:param str text: message to print
205+
:rtype: None
206+
"""
207+
if not isinstance(text, str):
208+
text = str(text)
209+
core.BNLogStringWithStackTrace(0, LogLevel.WarningLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
210+
211+
212+
def log_error_for_exception(text: Any, logger: str = ""):
213+
"""
214+
``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.
215+
216+
:param str text: message to print
217+
:rtype: None
218+
"""
219+
if not isinstance(text, str):
220+
text = str(text)
221+
core.BNLogStringWithStackTrace(0, LogLevel.ErrorLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
222+
223+
224+
def log_alert_for_exception(text: Any, logger: str = ""):
225+
"""
226+
``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.
227+
228+
:param str text: message to print
229+
:rtype: None
230+
"""
231+
if not isinstance(text, str):
232+
text = str(text)
233+
core.BNLogStringWithStackTrace(0, LogLevel.AlertLog, logger, threading.current_thread().ident, traceback.format_exc(), text)
234+
235+
152236
def log_to_stdout(min_level: LogLevel = LogLevel.InfoLog):
153237
"""
154238
``log_to_stdout`` redirects minimum log level to standard out.

0 commit comments

Comments
 (0)