Skip to content

Instantly share code, notes, and snippets.

@jigpu
Created March 22, 2017 22:10
Show Gist options
  • Save jigpu/a295c4d13c6b3b5c2ac190dffa62347b to your computer and use it in GitHub Desktop.
Save jigpu/a295c4d13c6b3b5c2ac190dffa62347b to your computer and use it in GitHub Desktop.
Script to timestamp stdout and stderr from a process
#!/usr/bin/env python2
from __future__ import print_function
import sys
import datetime
import time
from subprocess import Popen, PIPE
from Queue import Queue, Empty
from threading import Thread
def enqueue_output(out, queue, formatter):
for line in iter(out.readline, b''):
queue.put(formatter(line))
out.close()
def format_line(line, prefix, timestamp=False):
if timestamp:
stamp = time.time()
else:
stamp = datetime.datetime.now()
return "{}\t{}\t{}".format(prefix, stamp, line)
def print_output(queue, flush=False):
try:
while queue.qsize() > 0 or flush:
lout = queue.get_nowait()
dest = sys.stdout if lout.startswith("STDOUT") else sys.stderr
print(lout, end='', file=dest)
except Empty:
pass
def start(timestamp, args):
proc = Popen(args, stdin=sys.stdin, stdout=PIPE, stderr=PIPE, bufsize=1)
q = Queue()
tout = Thread(target=enqueue_output, args=(proc.stdout, q, lambda x: format_line(x, "STDOUT", timestamp)))
terr = Thread(target=enqueue_output, args=(proc.stderr, q, lambda x: format_line(x, "STDERR", timestamp)))
tout.daemon = True
terr.daemon = True
tout.start()
terr.start()
return (proc, q)
def main():
argend = 0
if "--" in sys.argv:
argend = sys.argv.index("--")
args = sys.argv[argend+1:]
t = False
for arg in sys.argv[1:argend]:
if arg == "-t": t = True # Print out UNIX timestamp
elif arg == "-d": t = False # Print out date + time string
else:
print("Unknown argument: {}".format(arg))
return 1
proc, q = start(t, args)
ret = None
while True:
print_output(q)
if proc.poll() is not None:
ret = proc.returncode
if ret is not None and proc.stdout.closed and proc.stderr.closed:
break
print_output(q, flush=True)
return ret
if __name__=="__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment