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