blob: 2b2c8d81cd25f3361e01dc58d33fe88c38285b3d [file] [log] [blame]
Zhuoyao Zhang0edf1d32024-05-21 23:57:11 +00001#!/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(
20tool_tag="$1"
21shift
22tool_binary="$1"
23shift
24
25# If the logger is not configured, run the original command and return.
26if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then
27 "${tool_binary}" "${@}"
28 exit $?
29fi
30
31# Otherwise, run the original command and call the logger when done.
32start_time=$(date +%s.%N)
33logger=${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.
38trap '
39exit_code=$?;
40# Remove the trap to prevent duplicate log.
41trap - 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 &
50exit ${exit_code}
51' SIGINT SIGTERM SIGQUIT EXIT
52
53# Run the original command.
54"${tool_binary}" "${@}"
55)