Skip to content

Commit 609ee13

Browse files
committed
Redirect stderr and stdout to logger when we are frozen. Need to test this.
1 parent 4216904 commit 609ee13

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

config.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
saveDB = None
3333
gameDB = None
3434

35+
class StreamToLogger(object):
36+
"""
37+
Fake file-like stream object that redirects writes to a logger instance.
38+
From: http://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
39+
"""
40+
def __init__(self, logger, log_level=logging.INFO):
41+
self.logger = logger
42+
self.log_level = log_level
43+
self.linebuf = ''
44+
45+
def write(self, buf):
46+
for line in buf.rstrip().splitlines():
47+
self.logger.log(self.log_level, line.rstrip())
48+
49+
3550
def defPaths():
3651
global pyfaPath
3752
global savePath
@@ -66,19 +81,14 @@ def defPaths():
6681

6782
logging.info("Starting pyfa")
6883

69-
# Redirect stderr to file if we're requested to do so
70-
stderrToFile = getattr(configforced, "stderrToFile", None)
71-
if stderrToFile is True:
72-
if not os.path.exists(savePath):
73-
os.mkdir(savePath)
74-
sys.stderr = open(os.path.join(savePath, "error_log.txt"), "w")
75-
76-
# Same for stdout
77-
stdoutToFile = getattr(configforced, "stdoutToFile", None)
78-
if stdoutToFile is True:
79-
if not os.path.exists(savePath):
80-
os.mkdir(savePath)
81-
sys.stdout = open(os.path.join(savePath, "output_log.txt"), "w")
84+
if hasattr(sys, 'frozen'):
85+
stdout_logger = logging.getLogger('STDOUT')
86+
sl = StreamToLogger(stdout_logger, logging.INFO)
87+
sys.stdout = sl
88+
89+
stderr_logger = logging.getLogger('STDERR')
90+
sl = StreamToLogger(stderr_logger, logging.ERROR)
91+
sys.stderr = sl
8292

8393
# Static EVE Data from the staticdata repository, should be in the staticdata
8494
# directory in our pyfa directory

0 commit comments

Comments
 (0)