From 1da687cf2d8ff731551584bd12eef31df9001093 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sat, 29 Jun 2024 16:15:32 -0700 Subject: [PATCH] move @thebentern spiffy logging so it is shared with !ble log sources --- meshtastic/ble_interface.py | 12 +----------- meshtastic/mesh_interface.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/meshtastic/ble_interface.py b/meshtastic/ble_interface.py index 69cb9a3..1ebc57b 100644 --- a/meshtastic/ble_interface.py +++ b/meshtastic/ble_interface.py @@ -8,7 +8,6 @@ import time from threading import Thread from typing import Optional -import print_color # type: ignore[import-untyped] from bleak import BleakClient, BleakScanner, BLEDevice from bleak.exc import BleakDBusError, BleakError @@ -82,16 +81,7 @@ class BLEInterface(MeshInterface): async def log_radio_handler(self, _, b): # pylint: disable=C0116 log_radio = b.decode("utf-8").replace("\n", "") - if log_radio.startswith("DEBUG"): - print_color.print(log_radio, color="cyan", end=None) - elif log_radio.startswith("INFO"): - print_color.print(log_radio, color="white", end=None) - elif log_radio.startswith("WARN"): - print_color.print(log_radio, color="yellow", end=None) - elif log_radio.startswith("ERROR"): - print_color.print(log_radio, color="red", end=None) - else: - print_color.print(log_radio, end=None) + self._handleLogLine(log_radio) @staticmethod def scan() -> list[BLEDevice]: diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 38eca07..2c0ceeb 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -16,6 +16,7 @@ from typing import Any, Callable, Dict, List, Optional, Union import google.protobuf.json_format from pubsub import pub # type: ignore[import-untyped] from tabulate import tabulate +import print_color # type: ignore[import-untyped] import meshtastic.node @@ -143,8 +144,21 @@ class MeshInterface: # pylint: disable=R0902 @staticmethod def _printLogLine(line, interface): - """Print a line of log output""" - interface.debugOut.write(line + "\n") + """Print a line of log output.""" + if interface.debugOut == sys.stdout: + # this isn't quite correct (could cause false positives), but currently our formatting differs between different log representations + if "DEBUG" in line: + print_color.print(line, color="cyan", end=None) + elif "INFO" in line: + print_color.print(line, color="white", end=None) + elif "WARN" in line: + print_color.print(line, color="yellow", end=None) + elif "ERR" in line: + print_color.print(line, color="red", end=None) + else: + print_color.print(line, end=None) + else: + interface.debugOut.write(line + "\n") def _handleLogLine(self, line: str) -> None: """Handle a line of log output from the device."""