1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-21 11:27:38 +03:00

Move sys.stdout redirect to top.

* In `develop` config import will set up logging, so let's head that off
  at the pass and put the redirect right at the top.
* Also moved the EDMCLogging import to right after the config import's
  new position, to emulate `develop` behaviour.
* We *append* on the initial open of the redirect log file.  Then once
  we're sure we're the only process we truncate this.
This commit is contained in:
Athanasius 2021-01-18 21:40:18 +00:00
parent 820d481ca9
commit a75f5b9130

@ -15,11 +15,25 @@ from sys import platform
from time import localtime, strftime, time
from typing import TYPE_CHECKING
from config import appversion, appversion_nobuild, config, copyright
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
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 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
from EDMCLogging import edmclogger, logger, logging
if __name__ == '__main__':
# Command-line arguments
parser = argparse.ArgumentParser(
@ -165,16 +179,9 @@ if __name__ == '__main__':
root.mainloop()
if not no_other_instance_running():
# There's a copy already running. We want to inform the user by
# **appending** to the log file, not truncating it.
if getattr(sys, 'frozen', False):
# By default py2exe tries to write log to dirname(sys.executable) which fails when installed
import tempfile
# There's a copy already running.
# 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)
# Logging isn't set up yet.
# Logging isn't set up yet. # TODO: Actually it will be by now in `develop`
# We'll keep this print, but it will be over-written by any subsequent
# write by the already-running process.
print("An EDMarketConnector.exe process was already running, exiting.")
@ -185,18 +192,10 @@ 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
# Now that we're sure we're the only instance running we can truncate the logfile
sys.stdout.truncate()
# 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
from EDMCLogging import edmclogger, logger, logging
# See EDMCLogging.py docs.
# isort: off