From f6f016a4cc4a8d502dbb1866843e616816156b44 Mon Sep 17 00:00:00 2001 From: D4MI4NX <105593627+D4MI4NX@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:32:52 +0200 Subject: [PATCH] Improved logging --- BlueDucky.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/BlueDucky.py b/BlueDucky.py index 7705ead..f206554 100644 --- a/BlueDucky.py +++ b/BlueDucky.py @@ -51,6 +51,8 @@ def setup_logging(): # Set the logging level to INFO to filter out DEBUG messages logging.basicConfig(level=logging.INFO, handlers=[handler]) +def print_error(msg): + print(f"{color.RESET}[{color.RED}!{color.RESET}] {color.RED}CRITICAL ERROR{color.RESET}:{color.RESET}", msg) class ConnectionFailureException(Exception): pass @@ -263,12 +265,10 @@ class L2CAPClient: error = True self.connected = False log.error("ERROR connecting on port %d: %s" % (self.port, ex)) - raise ConnectionFailureException(f"Connection failure on port {self.port}") if (error == True & self.port == 14): - print(f"{color.RESET}[{color.RED}!{color.RESET}] {color.RED}CRITICAL ERROR{color.RESET}: {color.RESET}Attempted Connection to {color.RED}{target_address} {color.RESET}was {color.RED}denied{color.RESET}.") + print_error(f"{color.RESET}Attempted Connection to {color.RED}{target_address} {color.RESET}was {color.RED}denied{color.RESET}.") return self.connected - - + raise ConnectionFailureException(f"Connection failure on port {self.port}") return self.connected @@ -632,20 +632,19 @@ def troubleshoot_bluetooth(): try: subprocess.run(['bluetoothctl', '--version'], check=True, stdout=subprocess.PIPE) except subprocess.CalledProcessError: - print(f"{color.RESET}[{color.RED}!{color.RESET}] {color.RED}CRITICAL{color.RESET}: {color.BLUE}bluetoothctl {color.RESET}is not installed or not working properly.") + print_error(f"{color.BLUE}bluetoothctl {color.RESET}is not installed or not working properly.") return False # Check for Bluetooth adapters result = subprocess.run(['bluetoothctl', 'list'], capture_output=True, text=True) if "Controller" not in result.stdout: - print(f"{color.RESET}[{color.RED}!{color.RESET}] {color.RED}CRITICAL{color.RESET}: No {color.BLUE}Bluetooth adapters{color.RESET} have been detected.") + print_error(f"No {color.BLUE}Bluetooth adapters{color.RESET} have been detected.") return False # List devices to see if any are connected result = subprocess.run(['bluetoothctl', 'devices'], capture_output=True, text=True) if "Device" not in result.stdout: - print(f"{color.RESET}[{color.RED}!{color.RESET}] {color.YELLOW}WARNING{color.RESET}: Your {color.BLUE}Bluetooth devices{color.RESET} might not be supported.") - time.sleep(2) + log.warning(f"{color.RESET}Your {color.BLUE}Bluetooth devices{color.RESET} might not be supported.") return True @@ -716,7 +715,7 @@ def main(): finally: # unpair the target device - command = f'echo -e "remove {target_address}\n" | bluetoothctl' + command = f'echo -e "\nremove {target_address}\n" | bluetoothctl' subprocess.run(command, shell=True) print(f"{color.BLUE}Successfully Removed device{color.RESET}: {color.BLUE}{target_address}{color.RESET}")