From 463ac6586bcba4b982007e31e02fe7f4627acbea Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 20 Jun 2025 09:22:05 -0500 Subject: [PATCH] fix logger handler classes, remove skip file --- CircuitPython_Logger/.circuitpython.skip | 4 -- .../code.py => aio_test/aio_handler.py} | 23 +++---- CircuitPython_Logger/aio_test/code.py | 17 ++--- .../code.py => ble_test/ble_handler.py} | 10 +-- CircuitPython_Logger/ble_test/code.py | 13 ++-- CircuitPython_Logger/file_test/code.py | 15 ++--- CircuitPython_Logger/uart_handler/code.py | 62 +++---------------- .../uart_handler/uart_handler.py | 46 ++++++++++++++ 8 files changed, 92 insertions(+), 98 deletions(-) delete mode 100644 CircuitPython_Logger/.circuitpython.skip rename CircuitPython_Logger/{aio_handler/code.py => aio_test/aio_handler.py} (63%) rename CircuitPython_Logger/{ble_handler/code.py => ble_test/ble_handler.py} (84%) create mode 100644 CircuitPython_Logger/uart_handler/uart_handler.py diff --git a/CircuitPython_Logger/.circuitpython.skip b/CircuitPython_Logger/.circuitpython.skip deleted file mode 100644 index 42c98ade8..000000000 --- a/CircuitPython_Logger/.circuitpython.skip +++ /dev/null @@ -1,4 +0,0 @@ -CircuitPython_Logger/ble_handler.py 16: Bad option value 'missing-super-argument' (bad-option-value) -CircuitPython_Logger/uart_handler.py 16: Bad option value 'missing-super-argument' (bad-option-value) -CircuitPython_Logger/file_handler.py 15: Bad option value 'missing-super-argument' (bad-option-value) -CircuitPython_Logger/aio_handler.py 15: Bad option value 'missing-super-argument' (bad-option-value) diff --git a/CircuitPython_Logger/aio_handler/code.py b/CircuitPython_Logger/aio_test/aio_handler.py similarity index 63% rename from CircuitPython_Logger/aio_handler/code.py rename to CircuitPython_Logger/aio_test/aio_handler.py index 704f5660d..4caadc0b8 100644 --- a/CircuitPython_Logger/aio_handler/code.py +++ b/CircuitPython_Logger/aio_test/aio_handler.py @@ -17,30 +17,21 @@ All text above must be included in any redistribution. """ from adafruit_portalbase import PortalBase +from adafruit_logging import Handler, NOTSET -# Example: -# -# from aio_handler import AIOHandler -# import adafruit_logging as logging -# l = logging.getLogger('aio') -# # Pass in the device object based on portal_base -# # (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter -# l.addHandler(AIOHandler('test', portal_device)) -# l.level = logging.ERROR -# l.error("test") - -from adafruit_logging import Handler class AIOHandler(Handler): - def __init__(self, name, portal_device): + def __init__(self, name, portal_device, level: int = NOTSET): """Create an instance.""" - self._log_feed_name=f"{name}-logging" + super().__init__(name) + self._log_feed_name = f"{name}-logging" if not issubclass(type(portal_device), PortalBase): - raise TypeError("portal_device must be a PortalBase or subclass of PortalBase") + raise TypeError( + "portal_device must be a PortalBase or subclass of PortalBase" + ) self._portal_device = portal_device - def emit(self, record): """Generate the message and write it to the AIO Feed. diff --git a/CircuitPython_Logger/aio_test/code.py b/CircuitPython_Logger/aio_test/code.py index faa78fa7c..f2823bb08 100644 --- a/CircuitPython_Logger/aio_test/code.py +++ b/CircuitPython_Logger/aio_test/code.py @@ -8,27 +8,28 @@ from adafruit_pyportal import PyPortal from aio_handler import AIOHandler import adafruit_logging as logging -device=PyPortal() +device = PyPortal() + +l = logging.getLogger("aio") +l.addHandler(AIOHandler("test", device)) -l = logging.getLogger('aio') -l.addHandler(AIOHandler('test', device)) def go(): while True: t = random.randint(1, 5) if t == 1: - print('debug') + print("debug") l.debug("debug message: %d", random.randint(0, 1000)) elif t == 2: - print('info') + print("info") l.info("info message: %d", random.randint(0, 1000)) elif t == 3: - print('warning') + print("warning") l.warning("warning message: %d", random.randint(0, 1000)) elif t == 4: - print('error') + print("error") l.error("error message: %d", random.randint(0, 1000)) elif t == 5: - print('critical') + print("critical") l.critical("critical message: %d", random.randint(0, 1000)) time.sleep(5.0 + (random.random() * 5.0)) diff --git a/CircuitPython_Logger/ble_handler/code.py b/CircuitPython_Logger/ble_test/ble_handler.py similarity index 84% rename from CircuitPython_Logger/ble_handler/code.py rename to CircuitPython_Logger/ble_test/ble_handler.py index 9650bc05b..b60de5a36 100644 --- a/CircuitPython_Logger/ble_handler/code.py +++ b/CircuitPython_Logger/ble_test/ble_handler.py @@ -17,17 +17,19 @@ All text above must be included in any redistribution. """ -from adafruit_logging import Handler +from adafruit_logging import Handler, NOTSET from adafruit_ble.uart import UARTServer + class BLEHandler(Handler): """Send logging output to the BLE uart port.""" - def __init__(self): + def __init__(self, level: int = NOTSET): """Create an instance. :param uart: the busio.UART instance to which to write messages """ + super().__init__(level) self._advertising_now = False self._uart = UARTServer() self._uart.start_advertising() @@ -37,7 +39,7 @@ class BLEHandler(Handler): :param record: The record (message object) to be logged """ - return super().format(record) + '\r\n' + return super().format(record) + "\r\n" def emit(self, record): """Generate the message and write it to the UART. @@ -46,5 +48,5 @@ class BLEHandler(Handler): """ while not self._uart.connected: pass - data = bytes(self.format(record), 'utf-8') + data = bytes(self.format(record), "utf-8") self._uart.write(data) diff --git a/CircuitPython_Logger/ble_test/code.py b/CircuitPython_Logger/ble_test/code.py index 0b0938064..09ef7e039 100644 --- a/CircuitPython_Logger/ble_test/code.py +++ b/CircuitPython_Logger/ble_test/code.py @@ -7,26 +7,27 @@ import random from ble_handler import BLEHandler import adafruit_logging as logging -l = logging.getLogger('ble') +l = logging.getLogger("ble") l.addHandler(BLEHandler()) + def go(): while True: t = random.randint(1, 5) if t == 1: - print('debug') + print("debug") l.debug("%d", random.randint(0, 1000)) elif t == 2: - print('info') + print("info") l.info("%d", random.randint(0, 1000)) elif t == 3: - print('warning') + print("warning") l.warning("%d", random.randint(0, 1000)) elif t == 4: - print('error') + print("error") l.error("%d", random.randint(0, 1000)) elif t == 5: - print('critical') + print("critical") l.critical(" %d", random.randint(0, 1000)) time.sleep(5.0 + (random.random() * 5.0)) diff --git a/CircuitPython_Logger/file_test/code.py b/CircuitPython_Logger/file_test/code.py index 2ec9ce59a..188239008 100644 --- a/CircuitPython_Logger/file_test/code.py +++ b/CircuitPython_Logger/file_test/code.py @@ -21,25 +21,26 @@ sdcard = adafruit_sdcard.SDCard(spi, cs) vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") -l = logging.getLogger('file') -l.addHandler(logging.FileHandler('/sd/test.txt')) +l = logging.getLogger("file") +l.addHandler(logging.FileHandler("/sd/test.txt")) + def go(): while True: t = random.randint(1, 5) if t == 1: - print('debug') + print("debug") l.debug("debug message: %d", random.randint(0, 1000)) elif t == 2: - print('info') + print("info") l.info("info message: %d", random.randint(0, 1000)) elif t == 3: - print('warning') + print("warning") l.warning("warning message: %d", random.randint(0, 1000)) elif t == 4: - print('error') + print("error") l.error("error message: %d", random.randint(0, 1000)) elif t == 5: - print('critical') + print("critical") l.critical("critical message: %d", random.randint(0, 1000)) time.sleep(5.0 + (random.random() * 5.0)) diff --git a/CircuitPython_Logger/uart_handler/code.py b/CircuitPython_Logger/uart_handler/code.py index 39b515ce9..b4932f45b 100644 --- a/CircuitPython_Logger/uart_handler/code.py +++ b/CircuitPython_Logger/uart_handler/code.py @@ -1,57 +1,13 @@ # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries # # SPDX-License-Identifier: MIT +import board +import busio +from uart_handler import UartHandler +import adafruit_logging as logging -""" -UART based message handler for CircuitPython logging. - -Adafruit invests time and resources providing this open source code. -Please support Adafruit and open source hardware by purchasing -products from Adafruit! - -Written by Dave Astels for Adafruit Industries -Copyright (c) 2018 Adafruit Industries -Licensed under the MIT license. - -All text above must be included in any redistribution. -""" - - -# Example: -# -# import board -# import busio -# from uart_handler import UartHandler -# import adafruit_logging as logging -# -# uart = busio.UART(board.TX, board.RX, baudrate=115200) -# logger = logging.getLogger('uart') -# logger.addHandler(UartHandler(uart)) -# logger.level = logging.INFO -# logger.info('testing') - -from adafruit_logging import Handler - -class UartHandler(Handler): - """Send logging output to a serial port.""" - - def __init__(self, uart): - """Create an instance. - - :param uart: the busio.UART instance to which to write messages - """ - self._uart = uart - - def format(self, record): - """Generate a string to log. - - :param record: The record (message object) to be logged - """ - return super().format(record) + '\r\n' - - def emit(self, record): - """Generate the message and write it to the UART. - - :param record: The record (message object) to be logged - """ - self._uart.write(bytes(self.format(record), 'utf-8')) +uart = busio.UART(board.TX, board.RX, baudrate=115200) +logger = logging.getLogger("test") +logger.addHandler(UartHandler(uart)) +logger.setLevel(logging.INFO) +logger.info("testing") diff --git a/CircuitPython_Logger/uart_handler/uart_handler.py b/CircuitPython_Logger/uart_handler/uart_handler.py new file mode 100644 index 000000000..9d743a009 --- /dev/null +++ b/CircuitPython_Logger/uart_handler/uart_handler.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +UART based message handler for CircuitPython logging. + +Adafruit invests time and resources providing this open source code. +Please support Adafruit and open source hardware by purchasing +products from Adafruit! + +Written by Dave Astels for Adafruit Industries +Copyright (c) 2018 Adafruit Industries +Licensed under the MIT license. + +All text above must be included in any redistribution. +""" + + +from adafruit_logging import Handler, NOTSET + + +class UartHandler(Handler): + """Send logging output to a serial port.""" + + def __init__(self, uart, level: int = NOTSET): + """Create an instance. + + :param uart: the busio.UART instance to which to write messages + """ + super().__init__(level) + self._uart = uart + + def format(self, record): + """Generate a string to log. + + :param record: The record (message object) to be logged + """ + return super().format(record) + "\r\n" + + def emit(self, record): + """Generate the message and write it to the UART. + + :param record: The record (message object) to be logged + """ + self._uart.write(bytes(self.format(record), "utf-8"))