|
32 | 32 | saveDB = None |
33 | 33 | gameDB = None |
34 | 34 |
|
| 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 | + |
35 | 50 | def defPaths(): |
36 | 51 | global pyfaPath |
37 | 52 | global savePath |
@@ -66,19 +81,14 @@ def defPaths(): |
66 | 81 |
|
67 | 82 | logging.info("Starting pyfa") |
68 | 83 |
|
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 |
82 | 92 |
|
83 | 93 | # Static EVE Data from the staticdata repository, should be in the staticdata |
84 | 94 | # directory in our pyfa directory |
|
0 commit comments