From 24fef31b14b89198185e5edc39ab7e6a7ef2e9ba Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 31 Jul 2025 17:27:38 -0400 Subject: Add log_for_exception family of APIs to the Python API --- python/log.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'python/log.py') 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. -- cgit v1.3.1