Added stdout and stderr output to files

This commit is contained in:
zhaoyafan 2025-02-12 23:12:16 +08:00
parent 21e1dfb890
commit 490fec0dfb
1 changed files with 32 additions and 6 deletions

View File

@ -2243,6 +2243,32 @@ class WebServer:
uvicorn.run(self.app, host=host, port=port, log_level='warning')
class OutputRedirector:
def __init__(self, sys_fp, new_fp):
self.sys_fp = sys_fp
self.new_fp = new_fp
def write(self, s):
self.sys_fp.write(s)
self.new_fp.write(s)
self.flush()
def writelines(self, lines):
self.sys_fp.writelines(lines)
self.new_fp.writelines(lines)
self.flush()
def flush(self):
self.sys_fp.flush()
self.new_fp.flush()
def __getattr__(self, item):
if item in ['write', 'writelines', 'flush']:
return getattr(self, item)
else:
return getattr(self.sys_fp, item)
class MainRunner:
def __init__(self):
signal.signal(signal.SIGINT, self._handle_interrupt)
@ -2288,9 +2314,11 @@ class MainRunner:
def run(self):
os.path.exists(self.app_data) or os.makedirs(self.app_data)
if (not os.environ.get('PYCHARM_HOSTED')) == 1:
sys.stderr = open(os.path.join(self.app_data, 'stderr.txt'), 'w')
sys.stdout = open(os.path.join(self.app_data, 'stdout.txt'), 'w')
if (os.environ.get('PYCHARM_HOSTED') is None) == 1:
stdout_redirector = OutputRedirector(sys.stdout, open(os.path.join(self.app_data, 'stdout.txt'), mode='w'))
stderr_redirector = OutputRedirector(sys.stderr, open(os.path.join(self.app_data, 'stderr.txt'), mode='w'))
sys.stderr = stderr_redirector
sys.stdout = stdout_redirector
sys.path.append(os.path.join(os.path.dirname(__file__), 'Packages'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'site-packages.zip'))
sys.path.append(os.path.join(self.app_data, 'Packages'))
@ -2300,8 +2328,6 @@ class MainRunner:
self.web_server_thread.daemon = True
self.web_server_thread.start()
self.application = QApplication(sys.argv)
self.application.setAttribute(Qt.AA_EnableHighDpiScaling, True)
self.application.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
self.application.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
self.application_scale_rate = self.application.screens()[0].logicalDotsPerInch() / 96
self.window = MainWindow(
@ -2335,7 +2361,7 @@ class MainRunner:
'--include-module=fastapi',
'--include-module=pydantic',
'--include-module=starlette',
'--windows-console-mode=disable',
'--windows-console-mode=hide',
'--windows-icon-from-ico=favicon.ico',
'--product-name=%s' % (self.app_name,),
'--file-description=%s' % (self.app_name,),