1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Catch encoding errors in windows logger

This commit is contained in:
Julien Duponchelle 2015-05-26 13:05:37 +02:00
parent 4216724d0b
commit 50d7a4f335

View File

@ -81,12 +81,30 @@ class ColouredStreamHandler(logging.StreamHandler):
self.handleError(record) 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): def init_logger(level, logfile=None, quiet=False):
if logfile and len(logfile) > 0: if logfile and len(logfile) > 0:
stream_handler = logging.FileHandler(logfile) stream_handler = logging.FileHandler(logfile)
stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{") stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{")
elif sys.platform.startswith("win"): 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", "{") stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{")
else: else:
stream_handler = ColouredStreamHandler(sys.stdout) stream_handler = ColouredStreamHandler(sys.stdout)