From 5064f8cf424e24f31dd012873a9b7cf09f67397a Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Tue, 29 Oct 2024 16:21:37 +0100 Subject: [PATCH] FIXUP - improve output --- tools/modules/flash_utils.py | 22 +--------------------- tools/modules/serial_utils.py | 17 +++++++++-------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/tools/modules/flash_utils.py b/tools/modules/flash_utils.py index d3c5620a6..34e87e2da 100644 --- a/tools/modules/flash_utils.py +++ b/tools/modules/flash_utils.py @@ -101,28 +101,8 @@ def erase_flash(): """ print("Erasing flash...") cmd_erase = "st-flash --connect-under-reset --reset erase" - ret = subprocess.run(cmd_erase, shell=True) + ret = subprocess.run(cmd_erase, capture_output=True, text=True) if ret.returncode != 0: print(f"Erasing flash... {Fore.RED}❌{Style.RESET_ALL}") sys.exit(1) print(f"Erasing flash... {Fore.GREEN}✅{Style.RESET_ALL}") - - -def print_end_success(message: str): - """ - Print a success message in cyan with a checkmark. - - Args: - message (str): The message to print. - """ - print(f"{Fore.CYAN}{message}... ✅{Style.RESET_ALL}") - - -def print_end_failure(message: str): - """ - Print a failure message in red with a cross mark. - - Args: - message (str): The message to print. - """ - print(f"{Fore.RED}{message}... ❌{Style.RESET_ALL}") diff --git a/tools/modules/serial_utils.py b/tools/modules/serial_utils.py index c1ec5653f..c01dd346b 100644 --- a/tools/modules/serial_utils.py +++ b/tools/modules/serial_utils.py @@ -9,6 +9,7 @@ import glob +import logging import sys from time import sleep from typing import List, Optional @@ -35,13 +36,12 @@ def connect_serial(port_pattern: str) -> serial.Serial: serial_port = ports[0] if ports else port_pattern try: + logging.info(f"Connecting to {serial_port} ...") com = serial.Serial(serial_port, 115200, timeout=SERIAL_TIMEOUT) - print(f"Connected to {com.name}... {Fore.GREEN}✅{Style.RESET_ALL}") + logging.info(f"Connecting to {serial_port} ... ✅") return com except serial.serialutil.SerialException as error: - print( - f"Error connecting to {serial_port}: {error}... {Fore.RED}❌{Style.RESET_ALL}" - ) + logging.exception(f"Connecting to {serial_port} ... 💥️\n{error}") sys.exit(1) @@ -54,13 +54,14 @@ def reset_buffer(com: serial.Serial): """ print("Resetting COM buffer...") try: + logging.info(f"Resetting serial port {com.name} ...") com.reset_input_buffer() com.reset_output_buffer() com.send_break(duration=1) sleep(1) - print(f"Resetting COM buffer... {Fore.GREEN}✅{Style.RESET_ALL}") - except serial.SerialException as e: - print(f"Error resetting COM buffer: {e}... {Fore.RED}❌{Style.RESET_ALL}") + logging.info(f"Resetting serial port {com.name} ... ✅") + except serial.SerialException as error: + logging.exception(f"Resetting serial port {com.name} ... 💥️\n{error}") sys.exit(1) @@ -78,7 +79,7 @@ def read_output_serial(com: serial.Serial) -> str: data = com.readline().decode("utf-8").strip() return data except serial.SerialException as e: - print(f"Serial read error: {e}") + logging.exception(f"Serial read error: {e}") return ""