Skip to content

Commit 2ef8a98

Browse files
authored
Merge pull request qualcomm-linux#199 from smuppand/stdout-log
Centralize stdout/stderr capture in init_env and make run-test.sh inherit unified log
2 parents f1674ac + 163182b commit 2ef8a98

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
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

Runner/run-test.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
99
# Set TOOLS path to utils under the script directory
1010
TOOLS="$SCRIPT_DIR/utils"
1111

12+
# Disable wrapper-level capture; each test will capture in its own folder
13+
export RUN_STDOUT_ENABLE=0
14+
unset RUN_STDOUT_TAG RUN_STDOUT_FILE
15+
1216
# Safely source init_env from the same directory as this script
1317
if [ -f "$SCRIPT_DIR/init_env" ]; then
1418
# shellcheck source=/dev/null
@@ -46,7 +50,11 @@ execute_test_case() {
4650
run_script="$test_path/run.sh"
4751
if [ -f "$run_script" ]; then
4852
log "Executing test case: $test_name"
49-
(cd "$test_path" && sh "./run.sh" "$@")
53+
(
54+
cd "$test_path" || exit 2
55+
# Enable per-test capture in the test folder with a clear tag
56+
RUN_STDOUT_ENABLE=1 RUN_STDOUT_TAG="$test_name" sh "./run.sh" "$@"
57+
)
5058
res_file="$test_path/$test_name.res"
5159
if [ -f "$res_file" ]; then
5260
if grep -q "SKIP" "$res_file"; then
@@ -131,7 +139,8 @@ Usage:
131139
Notes:
132140
- Extra args are forwarded only when a single <testcase_name> is specified.
133141
- 'all' runs every test and does not accept additional args.
134-
- Use -h or --help to display this message.
142+
- Each test captures stdout/stderr next to its .res file as:
143+
<testname>_stdout_<timestamp>.log
135144
EOF
136145
}
137146

0 commit comments

Comments
 (0)