Skip to content

Commit

Permalink
istead redirecting stdout/stderr to logger, now the output is copied
Browse files Browse the repository at this point in the history
  • Loading branch information
m1lhaus committed Aug 16, 2015
1 parent 087262c commit ad2e646
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import psutil


class StreamToLogger(object):
class CopyToLogger(object):
"""
Fake file-like stream object that redirects stdout/stderr writes to a logger instance.
Fake file-like stream object that copies stdout/stderr writes to a logger instance.
"""

def __init__(self, fdnum, logger, log_level=logging.INFO):
Expand All @@ -36,13 +36,18 @@ def __init__(self, fdnum, logger, log_level=logging.INFO):
self.log_level = log_level

def write(self, buf):
if buf == '\n':
self.orig_output.write(buf)
else:
if isinstance(buf, str):
buf = unicode(buf, u'utf-8')
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
self.orig_output.write(buf)
buf = buf.decode(sys.getfilesystemencoding())
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())

# if buf == '\n':
# self.orig_output.write(buf)
# else:
# if isinstance(buf, str):
# buf = unicode(buf, u'utf-8')
# for line in buf.rstrip().splitlines():
# self.logger.log(self.log_level, line.rstrip())

def __getattr__(self, name):
return self.orig_output.__getattribute__(name) # pass all other methods to original fd
Expand All @@ -59,8 +64,8 @@ def setup_logging(log_dir):
logger = logging.getLogger('') # get root logger

# redirects all stderr output (exceptions, etc.) to logger ERROR level
sys.stdout = StreamToLogger(0, logger, logging.DEBUG)
sys.stderr = StreamToLogger(1, logger, logging.ERROR)
sys.stdout = CopyToLogger(0, logger, logging.DEBUG)
sys.stderr = CopyToLogger(1, logger, logging.ERROR)
logger.debug("Logger initialized")

return logger
Expand All @@ -72,9 +77,9 @@ def is_running(pid):

print "Waiting until parent process PID: %s finishes ..." % args.pid

attempts = 5
attempts = 9
time.sleep(0.5)
while is_running(args.pid) and attempts > 0: # no more waiting than 3s
while is_running(args.pid) and attempts > 0: # no more waiting than 5s
time.sleep(0.5)
attempts -= 1

Expand Down

0 comments on commit ad2e646

Please sign in to comment.