Zhuoyao Zhang | 0edf1d3 | 2024-05-21 23:57:11 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (C) 2024 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Run commands in a subshell for us to handle forced terminations with a trap |
| 18 | # handler. |
| 19 | ( |
| 20 | tool_tag="$1" |
| 21 | shift |
| 22 | tool_binary="$1" |
| 23 | shift |
| 24 | |
| 25 | # If the logger is not configured, run the original command and return. |
| 26 | if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then |
| 27 | "${tool_binary}" "${@}" |
| 28 | exit $? |
| 29 | fi |
| 30 | |
| 31 | # Otherwise, run the original command and call the logger when done. |
| 32 | start_time=$(date +%s.%N) |
| 33 | logger=${ANDROID_TOOL_LOGGER} |
| 34 | |
| 35 | # Install a trap to call the logger even when the process terminates abnormally. |
| 36 | # The logger is run in the background and its output suppressed to avoid |
| 37 | # interference with the user flow. |
| 38 | trap ' |
| 39 | exit_code=$?; |
| 40 | # Remove the trap to prevent duplicate log. |
| 41 | trap - EXIT; |
| 42 | "${logger}" \ |
| 43 | --tool_tag="${tool_tag}" \ |
| 44 | --start_timestamp="${start_time}" \ |
| 45 | --end_timestamp="$(date +%s.%N)" \ |
| 46 | --tool_args="$*" \ |
| 47 | --exit_code="${exit_code}" \ |
| 48 | ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \ |
| 49 | > /dev/null 2>&1 & |
| 50 | exit ${exit_code} |
| 51 | ' SIGINT SIGTERM SIGQUIT EXIT |
| 52 | |
| 53 | # Run the original command. |
| 54 | "${tool_binary}" "${@}" |
| 55 | ) |