From a25a6f6d80cb844f13540fecf616b289c42e3ebe Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Mon, 24 Feb 2014 16:03:41 -0600 Subject: [PATCH] Unbuffer log output * Force-flush log output so we don't lose log output in certain error cases. * Slow down exit paths: add sleep to die(), wait until last moment to kill child processes (including the awk log output filter) Change-Id: I1620fd33b89b237d9c2bb6206f3de2c81719f676 --- functions-common | 2 ++ stack.sh | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/functions-common b/functions-common index 79003fcfaf..4bc3bbaac5 100644 --- a/functions-common +++ b/functions-common @@ -222,6 +222,8 @@ function die() { fi backtrace 2 err $line "$*" + # Give buffers a second to flush + sleep 1 exit $exitcode } diff --git a/stack.sh b/stack.sh index 22a418f306..c95199769f 100755 --- a/stack.sh +++ b/stack.sh @@ -522,7 +522,7 @@ if [[ -n "$LOGFILE" ]]; then exec 3>&1 if [[ "$VERBOSE" == "True" ]]; then # Redirect stdout/stderr to tee to write the log file - exec 1> >( awk ' + exec 1> >( awk -v logfile=${LOGFILE} ' /((set \+o$)|xtrace)/ { next } { cmd ="date +\"%Y-%m-%d %H:%M:%S.%3N | \"" @@ -530,8 +530,9 @@ if [[ -n "$LOGFILE" ]]; then close("date +\"%Y-%m-%d %H:%M:%S.%3N | \"") sub(/^/, now) print - fflush() - }' | tee "${LOGFILE}" ) 2>&1 + print > logfile + fflush("") + }' ) 2>&1 # Set up a second fd for output exec 6> >( tee "${SUMFILE}" ) else @@ -579,21 +580,24 @@ fi # ----------------------- # Kill background processes on exit -trap clean EXIT -clean() { +trap exit_trap EXIT +function exit_trap { local r=$? - kill >/dev/null 2>&1 $(jobs -p) + echo "exit_trap called, cleaning up child processes" + kill 2>&1 $(jobs -p) exit $r } - # Exit on any errors so that errors don't compound -trap failed ERR -failed() { +trap err_trap ERR +function err_trap { local r=$? - kill >/dev/null 2>&1 $(jobs -p) set +o xtrace - [ -n "$LOGFILE" ] && echo "${0##*/} failed: full log in $LOGFILE" + if [[ -n "$LOGFILE" ]]; then + echo "${0##*/} failed: full log in $LOGFILE" + else + echo "${0##*/} failed" + fi exit $r }