From ce45a4d405e74a5b3da1488ed062a8877c1dc123 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 22 Mar 2021 14:20:20 +0000 Subject: [PATCH] Fix console redirect to work with config.py now using logging. 1. We were now importing from config before the console redirect code runs. 2. That means that config.py's `logger = logging.getLogger(appname)` caused the logger to be set up whilst stdout/err were still pointing at the console. 3. So the redirect then had no effect on logging output. This commit moves the import from config (and also killswitch, because it also imports logging) to after the *moved* console redirect code. NB: This needs the "detected a process already running" instance checking. NB: There's still a `True or ` for testing purposes EDMarketConnector.py:25 # Conflicts: # EDMarketConnector.py --- EDMarketConnector.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 596ded6e..9ba7ee87 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -12,12 +12,28 @@ import webbrowser from builtins import object, str from os import chdir, environ from os.path import dirname, isdir, join -import killswitch from sys import platform from time import localtime, strftime, time from typing import TYPE_CHECKING, Any, Mapping, Optional, Tuple, cast -from constants import applongname, appname, protocolhandler_redirect +# config will now cause an appname logger to be set up, so we need the +# console redirect before this +appname = 'EDMarketConnector' # TODO: Must match config.appname, add test +if __name__ == '__main__': + # Keep this as the very first code run to be as sure as possible of no + # output until after this redirect is done, if needed. + if True or getattr(sys, 'frozen', False): + # By default py2exe tries to write log to dirname(sys.executable) which fails when installed + import tempfile + + # unbuffered not allowed for text in python3, so use `1 for line buffering + sys.stdout = sys.stderr = open(join(tempfile.gettempdir(), f'{appname}.log'), mode='wt', buffering=1) + # TODO: Test: Make *sure* this redirect is working, else py2exe is going to cause an exit popup + +# isort: off +import killswitch # noqa: E402 # Will cause a logging import/startup so needs to be after the redirect +from config import applongname, appname, appversion, appversion_nobuild, config, copyright # noqa: E402 +# isort: on if __name__ == "__main__": def no_other_instance_running() -> bool: # noqa: CCR001 @@ -130,15 +146,6 @@ if __name__ == "__main__": # reach here. sys.exit(0) - # Keep this as the very first code run to be as sure as possible of no - # output until after this redirect is done, if needed. - if getattr(sys, 'frozen', False): - # By default py2exe tries to write log to dirname(sys.executable) which fails when installed - import tempfile - - # unbuffered not allowed for text in python3, so use `1 for line buffering - sys.stdout = sys.stderr = open(join(tempfile.gettempdir(), f'{appname}.log'), mode='a', buffering=1) - # TODO: Test: Make *sure* this redirect is working, else py2exe is going to cause an exit popup # After the redirect in case config does logging setup from config import appversion, appversion_nobuild, config, copyright