Skip to content

Commit c0bb398

Browse files
authored
Merge pull request #653 from dastels/cp_logging
Add BLE handler
2 parents 5218936 + 0d6c45f commit c0bb398

3 files changed

Lines changed: 100 additions & 13 deletions

File tree

CircuitPython_Logger/aio_test.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66
l = logging.getLogger('aio')
77
l.addHandler(AIOHandler('test'))
88

9-
while True:
10-
t = random.randint(1, 5)
11-
if t == 1:
12-
l.debug("debug message: %d", random.randint(0, 1000))
13-
elif t == 2:
14-
l.info("info message: %d", random.randint(0, 1000))
15-
elif t == 3:
16-
l.warning("warning message: %d", random.randint(0, 1000))
17-
elif t == 4:
18-
l.error("error message: %d", random.randint(0, 1000))
19-
elif t == 5:
20-
l.critical("critical message: %d", random.randint(0, 1000))
21-
time.sleep(5.0 + (random.random() * 5.0))
9+
def go():
10+
while True:
11+
t = random.randint(1, 5)
12+
if t == 1:
13+
print('debug')
14+
l.debug("debug message: %d", random.randint(0, 1000))
15+
elif t == 2:
16+
print('info')
17+
l.info("info message: %d", random.randint(0, 1000))
18+
elif t == 3:
19+
print('warning')
20+
l.warning("warning message: %d", random.randint(0, 1000))
21+
elif t == 4:
22+
print('error')
23+
l.error("error message: %d", random.randint(0, 1000))
24+
elif t == 5:
25+
print('critical')
26+
l.critical("critical message: %d", random.randint(0, 1000))
27+
time.sleep(5.0 + (random.random() * 5.0))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
BLE based message handler for CircuitPython logging.
3+
4+
Adafruit invests time and resources providing this open source code.
5+
Please support Adafruit and open source hardware by purchasing
6+
products from Adafruit!
7+
8+
Written by Dave Astels for Adafruit Industries
9+
Copyright (c) 2018 Adafruit Industries
10+
Licensed under the MIT license.
11+
12+
All text above must be included in any redistribution.
13+
"""
14+
15+
16+
#pylint:disable=missing-super-argument
17+
18+
from adafruit_logging import LoggingHandler
19+
from adafruit_ble.uart import UARTServer
20+
21+
class BLEHandler(LoggingHandler):
22+
"""Send logging output to the BLE uart port."""
23+
24+
def __init__(self):
25+
"""Create an instance.
26+
27+
:param uart: the busio.UART instance to which to write messages
28+
29+
"""
30+
self._advertising_now = False
31+
self._uart = UARTServer()
32+
self._uart.start_advertising()
33+
34+
def format(self, level, msg):
35+
"""Generate a string to log.
36+
37+
:param level: The level at which to log
38+
:param msg: The core message
39+
40+
"""
41+
return super().format(level, msg) + '\r\n'
42+
43+
def emit(self, level, msg):
44+
"""Generate the message and write it to the UART.
45+
46+
:param level: The level at which to log
47+
:param msg: The core message
48+
49+
"""
50+
while not self._uart.connected:
51+
pass
52+
data = bytes(self.format(level, msg), 'utf-8')
53+
self._uart.write(data)

CircuitPython_Logger/ble_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
import random
3+
from ble_handler import BLEHandler
4+
import adafruit_logging as logging
5+
6+
l = logging.getLogger('ble')
7+
8+
l.addHandler(BLEHandler())
9+
10+
def go():
11+
while True:
12+
t = random.randint(1, 5)
13+
if t == 1:
14+
print('debug')
15+
l.debug("%d", random.randint(0, 1000))
16+
elif t == 2:
17+
print('info')
18+
l.info("%d", random.randint(0, 1000))
19+
elif t == 3:
20+
print('warning')
21+
l.warning("%d", random.randint(0, 1000))
22+
elif t == 4:
23+
print('error')
24+
l.error("%d", random.randint(0, 1000))
25+
elif t == 5:
26+
print('critical')
27+
l.critical(" %d", random.randint(0, 1000))
28+
time.sleep(5.0 + (random.random() * 5.0))

0 commit comments

Comments
 (0)