Skip to content

Instantly share code, notes, and snippets.

@scoates
Last active December 10, 2015 09:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scoates/4414560 to your computer and use it in GitHub Desktop.
Save scoates/4414560 to your computer and use it in GitHub Desktop.
Times each line of stdin
#!/usr/bin/env python
import time
from sys import stdin, stdout, argv, exit
try:
if argv[1] == 'help' or argv[1] == '--help' or argv[1] == '-h':
print "%s: times each line of stdin.\n Optional parameter is a float of a threshold. (defaults to 2.0)" % argv[0]
exit(255);
else:
threshold = argv[1]
except IndexError:
threshold = 2.0
timeline_last = time.time()
while 1:
try:
line = stdin.readline()
except KeyboardInterrupt:
break
if not line:
break
diff = time.time() - timeline_last
if diff > threshold:
indicator = '!'
else:
indicator = ' '
print "[%s%0.2f] %s" % (indicator, diff, line.rstrip())
timeline_last = time.time()
stdout.flush()
@scoates
Copy link
Author

scoates commented Jan 2, 2013

usage example: puppet agent … | /path/to/timeline | tee /path/to/file.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment