6262from collections import namedtuple
6363
6464try :
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+
6781except ImportError :
6882 pass
6983
@@ -143,8 +157,7 @@ class Handler:
143157 def format (self , record : LogRecord ) -> str :
144158 """Generate a timestamped message.
145159
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
148161 """
149162
150163 return "{0:<0.3f}: {1} - {2}" .format (
@@ -154,6 +167,8 @@ def format(self, record: LogRecord) -> str:
154167 def emit (self , record : LogRecord ) -> None :
155168 """Send a message where it should go.
156169 Placeholder for subclass implementations.
170+
171+ :param record: The record (message object) to be logged
157172 """
158173
159174 raise NotImplementedError ()
@@ -164,10 +179,12 @@ class StreamHandler(Handler):
164179 """Send logging messages to a stream, `sys.stderr` (typically
165180 the serial console) by default.
166181
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
168185 """
169186
170- def __init__ (self , stream : Optional [Union [ TextIOWrapper , StringIO ] ] = None ) -> None :
187+ def __init__ (self , stream : Optional [WriteableStream ] = None ) -> None :
171188 super ().__init__ ()
172189 if stream is None :
173190 stream = sys .stderr
@@ -177,8 +194,7 @@ def __init__(self, stream: Optional[Union[TextIOWrapper, StringIO]] = None) -> N
177194 def emit (self , record : LogRecord ) -> None :
178195 """Send a message to the console.
179196
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
182198 """
183199 self .stream .write (self .format (record ))
184200
@@ -203,16 +219,14 @@ def close(self) -> None:
203219 def format (self , record : LogRecord ) -> str :
204220 """Generate a string to log
205221
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
208223 """
209224 return super ().format (record ) + "\r \n "
210225
211226 def emit (self , record : LogRecord ) -> None :
212227 """Generate the message and write it to the UART.
213228
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
216230 """
217231 self .stream .write (self .format (record ))
218232
@@ -286,7 +300,7 @@ def addHandler(self, hdlr: Handler) -> None:
286300 *NOTE* This is slightly different from the CPython equivalent
287301 which adds the handler rather than replacing it.
288302
289- :param Handler hdlr: the handler
303+ :param Handler hdlr: The handler to add
290304 """
291305 self ._handler = hdlr
292306
0 commit comments