From 50d7a4f3353825cb6cb480ea7ebaec1f778772ce Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 26 May 2015 13:05:37 +0200 Subject: [PATCH] Catch encoding errors in windows logger --- gns3server/web/logger.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gns3server/web/logger.py b/gns3server/web/logger.py index 1fe40ffa..609bcbd3 100644 --- a/gns3server/web/logger.py +++ b/gns3server/web/logger.py @@ -81,12 +81,30 @@ class ColouredStreamHandler(logging.StreamHandler): self.handleError(record) +class WinStreamHandler(logging.StreamHandler): + + def emit(self, record): + + if sys.stdin.encoding != "utf-8": + record = record + + stream = self.stream + try: + msg = self.formatter.format(record, stream.isatty()) + stream.write(msg.encode(stream.encoding, errors="replace").decode(stream.encoding)) + stream.write(self.terminator) + self.flush() + pass + except Exception: + self.handleError(record) + + def init_logger(level, logfile=None, quiet=False): if logfile and len(logfile) > 0: stream_handler = logging.FileHandler(logfile) stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{") elif sys.platform.startswith("win"): - stream_handler = logging.StreamHandler(sys.stdout) + stream_handler = WinStreamHandler(sys.stdout) stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{") else: stream_handler = ColouredStreamHandler(sys.stdout)