1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #!/bin/bash
- set -euo pipefail
- if [ "$#" == "0" ]
- then
- echo capture
- exit 0
- fi
- . "${BASH_SOURCE[0]%/*}"/helpers/common.sh
- PROG="$1"
- SUBPROG="$(get_subprog "$@")"
- TIMESTAMP=$(date +%Y%m%d_%H%M%S_%N)
- LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"/"$TIMESTAMP"
- mkdir -p "$LOG_DIR"
- PTYA="$LOG_DIR"/ptyA
- PTYB="$LOG_DIR"/ptyB
- OUT="$LOG_DIR"/stdout
- ERR="$LOG_DIR"/stderr
- DAT="$LOG_DIR"/dat
- INF="$LOG_DIR"/info
- CMD="$(shell_escape "$@")"
- info(){
- cat <<EOF > "$INF"
- $CMD
- status=$?
- EOF
- exec 1>&- 2>&-
- [ "${socat:-}" ] && kill "$socat"
- wait
- }
- trap 'info' EXIT
- exec 2> >(tee -a "$DAT" "$ERR")
- if [ -t 1 ] && ! [ "$PROG" = "sudo" ] && which socat 1>/dev/null 2>&1
- then
- socat PTY,rawer,link="$PTYA" PTY,rawer,link="$PTYB" &
- socat="$!"
- for pty in "$PTYA" "$PTYB"
- do
- while ! [ -e "$pty" ]; do sleep 0.01; done
- stty -F "$pty" rows "$LINES" cols "$COLUMNS"
- done
- tee -a "$DAT" "$OUT" 2>/dev/null < "$PTYB" &
- exec 1> "$PTYA"
- else
- exec 1> >(tee -a "$DAT" "$OUT")
- fi
- "$@"
|