summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRusty Wagner <rusty.wagner@gmail.com>2025-07-31 17:27:38 -0400
committerRusty Wagner <rusty.wagner@gmail.com>2025-08-01 14:35:05 -0400
commit24fef31b14b89198185e5edc39ab7e6a7ef2e9ba (patch)
tree192469f8009e45b843dabf3a3c3b6860d838af10 /python
parentafccc4b8e0a698b9e9ae39865162c671380e876a (diff)
Add log_for_exception family of APIs to the Python API
Diffstat (limited to 'python')
-rw-r--r--python/log.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/python/log.py b/python/log.py
index 295d428e..25b692ac 100644
--- a/python/log.py
+++ b/python/log.py
@@ -24,6 +24,7 @@ from typing import Optional, Union, Any
from . import _binaryninjacore as core
from .enums import LogLevel
import threading
+import traceback
_output_to_log = False
@@ -149,6 +150,89 @@ def log_alert(text: Any, logger: str = ""):
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_to_stdout(min_level: LogLevel = LogLevel.InfoLog):
"""
``log_to_stdout`` redirects minimum log level to standard out.