wrapper function for writing to stdout

This commit is contained in:
Tomas Kopecek 2019-02-19 13:55:06 +01:00 committed by Mike McLean
parent 8e41dd6d09
commit a4d41bd078
3 changed files with 19 additions and 10 deletions

View file

@ -343,6 +343,21 @@ Running Tasks:
return rv
def write_to_stdout(contents):
"""Helper function to write str/bytes to stdout
https://docs.python.org/3/library/sys.html#sys.displayhook
"""
try:
sys.stdout.write(contents)
except UnicodeEncodeError:
bytes = contents.encode(sys.stdout.encoding, 'backslashreplace')
if hasattr(sys.stdout, 'buffer'):
sys.stdout.buffer.write(bytes)
else:
contents = bytes.decode(sys.stdout.encoding, 'strict')
sys.stdout.write(contents)
def watch_logs(session, tasklist, opts, poll_interval):
print("Watching logs (this may be safely interrupted)...")
@ -391,10 +406,7 @@ def watch_logs(session, tasklist, opts, poll_interval):
sys.stdout.write("\n")
sys.stdout.write("==> %s <==\n" % currlog)
lastlog = currlog
if six.PY3:
sys.stdout.buffer.write(contents)
else:
sys.stdout.write(contents)
write_to_stdout(contents)
if opts.follow: