Skip to content

Commit d66cdaf

Browse files
committed
init_env: centralize stdout/stderr capture for all tests
- Add optional global tee capture controlled by RUN_STDOUT_ENABLE/… env vars. - Create timestamped (under or ROOT_DIR/logs). - Export the same file so child run.sh scripts append to one unified log. - Install trap to restore FDs on exit; idempotent guard prevents double-capture. Signed-off-by: Srikanth Muppandam <smuppand@qti.qualcomm.com>
1 parent e49d314 commit d66cdaf

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

Runner/init_env

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,70 @@ export __RUNNER_UTILS_BIN_DIR="$ROOT_DIR/common"
4343
# --- Ensure TOOLS is usable in all shells ---
4444
case ":$PATH:" in
4545
*":$TOOLS:"*) : ;;
46-
*) PATH="$TOOLS:$PATH"; export PATH ;;
46+
*)
47+
PATH="$TOOLS:$PATH"
48+
export PATH
49+
;;
4750
esac
4851

4952
# --- Optional: pre-check for required tools (safe no-op for minimal builds) ---
5053
if [ -f "$TOOLS/functestlib.sh" ]; then
5154
# shellcheck disable=SC1090,SC1091
5255
. "$TOOLS/functestlib.sh" >/dev/null 2>&1 || true
5356
fi
57+
58+
###############################################################################
59+
# Stdout/stderr capture (per-test folder)
60+
#
61+
# Controls (set BEFORE sourcing this file):
62+
# RUN_STDOUT_ENABLE = 1 | 0 (default: 1)
63+
# RUN_STDOUT_TAG = <string> (default: basename of $PWD)
64+
# RUN_STDOUT_FILE = <path> (default: $PWD/<tag>_stdout_<ts>.log)
65+
#
66+
# Behavior:
67+
# - Writes the capture file into the CURRENT DIRECTORY (usually the test dir).
68+
# - No global logs/stdout directory is created/used.
69+
###############################################################################
70+
_runner_stdout_cleanup() {
71+
st=$?
72+
# restore original fds (if they were saved)
73+
exec 1>&3 2>&4
74+
if [ -n "${__TEE_PID:-}" ]; then
75+
kill "$__TEE_PID" 2>/dev/null
76+
fi
77+
if [ -n "${PIPE:-}" ]; then
78+
rm -f "$PIPE" 2>/dev/null
79+
fi
80+
exit "$st"
81+
}
82+
83+
if [ "${RUN_STDOUT_ENABLE:-1}" -eq 1 ] && [ -z "${__RUN_STDOUT_ACTIVE:-}" ]; then
84+
_tag="${RUN_STDOUT_TAG:-$(basename "$(pwd)")}"
85+
_ts="$(date +%Y%m%d-%H%M%S)"
86+
RUN_STDOUT_FILE="${RUN_STDOUT_FILE:-$(pwd)/${_tag}_stdout_${_ts}.log}"
87+
export RUN_STDOUT_FILE
88+
89+
# Save original stdout/stderr
90+
exec 3>&1 4>&2
91+
92+
if command -v tee >/dev/null 2>&1; then
93+
PIPE="$(mktemp -u "/tmp/stdout_pipe.XXXXXX")"
94+
if mkfifo "$PIPE" 2>/dev/null; then
95+
( tee -a "$RUN_STDOUT_FILE" >&3 ) < "$PIPE" &
96+
__TEE_PID=$!
97+
exec > "$PIPE" 2>&1
98+
__RUN_STDOUT_ACTIVE=1
99+
trap _runner_stdout_cleanup EXIT INT TERM
100+
else
101+
# Fallback: file-only capture
102+
exec >> "$RUN_STDOUT_FILE" 2>&1
103+
__RUN_STDOUT_ACTIVE=1
104+
trap _runner_stdout_cleanup EXIT INT TERM
105+
fi
106+
else
107+
# Fallback: file-only capture
108+
exec >> "$RUN_STDOUT_FILE" 2>&1
109+
__RUN_STDOUT_ACTIVE=1
110+
trap _runner_stdout_cleanup EXIT INT TERM
111+
fi
112+
fi

0 commit comments

Comments
 (0)