From c751fe5c99527306bb8f38cf173ae5ada6fa13fb Mon Sep 17 00:00:00 2001 From: yushihang Date: Fri, 21 Jun 2024 00:43:19 +0800 Subject: [PATCH] fix issue that print non-ascii character may cause exception (#22) --- lldb/logs.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lldb/logs.py b/lldb/logs.py index c277b48..37e17a0 100644 --- a/lldb/logs.py +++ b/lldb/logs.py @@ -1,19 +1,25 @@ import threading import time import os +import sys def background_follow(filename): - with open(filename, 'r') as file: + with open(filename, 'r', encoding='utf-8') as file: file.seek(0, os.SEEK_END) + utf8_stdout = open(sys.stdout.fileno(), mode='w', encoding='utf-8', closefd=False) while True: - line = file.readline() - - if not line: - time.sleep(0.1) - continue - - print(line.rstrip("\n"), end=None) + try: + line = file.readline() + + if not line: + time.sleep(0.1) + continue + + print(line.rstrip("\n"), end=None, file=utf8_stdout, flush=True) + + except UnicodeDecodeError as e: + print(f"UnicodeDecodeError occurred while following file {filename}: {e}") def follow(debugger, command, result, internal_dict):