aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtem Serov <artem.serov@linaro.org>2022-02-02 09:25:05 +0000
committerLinaro Android Code Review <android-review@review.linaro.org>2022-02-02 09:25:05 +0000
commita0d57d8a810aedf1a7ba64f4832d6056a1e490a1 (patch)
tree335be8106ad9168608632cdb789d7234a47016db
parent46cd1666075c7e1d304eb1301a17773b83e55ccf (diff)
parent4eefd744f483488e3c7706941cc7a1f4f77554fb (diff)
downloadart-build-scripts-a0d57d8a810aedf1a7ba64f4832d6056a1e490a1.tar.gz
Merge "utils: Support nesting in {start,stop}_logging"
-rw-r--r--utils/utils.sh50
1 files changed, 31 insertions, 19 deletions
diff --git a/utils/utils.sh b/utils/utils.sh
index 8a401a22..ed073a44 100644
--- a/utils/utils.sh
+++ b/utils/utils.sh
@@ -428,37 +428,49 @@ exists() {
fi
}
-current_log_file=""
+declare -a logging_fd_stack
# Start logging all standard output and error onto a file.
# Arguments:
# ${1} - output file name.
start_logging() {
- if [[ -z "${current_log_file}" ]]; then
- current_log_file=$1
- else
- log E "Trying to start logging into $1 whilst logging into ${current_log_file}."
- log E "You must stop the previous logging before starting new logging."
- abort
- fi
+ local saved_stdout saved_stderr
- # Save a copy file descriptors 1 and 2 (stdin and stderr) into fd 3 and 4.
- exec 3>&1 4>&2
+ # Move the stdout and stderr file descriptors to new file descriptors
+ # allocated by Bash.
+ # shellcheck disable=SC2093
+ exec {saved_stdout}>&1- {saved_stderr}>&2-
- # Standard output is directed to a subprocess, tee, which sends the output
- # to both a file and the original stdout.
- exec > >(safe tee -i -a "$1")
- # Same for standard error.
+ # The standard output is redirected to a subprocess, tee, which sends the
+ # output to both a file and the original stdout.
+ exec > >(safe tee -i -a "$1" >&${saved_stdout})
+ # Same for the standard error.
exec 2>&1
+
+ # Save the file descriptors on the fd stack.
+ logging_fd_stack+=(${saved_stdout} ${saved_stderr})
}
stop_logging() {
- # Restore file descriptors.
- exec >&3 2>&4
- # Destroy copies.
- exec 3>&- 4>&-
+ local cur_stack_size=${#logging_fd_stack[@]}
+
+ if [[ ${cur_stack_size} -eq 0 ]]; then
+ # Not currently logging, nothing to do.
+ return 0
+ elif ((cur_stack_size % 2 != 0)); then
+ # This should never happen (we always push two fd's to the stack).
+ abort
+ fi
+
+ local saved_stdout=${logging_fd_stack[cur_stack_size - 2]}
+ local saved_stderr=${logging_fd_stack[cur_stack_size - 1]}
+
+ # Move the saved file descriptors back to stdout / stderr and pop them from
+ # the stack.
+ exec >&"${saved_stdout}"- 2>&"${saved_stderr}"-
- current_log_file=""
+ unset -v "logging_fd_stack[cur_stack_size - 2]"
+ unset -v "logging_fd_stack[cur_stack_size - 1]"
}
enable_verbose() {