Browse code

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

Dean Troyer authored on 2014/02/25 07:03:41
Showing 2 changed files
... ...
@@ -222,6 +222,8 @@ function die() {
222 222
     fi
223 223
     backtrace 2
224 224
     err $line "$*"
225
+    # Give buffers a second to flush
226
+    sleep 1
225 227
     exit $exitcode
226 228
 }
227 229
 
... ...
@@ -522,7 +522,7 @@ if [[ -n "$LOGFILE" ]]; then
522 522
     exec 3>&1
523 523
     if [[ "$VERBOSE" == "True" ]]; then
524 524
         # Redirect stdout/stderr to tee to write the log file
525
-        exec 1> >( awk '
525
+        exec 1> >( awk -v logfile=${LOGFILE} '
526 526
                 /((set \+o$)|xtrace)/ { next }
527 527
                 {
528 528
                     cmd ="date +\"%Y-%m-%d %H:%M:%S.%3N | \""
... ...
@@ -530,8 +530,9 @@ if [[ -n "$LOGFILE" ]]; then
530 530
                     close("date +\"%Y-%m-%d %H:%M:%S.%3N | \"")
531 531
                     sub(/^/, now)
532 532
                     print
533
-                    fflush()
534
-                }' | tee "${LOGFILE}" ) 2>&1
533
+                    print > logfile
534
+                    fflush("")
535
+                }' ) 2>&1
535 536
         # Set up a second fd for output
536 537
         exec 6> >( tee "${SUMFILE}" )
537 538
     else
... ...
@@ -579,21 +580,24 @@ fi
579 579
 # -----------------------
580 580
 
581 581
 # Kill background processes on exit
582
-trap clean EXIT
583
-clean() {
582
+trap exit_trap EXIT
583
+function exit_trap {
584 584
     local r=$?
585
-    kill >/dev/null 2>&1 $(jobs -p)
585
+    echo "exit_trap called, cleaning up child processes"
586
+    kill 2>&1 $(jobs -p)
586 587
     exit $r
587 588
 }
588 589
 
589
-
590 590
 # Exit on any errors so that errors don't compound
591
-trap failed ERR
592
-failed() {
591
+trap err_trap ERR
592
+function err_trap {
593 593
     local r=$?
594
-    kill >/dev/null 2>&1 $(jobs -p)
595 594
     set +o xtrace
596
-    [ -n "$LOGFILE" ] && echo "${0##*/} failed: full log in $LOGFILE"
595
+    if [[ -n "$LOGFILE" ]]; then
596
+        echo "${0##*/} failed: full log in $LOGFILE"
597
+    else
598
+        echo "${0##*/} failed"
599
+    fi
597 600
     exit $r
598 601
 }
599 602