From 337770e62bd250e8af777e8e9f703fee7ac862f9 Mon Sep 17 00:00:00 2001 From: Pat Date: Sun, 2 Jun 2024 22:18:58 -0500 Subject: [PATCH 1/4] Add flush to the file write functions --- adafruit_logging.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_logging.py b/adafruit_logging.py index 370f252..fc552d7 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -239,6 +239,7 @@ def emit(self, record: LogRecord) -> None: :param record: The record (message object) to be logged """ self.stream.write(self.format(record)) + self.stream.flush() class RotatingFileHandler(FileHandler): @@ -338,6 +339,7 @@ def emit(self, record: LogRecord) -> None: ): self.doRollover() self.stream.write(self.format(record)) + self.stream.flush() class NullHandler(Handler): From 4e0c2f33fe1bc76b65df8e384c930e7b7c205919 Mon Sep 17 00:00:00 2001 From: Pat Date: Mon, 3 Jun 2024 19:21:40 -0500 Subject: [PATCH 2/4] Add flush function to the stream handler. --- adafruit_logging.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index fc552d7..03a0351 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -206,6 +206,12 @@ def emit(self, record: LogRecord) -> None: """ self.stream.write(self.format(record) + self.terminator) + def flush(self) -> None: + """flush the stream. You might need to call this if your messages + are not appearing in the log file. + """ + self.stream.flush() + class FileHandler(StreamHandler): """File handler for working with log files off of the microcontroller (like @@ -239,7 +245,6 @@ def emit(self, record: LogRecord) -> None: :param record: The record (message object) to be logged """ self.stream.write(self.format(record)) - self.stream.flush() class RotatingFileHandler(FileHandler): @@ -339,7 +344,6 @@ def emit(self, record: LogRecord) -> None: ): self.doRollover() self.stream.write(self.format(record)) - self.stream.flush() class NullHandler(Handler): From 18a4a3de2d41d3aab2523ab2066fc0504be8a15e Mon Sep 17 00:00:00 2001 From: Pat Date: Mon, 3 Jun 2024 20:21:31 -0500 Subject: [PATCH 3/4] Added a flushHandlers() function to the logger. --- adafruit_logging.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/adafruit_logging.py b/adafruit_logging.py index 03a0351..006f431 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -179,6 +179,9 @@ def emit(self, record: LogRecord) -> None: raise NotImplementedError() + def flush(self) -> None: + """Placeholder for flush function in subclasses.""" + # pylint: disable=too-few-public-methods class StreamHandler(Handler): @@ -415,6 +418,13 @@ def getEffectiveLevel(self) -> int: return self._level + def flushHandlers(self) -> None: + """Flush all handlers. This will ensure that all data is immediately written to the streams. + This can be useful if you need to make sure the log is written before a reset. + """ + for handlerName in self._handlers: + handlerName.flush() + def addHandler(self, hdlr: Handler) -> None: """Adds the handler to this logger. From 3cc0d4f0e13886b581ded13aa523849d6a319b55 Mon Sep 17 00:00:00 2001 From: Pat Date: Tue, 4 Jun 2024 21:10:59 -0500 Subject: [PATCH 4/4] Revert to flushing after file writes. --- adafruit_logging.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index 006f431..f1e171f 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -248,6 +248,7 @@ def emit(self, record: LogRecord) -> None: :param record: The record (message object) to be logged """ self.stream.write(self.format(record)) + self.stream.flush() class RotatingFileHandler(FileHandler): @@ -347,6 +348,7 @@ def emit(self, record: LogRecord) -> None: ): self.doRollover() self.stream.write(self.format(record)) + self.stream.flush() class NullHandler(Handler): @@ -418,13 +420,6 @@ def getEffectiveLevel(self) -> int: return self._level - def flushHandlers(self) -> None: - """Flush all handlers. This will ensure that all data is immediately written to the streams. - This can be useful if you need to make sure the log is written before a reset. - """ - for handlerName in self._handlers: - handlerName.flush() - def addHandler(self, hdlr: Handler) -> None: """Adds the handler to this logger.