62
62
from collections import namedtuple
63
63
64
64
try :
65
- from typing import Optional , Union
66
- from io import TextIOWrapper , StringIO
65
+ from typing import Optional
66
+
67
+ try :
68
+ from typing import Protocol
69
+ except ImportError :
70
+ from typing_extensions import Protocol
71
+
72
+ class WriteableStream (Protocol ):
73
+ """Any stream that can ``write`` strings"""
74
+
75
+ def write (self , buf : str ) -> int :
76
+ """Write to the stream
77
+
78
+ :param str buf: The string data to write to the stream
79
+ """
80
+
67
81
except ImportError :
68
82
pass
69
83
@@ -143,8 +157,7 @@ class Handler:
143
157
def format (self , record : LogRecord ) -> str :
144
158
"""Generate a timestamped message.
145
159
146
- :param int log_level: the logging level
147
- :param str message: the message to log
160
+ :param record: The record (message object) to be logged
148
161
"""
149
162
150
163
return "{0:<0.3f}: {1} - {2}" .format (
@@ -154,6 +167,8 @@ def format(self, record: LogRecord) -> str:
154
167
def emit (self , record : LogRecord ) -> None :
155
168
"""Send a message where it should go.
156
169
Placeholder for subclass implementations.
170
+
171
+ :param record: The record (message object) to be logged
157
172
"""
158
173
159
174
raise NotImplementedError ()
@@ -164,10 +179,12 @@ class StreamHandler(Handler):
164
179
"""Send logging messages to a stream, `sys.stderr` (typically
165
180
the serial console) by default.
166
181
167
- :param stream: The stream to log to, default is `sys.stderr`
182
+ :param stream: The stream to log to, default is `sys.stderr`;
183
+ can accept any stream that implements ``stream.write()``
184
+ with string inputs
168
185
"""
169
186
170
- def __init__ (self , stream : Optional [Union [ TextIOWrapper , StringIO ] ] = None ) -> None :
187
+ def __init__ (self , stream : Optional [WriteableStream ] = None ) -> None :
171
188
super ().__init__ ()
172
189
if stream is None :
173
190
stream = sys .stderr
@@ -177,8 +194,7 @@ def __init__(self, stream: Optional[Union[TextIOWrapper, StringIO]] = None) -> N
177
194
def emit (self , record : LogRecord ) -> None :
178
195
"""Send a message to the console.
179
196
180
- :param int log_level: the logging level
181
- :param str message: the message to log
197
+ :param record: The record (message object) to be logged
182
198
"""
183
199
self .stream .write (self .format (record ))
184
200
@@ -203,16 +219,14 @@ def close(self) -> None:
203
219
def format (self , record : LogRecord ) -> str :
204
220
"""Generate a string to log
205
221
206
- :param level: The level of the message
207
- :param msg: The message to format
222
+ :param record: The record (message object) to be logged
208
223
"""
209
224
return super ().format (record ) + "\r \n "
210
225
211
226
def emit (self , record : LogRecord ) -> None :
212
227
"""Generate the message and write it to the UART.
213
228
214
- :param level: The level of the message
215
- :param msg: The message to log
229
+ :param record: The record (message object) to be logged
216
230
"""
217
231
self .stream .write (self .format (record ))
218
232
@@ -286,7 +300,7 @@ def addHandler(self, hdlr: Handler) -> None:
286
300
*NOTE* This is slightly different from the CPython equivalent
287
301
which adds the handler rather than replacing it.
288
302
289
- :param Handler hdlr: the handler
303
+ :param Handler hdlr: The handler to add
290
304
"""
291
305
self ._handler = hdlr
292
306
0 commit comments