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..936f38821 100644 --- a/CircuitPython_Logger/aio_handler/code.py +++ b/CircuitPython_Logger/aio_test/aio_handler.py @@ -17,30 +17,21 @@ """ 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__(level) + 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..fb3f38535 100644 --- a/CircuitPython_Logger/aio_test/code.py +++ b/CircuitPython_Logger/aio_test/code.py @@ -8,27 +8,26 @@ 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') - l.debug("debug message: %d", random.randint(0, 1000)) - elif t == 2: - print('info') - l.info("info message: %d", random.randint(0, 1000)) - elif t == 3: - print('warning') - l.warning("warning message: %d", random.randint(0, 1000)) - elif t == 4: - print('error') - l.error("error message: %d", random.randint(0, 1000)) - elif t == 5: - print('critical') - l.critical("critical message: %d", random.randint(0, 1000)) - time.sleep(5.0 + (random.random() * 5.0)) +while True: + t = random.randint(1, 5) + if t == 1: + print("debug") + l.debug("debug message: %d", random.randint(0, 1000)) + elif t == 2: + print("info") + l.info("info message: %d", random.randint(0, 1000)) + elif t == 3: + print("warning") + l.warning("warning message: %d", random.randint(0, 1000)) + elif t == 4: + print("error") + l.error("error message: %d", random.randint(0, 1000)) + elif t == 5: + 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 65% rename from CircuitPython_Logger/ble_handler/code.py rename to CircuitPython_Logger/ble_test/ble_handler.py index 9650bc05b..3e8aa7510 100644 --- a/CircuitPython_Logger/ble_handler/code.py +++ b/CircuitPython_Logger/ble_test/ble_handler.py @@ -17,34 +17,39 @@ """ -from adafruit_logging import Handler -from adafruit_ble.uart import UARTServer +from adafruit_logging import Handler, NOTSET + +from adafruit_ble import BLERadio +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement +from adafruit_ble.services.nordic import UARTService + 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() + ble = BLERadio() + self._uart = UARTService() + self._advertisement = ProvideServicesAdvertisement(self._uart) + ble.start_advertising(self._advertisement) def format(self, record): """Generate a string to log. :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. :param record: The record (message object) to be logged """ - 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..77d11eb7a 100644 --- a/CircuitPython_Logger/ble_test/code.py +++ b/CircuitPython_Logger/ble_test/code.py @@ -7,26 +7,25 @@ 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') - l.debug("%d", random.randint(0, 1000)) - elif t == 2: - print('info') - l.info("%d", random.randint(0, 1000)) - elif t == 3: - print('warning') - l.warning("%d", random.randint(0, 1000)) - elif t == 4: - print('error') - l.error("%d", random.randint(0, 1000)) - elif t == 5: - print('critical') - l.critical(" %d", random.randint(0, 1000)) - time.sleep(5.0 + (random.random() * 5.0)) +while True: + t = random.randint(1, 5) + if t == 1: + print("debug") + l.debug("%d", random.randint(0, 1000)) + elif t == 2: + print("info") + l.info("%d", random.randint(0, 1000)) + elif t == 3: + print("warning") + l.warning("%d", random.randint(0, 1000)) + elif t == 4: + print("error") + l.error("%d", random.randint(0, 1000)) + elif t == 5: + 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..14e8f7fb6 100644 --- a/CircuitPython_Logger/file_test/code.py +++ b/CircuitPython_Logger/file_test/code.py @@ -21,25 +21,24 @@ 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') - l.debug("debug message: %d", random.randint(0, 1000)) - elif t == 2: - print('info') - l.info("info message: %d", random.randint(0, 1000)) - elif t == 3: - print('warning') - l.warning("warning message: %d", random.randint(0, 1000)) - elif t == 4: - print('error') - l.error("error message: %d", random.randint(0, 1000)) - elif t == 5: - print('critical') - l.critical("critical message: %d", random.randint(0, 1000)) - time.sleep(5.0 + (random.random() * 5.0)) +while True: + t = random.randint(1, 5) + if t == 1: + print("debug") + l.debug("debug message: %d", random.randint(0, 1000)) + elif t == 2: + print("info") + l.info("info message: %d", random.randint(0, 1000)) + elif t == 3: + print("warning") + l.warning("warning message: %d", random.randint(0, 1000)) + elif t == 4: + print("error") + l.error("error message: %d", random.randint(0, 1000)) + elif t == 5: + 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 - -""" -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')) +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("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"))