Commit ea5a86b9 authored by Dan Pascu's avatar Dan Pascu

Improved the diverted stdout handler

parent e1e5ad6a
...@@ -31,9 +31,9 @@ if os.path.basename(script_dir) == 'bin' and os.path.exists(os.path.join(parent_ ...@@ -31,9 +31,9 @@ if os.path.basename(script_dir) == 'bin' and os.path.exists(os.path.join(parent_
if frozen: if frozen:
from StringIO import StringIO from StringIO import StringIO
class FakeStdout(object): class DivertedStdout(object):
def __init__(self): def __init__(self):
self._io = StringIO() self._output = StringIO()
self._file = None self._file = None
@property @property
...@@ -41,18 +41,20 @@ if frozen: ...@@ -41,18 +41,20 @@ if frozen:
return self._file return self._file
@file.setter @file.setter
def file(self, f): def file(self, path):
f.write(self._io.getvalue()) if self._file is not None:
self._io.close() raise RuntimeError("output file was already set to: {0._file!r}".format(self))
self._file = f f = open(path, 'a', 0)
# noinspection PyUnresolvedReferences
f.write(self._output.getvalue())
self._output.close()
self._output = f
self._file = path
def __getattr__(self, name): def __getattr__(self, name):
if self._file is not None: return getattr(self._output, name)
return getattr(self._file, name)
else:
return getattr(self._io, name)
sys.stdout = sys.stderr = FakeStdout() sys.stdout = sys.stderr = DivertedStdout()
# Import log last so the created StreamHandler instances have references # Import log last so the created StreamHandler instances have references
......
...@@ -45,10 +45,10 @@ __all__ = ['Blink'] ...@@ -45,10 +45,10 @@ __all__ = ['Blink']
if hasattr(sys, 'frozen'): if hasattr(sys, 'frozen'):
makedirs(ApplicationData.get('logs'))
sys.stdout.file = open(ApplicationData.get('logs/output.log'), 'a', 0)
import httplib2 import httplib2
httplib2.CA_CERTS = os.environ['SSL_CERT_FILE'] = Resources.get('tls/cacerts.pem') httplib2.CA_CERTS = os.environ['SSL_CERT_FILE'] = Resources.get('tls/cacerts.pem')
makedirs(ApplicationData.get('logs'))
sys.stdout.file = ApplicationData.get('logs/output.log')
class IPAddressMonitor(object): class IPAddressMonitor(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment