From e5b6299b9cf962d4c913c252dfcc0ba907926cd6 Mon Sep 17 00:00:00 2001 From: Mohamed Zarzoura Date: Thu, 9 Jul 2026 23:13:45 +0300 Subject: [PATCH 1/2] fix(logging): remove library stdout prints Refs: issue #141 https://github.com/bluerobotics/ping-python/issues/141 --- brping/pingmessage.py | 23 +++++++++++++++-------- generate/templates/device.py.in | 22 +++++++++++++++------- generate/templates/omniscan3d.py.in | 13 +++++++------ generate/templates/omniscan450.py.in | 13 +++++++------ generate/templates/s500.py.in | 13 +++++++------ generate/templates/surveyor240.py.in | 13 +++++++------ 6 files changed, 58 insertions(+), 39 deletions(-) diff --git a/brping/pingmessage.py b/brping/pingmessage.py index 6fbee97..6e88f86 100644 --- a/brping/pingmessage.py +++ b/brping/pingmessage.py @@ -3,8 +3,11 @@ # PingMessage.py # Python implementation of the Blue Robotics 'Ping' binary message protocol +import logging import struct from brping import definitions + +logger = logging.getLogger(__name__) payload_dict = definitions.payload_dict_all asciiMsgs = [definitions.COMMON_NACK, definitions.COMMON_ASCII_TEXT] variable_msgs = [ @@ -133,9 +136,8 @@ def __init__(self, msg_id=0, msg_data=None, payload_dict=None): self.payload_format = self.get_payload_format() # TODO handle better here, and catch Constructor 1 also - except KeyError as e: - print("message id not recognized: %d" % self.message_id, msg_data) - raise e + except KeyError: + raise ## Pack object attributes into self.msg_data (bytearray) # @return self.msg_data @@ -182,7 +184,7 @@ def unpack_msg_data(self, msg_data): try: self.name = self.payload_dict[self.message_id]["name"] except KeyError: - print("Unknown message: ", self.message_id) + logger.debug("Unknown message: %s", self.message_id) return False ## The field names of this message @@ -196,10 +198,15 @@ def unpack_msg_data(self, msg_data): try: payload = struct.unpack(PingMessage.endianess + self.payload_format, self.msg_data[PingMessage.headerLength:PingMessage.headerLength + self.payload_length]) except Exception as e: - print("error unpacking payload: %s" % e) - print("msg_data: %s, header: %s" % (msg_data, header)) - print("format: %s, buf: %s" % (PingMessage.endianess + self.payload_format, self.msg_data[PingMessage.headerLength:PingMessage.headerLength + self.payload_length])) - print(self.payload_format) + logger.debug( + "Error unpacking payload: %s; msg_data=%s; header=%s; format=%s; buf=%s", + e, + msg_data, + header, + PingMessage.endianess + self.payload_format, + self.msg_data[PingMessage.headerLength:PingMessage.headerLength + self.payload_length], + exc_info=True, + ) else: # only use payload if didn't raise exception for i, attr in enumerate(self.payload_field_names): try: diff --git a/generate/templates/device.py.in b/generate/templates/device.py.in index 5a143d5..f357478 100644 --- a/generate/templates/device.py.in +++ b/generate/templates/device.py.in @@ -11,10 +11,13 @@ from brping import definitions from brping import pingmessage from collections import deque +import logging import serial import socket import time +logger = logging.getLogger(__name__) + class PingDevice(object): {% for field in all_fields|sort %} _{{field}} = None @@ -47,11 +50,10 @@ class PingDevice(object): # def connect_serial(self, device_name: str, baudrate: int =115200): if device_name is None: - print("Device name is required") - return + raise ValueError("Device name is required") try: - print("Opening %s at %d bps" % (device_name, baudrate)) + logger.info("Opening %s at %d bps", device_name, baudrate) ## Serial object for device communication # write_timeout fixes it getting stuck forever atempting to write to @@ -62,7 +64,7 @@ class PingDevice(object): try: self.iodev.set_low_latency_mode(True) except Exception as exception: - print("Failed to set low latency mode: {0}".format(exception)) + logger.debug("Failed to set low latency mode: %s", exception, exc_info=True) self.iodev.send_break() time.sleep(0.001) self.iodev.write("U".encode("ascii")) @@ -82,7 +84,7 @@ class PingDevice(object): self.server_address = (host, port) try: - print("Opening %s:%d" % self.server_address) + logger.info("Opening %s:%d", *self.server_address) ## Serial object for device communication self.iodev = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.iodev.connect(self.server_address) @@ -198,14 +200,20 @@ class PingDevice(object): self._dst_device_id = msg.dst_device_id if not hasattr(msg, "payload_field_names"): - print(f"Unrecognized message: {msg.message_id}") + logger.debug("Unrecognized message: %s", msg.message_id) return False try: for attr in msg.payload_field_names: setattr(self, "_" + attr, getattr(msg, attr)) except AttributeError as e: - print("attribute error while handling msg %d (%s): %s" % (msg.message_id, msg.name, msg.msg_data)) + logger.debug( + "Attribute error while handling msg %d (%s): %s", + msg.message_id, + msg.name, + msg.msg_data, + exc_info=True, + ) return False return True diff --git a/generate/templates/omniscan3d.py.in b/generate/templates/omniscan3d.py.in index f9590e9..9215b87 100644 --- a/generate/templates/omniscan3d.py.in +++ b/generate/templates/omniscan3d.py.in @@ -9,6 +9,7 @@ from brping import definitions from brping import PingDevice from brping import pingmessage +import logging import math import time import struct @@ -23,6 +24,7 @@ import sys import platform MAX_LOG_SIZE_MB = 500 +logger = logging.getLogger(__name__) class Omniscan3D(PingDevice): def __init__(self, logging = False, log_directory = None): @@ -253,7 +255,7 @@ class Omniscan3D(PingDevice): self.logging = True self.bytes_written = 0 - print(f"Logging to {self.current_log}") + logger.info("Logging to %s", self.current_log) self.write_data(self.build_metadata_packet()) @@ -271,11 +273,11 @@ class Omniscan3D(PingDevice): self.bytes_written += len(msg.msg_data) except (OSError, IOError) as e: - print(f"[LOGGING ERROR] Failed to write to log file {self.current_log}: {e}") + logger.exception("Failed to write to log file %s", self.current_log) self.stop_logging() except Exception as e: - print(f"[LOGGING ERROR] Unexpected error: {e}") + logger.exception("Unexpected error while writing to log file %s", self.current_log) self.stop_logging() # Override wait_message to also handle point set requests from Omniscan3D and for creating atof_t data @@ -308,14 +310,13 @@ class Omniscan3D(PingDevice): self.server_address = (host, port) try: - print("Opening %s:%d" % self.server_address) + logger.info("Opening %s:%d", *self.server_address) self.iodev = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.iodev.settimeout(timeout) self.iodev.connect(self.server_address) self.iodev.setblocking(0) except socket.timeout: - print("Unable to connect to device") raise Exception("Connection timed out after {0} seconds".format(timeout)) except Exception as exception: raise Exception("Failed to open the given TCP port: {0}".format(exception)) @@ -416,4 +417,4 @@ if __name__ == "__main__": try: p.iodev.close() except Exception as e: - print(f"Failed to close socket: {e}") \ No newline at end of file + print(f"Failed to close socket: {e}") diff --git a/generate/templates/omniscan450.py.in b/generate/templates/omniscan450.py.in index b327902..916dcb8 100644 --- a/generate/templates/omniscan450.py.in +++ b/generate/templates/omniscan450.py.in @@ -11,6 +11,7 @@ from brping import definitions from brping import PingDevice from brping import pingmessage +import logging import time import math import struct @@ -25,6 +26,7 @@ import sys import platform MAX_LOG_SIZE_MB = 500 +logger = logging.getLogger(__name__) class Omniscan450(PingDevice): def __init__(self, logging = False, log_directory = None): @@ -262,7 +264,7 @@ class Omniscan450(PingDevice): self.logging = True self.bytes_written = 0 - print(f"Logging to {self.current_log}") + logger.info("Logging to %s", self.current_log) self.write_data(self.build_metadata_packet()) @@ -280,11 +282,11 @@ class Omniscan450(PingDevice): self.bytes_written += len(msg.msg_data) except (OSError, IOError) as e: - print(f"[LOGGING ERROR] Failed to write to log file {self.current_log}: {e}") + logger.exception("Failed to write to log file %s", self.current_log) self.stop_logging() except Exception as e: - print(f"[LOGGING ERROR] Unexpected error: {e}") + logger.exception("Unexpected error while writing to log file %s", self.current_log) self.stop_logging() # Override wait_message to format power results before returning @@ -317,14 +319,13 @@ class Omniscan450(PingDevice): self.server_address = (host, port) try: - print("Opening %s:%d" % self.server_address) + logger.info("Opening %s:%d", *self.server_address) self.iodev = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.iodev.settimeout(timeout) self.iodev.connect(self.server_address) self.iodev.setblocking(0) except socket.timeout: - print("Unable to connect to device") raise Exception("Connection timed out after {0} seconds".format(timeout)) except Exception as exception: raise Exception("Failed to open the given TCP port: {0}".format(exception)) @@ -388,4 +389,4 @@ if __name__ == "__main__": try: p.iodev.close() except Exception as e: - print(f"Failed to close socket: {e}") \ No newline at end of file + print(f"Failed to close socket: {e}") diff --git a/generate/templates/s500.py.in b/generate/templates/s500.py.in index ba98c8d..bedd15a 100644 --- a/generate/templates/s500.py.in +++ b/generate/templates/s500.py.in @@ -11,6 +11,7 @@ from brping import definitions from brping import PingDevice from brping import pingmessage +import logging import time import struct import socket @@ -23,6 +24,7 @@ import sys import platform MAX_LOG_SIZE_MB = 500 +logger = logging.getLogger(__name__) class S500(PingDevice): def __init__(self, logging = False, log_directory = None): @@ -121,14 +123,13 @@ class S500(PingDevice): self.server_address = (host, port) try: - print("Opening %s:%d" % self.server_address) + logger.info("Opening %s:%d", *self.server_address) self.iodev = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.iodev.settimeout(timeout) self.iodev.connect(self.server_address) self.iodev.setblocking(0) except socket.timeout: - print("Unable to connect to device") raise Exception("Connection timed out after {0} seconds".format(timeout)) except Exception as exception: raise Exception("Failed to open the given TCP port: {0}".format(exception)) @@ -296,7 +297,7 @@ class S500(PingDevice): self.logging = True self.bytes_written = 0 - print(f"Logging to {self.current_log}") + logger.info("Logging to %s", self.current_log) self.write_data(self.build_metadata_packet()) @@ -314,11 +315,11 @@ class S500(PingDevice): self.bytes_written += len(msg.msg_data) except (OSError, IOError) as e: - print(f"[LOGGING ERROR] Failed to write to log file {self.current_log}: {e}") + logger.exception("Failed to write to log file %s", self.current_log) self.stop_logging() except Exception as e: - print(f"[LOGGING ERROR] Unexpected error: {e}") + logger.exception("Unexpected error while writing to log file %s", self.current_log) self.stop_logging() # Override wait_message to format power results before returning @@ -367,4 +368,4 @@ if __name__ == "__main__": try: p.iodev.close() except Exception as e: - print(f"Failed to close socket: {e}") \ No newline at end of file + print(f"Failed to close socket: {e}") diff --git a/generate/templates/surveyor240.py.in b/generate/templates/surveyor240.py.in index e30f60a..69237ba 100644 --- a/generate/templates/surveyor240.py.in +++ b/generate/templates/surveyor240.py.in @@ -11,6 +11,7 @@ from brping import definitions from brping import PingDevice from brping import pingmessage +import logging import math import time import struct @@ -25,6 +26,7 @@ import sys import platform MAX_LOG_SIZE_MB = 500 +logger = logging.getLogger(__name__) class Surveyor240(PingDevice): def __init__(self, logging = False, log_directory = None): @@ -254,7 +256,7 @@ class Surveyor240(PingDevice): self.logging = True self.bytes_written = 0 - print(f"Logging to {self.current_log}") + logger.info("Logging to %s", self.current_log) self.write_data(self.build_metadata_packet()) @@ -272,11 +274,11 @@ class Surveyor240(PingDevice): self.bytes_written += len(msg.msg_data) except (OSError, IOError) as e: - print(f"[LOGGING ERROR] Failed to write to log file {self.current_log}: {e}") + logger.exception("Failed to write to log file %s", self.current_log) self.stop_logging() except Exception as e: - print(f"[LOGGING ERROR] Unexpected error: {e}") + logger.exception("Unexpected error while writing to log file %s", self.current_log) self.stop_logging() # Override handle_message to respond to a UTC request from Surveyor @@ -327,14 +329,13 @@ class Surveyor240(PingDevice): self.server_address = (host, port) try: - print("Opening %s:%d" % self.server_address) + logger.info("Opening %s:%d", *self.server_address) self.iodev = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.iodev.settimeout(timeout) self.iodev.connect(self.server_address) self.iodev.setblocking(0) except socket.timeout: - print("Unable to connect to device") raise Exception("Connection timed out after {0} seconds".format(timeout)) except Exception as exception: raise Exception("Failed to open the given TCP port: {0}".format(exception)) @@ -434,4 +435,4 @@ if __name__ == "__main__": try: p.iodev.close() except Exception as e: - print(f"Failed to close socket: {e}") \ No newline at end of file + print(f"Failed to close socket: {e}") From c0c8964a423b4337c2d9956b79ad0055a8a4d7fb Mon Sep 17 00:00:00 2001 From: Mohamed Zarzoura Date: Thu, 9 Jul 2026 23:39:08 +0300 Subject: [PATCH 2/2] chore: ignore generated omniscan3d module --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 92a300c..8bf1566 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ brping/ping360.py brping/s500.py brping/surveyor240.py brping/omniscan450.py +brping/omniscan3d.py doc/xml examples/logs