Skip to content

Commit

Permalink
No more need for the tty/pty pair hack for instruments
Browse files Browse the repository at this point in the history
Apple fixed the buffering for the Instruments command line tool.
  • Loading branch information
Jonathan Penn committed Mar 15, 2014
1 parent a3dc464 commit f64875a
Showing 1 changed file with 3 additions and 28 deletions.
31 changes: 3 additions & 28 deletions bin/unix_instruments
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,10 @@
set -e # Bomb on any script errors

run_instruments() {
# Because instruments buffers it's output if it determines that it is being
# piped to another process, we have to use ptys to get around that so that we
# can use `tee` to save the output for grepping and print to stdout in real
# time at the same time.
#
# I don't like this because I'm hard coding a tty/pty pair in here. Suggestions
# to make this cleaner?

# Pipe to `tee` using a temporary file so everything is sent to standard out
# and we have the output to check for errors.
output=$(mktemp -t unix-instruments)
instruments "$@" &> /dev/ttyvf &
pid_instruments=$!

# Cat the instruments output to tee which outputs to stdout and saves to
# $output at the same time
cat < /dev/ptyvf | tee $output

# Clear the process id we saved when forking instruments so the cleanup
# function called on exit knows it doesn't have to kill anything
pid_instruments=0
instruments "$@" 2>&1 | tee $output

# Process the instruments output looking for anything that resembles a fail
# message
Expand All @@ -72,16 +57,6 @@ get_error_status() {
ruby -e 'exit 1 if STDIN.read =~ /Instruments Usage Error|Instruments Trace Error|^\d+-\d+-\d+ \d+:\d+:\d+ [-+]\d+ (Fail:|Error:|None: Script threw an uncaught JavaScript error)/'
}

trap cleanup_instruments EXIT
cleanup_instruments() {
# Because we fork instruments in this script, we need to clean up if it's
# still running because of an error or the user pressed Ctrl-C
if [[ $pid_instruments -gt 0 ]]; then
echo "Cleaning up instruments..."
kill -9 $pid_instruments
fi
}

# Running this file with "----test" will try to parse an error out of whatever
# is handed in to it from stdin. Use this method to double check your work if
# you need a custom "get_error_status" function above.
Expand Down

0 comments on commit f64875a

Please sign in to comment.