Skip to content

Commit 2b4475b

Browse files
authored
Merge pull request #3065 from FoamyGuy/cp_logger_fixes
CircuitPython Logger fixes
2 parents 82e3a2a + 1614566 commit 2b4475b

File tree

8 files changed

+137
-146
lines changed

8 files changed

+137
-146
lines changed

CircuitPython_Logger/.circuitpython.skip

Lines changed: 0 additions & 4 deletions
This file was deleted.

CircuitPython_Logger/aio_handler/code.py renamed to CircuitPython_Logger/aio_test/aio_handler.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,21 @@
1717
"""
1818

1919
from adafruit_portalbase import PortalBase
20+
from adafruit_logging import Handler, NOTSET
2021

21-
# Example:
22-
#
23-
# from aio_handler import AIOHandler
24-
# import adafruit_logging as logging
25-
# l = logging.getLogger('aio')
26-
# # Pass in the device object based on portal_base
27-
# # (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter
28-
# l.addHandler(AIOHandler('test', portal_device))
29-
# l.level = logging.ERROR
30-
# l.error("test")
31-
32-
from adafruit_logging import Handler
3322

3423
class AIOHandler(Handler):
3524

36-
def __init__(self, name, portal_device):
25+
def __init__(self, name, portal_device, level: int = NOTSET):
3726
"""Create an instance."""
38-
self._log_feed_name=f"{name}-logging"
27+
super().__init__(level)
28+
self._log_feed_name = f"{name}-logging"
3929
if not issubclass(type(portal_device), PortalBase):
40-
raise TypeError("portal_device must be a PortalBase or subclass of PortalBase")
30+
raise TypeError(
31+
"portal_device must be a PortalBase or subclass of PortalBase"
32+
)
4133
self._portal_device = portal_device
4234

43-
4435
def emit(self, record):
4536
"""Generate the message and write it to the AIO Feed.
4637

CircuitPython_Logger/aio_test/code.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
from aio_handler import AIOHandler
99
import adafruit_logging as logging
1010

11-
device=PyPortal()
11+
device = PyPortal()
1212

13-
l = logging.getLogger('aio')
14-
l.addHandler(AIOHandler('test', device))
13+
l = logging.getLogger("aio")
14+
l.addHandler(AIOHandler("test", device))
1515

16-
def go():
17-
while True:
18-
t = random.randint(1, 5)
19-
if t == 1:
20-
print('debug')
21-
l.debug("debug message: %d", random.randint(0, 1000))
22-
elif t == 2:
23-
print('info')
24-
l.info("info message: %d", random.randint(0, 1000))
25-
elif t == 3:
26-
print('warning')
27-
l.warning("warning message: %d", random.randint(0, 1000))
28-
elif t == 4:
29-
print('error')
30-
l.error("error message: %d", random.randint(0, 1000))
31-
elif t == 5:
32-
print('critical')
33-
l.critical("critical message: %d", random.randint(0, 1000))
34-
time.sleep(5.0 + (random.random() * 5.0))
16+
while True:
17+
t = random.randint(1, 5)
18+
if t == 1:
19+
print("debug")
20+
l.debug("debug message: %d", random.randint(0, 1000))
21+
elif t == 2:
22+
print("info")
23+
l.info("info message: %d", random.randint(0, 1000))
24+
elif t == 3:
25+
print("warning")
26+
l.warning("warning message: %d", random.randint(0, 1000))
27+
elif t == 4:
28+
print("error")
29+
l.error("error message: %d", random.randint(0, 1000))
30+
elif t == 5:
31+
print("critical")
32+
l.critical("critical message: %d", random.randint(0, 1000))
33+
time.sleep(5.0 + (random.random() * 5.0))

CircuitPython_Logger/ble_handler/code.py renamed to CircuitPython_Logger/ble_test/ble_handler.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,39 @@
1717
"""
1818

1919

20-
from adafruit_logging import Handler
21-
from adafruit_ble.uart import UARTServer
20+
from adafruit_logging import Handler, NOTSET
21+
22+
from adafruit_ble import BLERadio
23+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
24+
from adafruit_ble.services.nordic import UARTService
25+
2226

2327
class BLEHandler(Handler):
2428
"""Send logging output to the BLE uart port."""
2529

26-
def __init__(self):
30+
def __init__(self, level: int = NOTSET):
2731
"""Create an instance.
2832
2933
:param uart: the busio.UART instance to which to write messages
3034
"""
35+
super().__init__(level)
3136
self._advertising_now = False
32-
self._uart = UARTServer()
33-
self._uart.start_advertising()
37+
ble = BLERadio()
38+
self._uart = UARTService()
39+
self._advertisement = ProvideServicesAdvertisement(self._uart)
40+
ble.start_advertising(self._advertisement)
3441

3542
def format(self, record):
3643
"""Generate a string to log.
3744
3845
:param record: The record (message object) to be logged
3946
"""
40-
return super().format(record) + '\r\n'
47+
return super().format(record) + "\r\n"
4148

4249
def emit(self, record):
4350
"""Generate the message and write it to the UART.
4451
4552
:param record: The record (message object) to be logged
4653
"""
47-
while not self._uart.connected:
48-
pass
49-
data = bytes(self.format(record), 'utf-8')
54+
data = bytes(self.format(record), "utf-8")
5055
self._uart.write(data)

CircuitPython_Logger/ble_test/code.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@
77
from ble_handler import BLEHandler
88
import adafruit_logging as logging
99

10-
l = logging.getLogger('ble')
10+
l = logging.getLogger("ble")
1111

1212
l.addHandler(BLEHandler())
1313

14-
def go():
15-
while True:
16-
t = random.randint(1, 5)
17-
if t == 1:
18-
print('debug')
19-
l.debug("%d", random.randint(0, 1000))
20-
elif t == 2:
21-
print('info')
22-
l.info("%d", random.randint(0, 1000))
23-
elif t == 3:
24-
print('warning')
25-
l.warning("%d", random.randint(0, 1000))
26-
elif t == 4:
27-
print('error')
28-
l.error("%d", random.randint(0, 1000))
29-
elif t == 5:
30-
print('critical')
31-
l.critical(" %d", random.randint(0, 1000))
32-
time.sleep(5.0 + (random.random() * 5.0))
14+
while True:
15+
t = random.randint(1, 5)
16+
if t == 1:
17+
print("debug")
18+
l.debug("%d", random.randint(0, 1000))
19+
elif t == 2:
20+
print("info")
21+
l.info("%d", random.randint(0, 1000))
22+
elif t == 3:
23+
print("warning")
24+
l.warning("%d", random.randint(0, 1000))
25+
elif t == 4:
26+
print("error")
27+
l.error("%d", random.randint(0, 1000))
28+
elif t == 5:
29+
print("critical")
30+
l.critical(" %d", random.randint(0, 1000))
31+
time.sleep(5.0 + (random.random() * 5.0))

CircuitPython_Logger/file_test/code.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,24 @@
2121
vfs = storage.VfsFat(sdcard)
2222
storage.mount(vfs, "/sd")
2323

24-
l = logging.getLogger('file')
25-
l.addHandler(logging.FileHandler('/sd/test.txt'))
24+
l = logging.getLogger("file")
25+
l.addHandler(logging.FileHandler("/sd/test.txt"))
2626

27-
def go():
28-
while True:
29-
t = random.randint(1, 5)
30-
if t == 1:
31-
print('debug')
32-
l.debug("debug message: %d", random.randint(0, 1000))
33-
elif t == 2:
34-
print('info')
35-
l.info("info message: %d", random.randint(0, 1000))
36-
elif t == 3:
37-
print('warning')
38-
l.warning("warning message: %d", random.randint(0, 1000))
39-
elif t == 4:
40-
print('error')
41-
l.error("error message: %d", random.randint(0, 1000))
42-
elif t == 5:
43-
print('critical')
44-
l.critical("critical message: %d", random.randint(0, 1000))
45-
time.sleep(5.0 + (random.random() * 5.0))
27+
while True:
28+
t = random.randint(1, 5)
29+
if t == 1:
30+
print("debug")
31+
l.debug("debug message: %d", random.randint(0, 1000))
32+
elif t == 2:
33+
print("info")
34+
l.info("info message: %d", random.randint(0, 1000))
35+
elif t == 3:
36+
print("warning")
37+
l.warning("warning message: %d", random.randint(0, 1000))
38+
elif t == 4:
39+
print("error")
40+
l.error("error message: %d", random.randint(0, 1000))
41+
elif t == 5:
42+
print("critical")
43+
l.critical("critical message: %d", random.randint(0, 1000))
44+
time.sleep(5.0 + (random.random() * 5.0))
Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,13 @@
11
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
4-
5-
"""
6-
UART based message handler for CircuitPython logging.
7-
8-
Adafruit invests time and resources providing this open source code.
9-
Please support Adafruit and open source hardware by purchasing
10-
products from Adafruit!
11-
12-
Written by Dave Astels for Adafruit Industries
13-
Copyright (c) 2018 Adafruit Industries
14-
Licensed under the MIT license.
15-
16-
All text above must be included in any redistribution.
17-
"""
18-
19-
20-
# Example:
21-
#
22-
# import board
23-
# import busio
24-
# from uart_handler import UartHandler
25-
# import adafruit_logging as logging
26-
#
27-
# uart = busio.UART(board.TX, board.RX, baudrate=115200)
28-
# logger = logging.getLogger('uart')
29-
# logger.addHandler(UartHandler(uart))
30-
# logger.level = logging.INFO
31-
# logger.info('testing')
32-
33-
from adafruit_logging import Handler
34-
35-
class UartHandler(Handler):
36-
"""Send logging output to a serial port."""
37-
38-
def __init__(self, uart):
39-
"""Create an instance.
40-
41-
:param uart: the busio.UART instance to which to write messages
42-
"""
43-
self._uart = uart
44-
45-
def format(self, record):
46-
"""Generate a string to log.
47-
48-
:param record: The record (message object) to be logged
49-
"""
50-
return super().format(record) + '\r\n'
51-
52-
def emit(self, record):
53-
"""Generate the message and write it to the UART.
54-
55-
:param record: The record (message object) to be logged
56-
"""
57-
self._uart.write(bytes(self.format(record), 'utf-8'))
4+
import board
5+
import busio
6+
from uart_handler import UartHandler
7+
import adafruit_logging as logging
8+
9+
uart = busio.UART(board.TX, board.RX, baudrate=115200)
10+
logger = logging.getLogger("test")
11+
logger.addHandler(UartHandler(uart))
12+
logger.setLevel(logging.INFO)
13+
logger.info("testing")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
UART based message handler for CircuitPython logging.
7+
8+
Adafruit invests time and resources providing this open source code.
9+
Please support Adafruit and open source hardware by purchasing
10+
products from Adafruit!
11+
12+
Written by Dave Astels for Adafruit Industries
13+
Copyright (c) 2018 Adafruit Industries
14+
Licensed under the MIT license.
15+
16+
All text above must be included in any redistribution.
17+
"""
18+
19+
20+
from adafruit_logging import Handler, NOTSET
21+
22+
23+
class UartHandler(Handler):
24+
"""Send logging output to a serial port."""
25+
26+
def __init__(self, uart, level: int = NOTSET):
27+
"""Create an instance.
28+
29+
:param uart: the busio.UART instance to which to write messages
30+
"""
31+
super().__init__(level)
32+
self._uart = uart
33+
34+
def format(self, record):
35+
"""Generate a string to log.
36+
37+
:param record: The record (message object) to be logged
38+
"""
39+
return super().format(record) + "\r\n"
40+
41+
def emit(self, record):
42+
"""Generate the message and write it to the UART.
43+
44+
:param record: The record (message object) to be logged
45+
"""
46+
self._uart.write(bytes(self.format(record), "utf-8"))

0 commit comments

Comments
 (0)