Skip to content

Commit ad10249

Browse files
authored
ci: introduce io::log_cmdline (#6264)
Add `io::log_cmdline` for logging command execution. It prefixes the command with `${PS4}`, just like `set -x` would in the shell. This helps to delineate the command from normal output. Also print the command in a bold typeface when possible, making it even easier to distinguish.
1 parent 7f5d46f commit ad10249

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

ci/kokoro/docker/build-in-docker-bazel.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ readonly BINARY_DIR="$2"
3232
# This script is designed to work in the context created by the
3333
# ci/Dockerfile.* build scripts.
3434

35-
echo
3635
io::log_yellow "Starting docker build with ${NCPU} cores"
37-
echo
3836

3937
echo "================================================================"
4038
readonly BAZEL_BIN="/usr/local/bin/bazel"
@@ -110,7 +108,7 @@ io::log_yellow "Verify generator golden file md5 hashes"
110108

111109
echo "================================================================"
112110
io::log "Compiling and running unit tests"
113-
echo "bazel ${BAZEL_VERB}" "${bazel_args[@]}"
111+
io::log_cmdline "bazel ${BAZEL_VERB}" "${bazel_args[@]}"
114112
"${BAZEL_BIN}" ${BAZEL_VERB} \
115113
"${bazel_args[@]}" "--test_tag_filters=-integration-test" ...
116114

ci/kokoro/docker/build-in-docker-cmake.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ fi
2929
readonly SOURCE_DIR="$1"
3030
readonly BINARY_DIR="$2"
3131

32-
echo
3332
io::log_yellow "Starting docker build with ${NCPU} cores"
34-
echo
3533

3634
# Run the configure / compile / test cycle inside a docker image.
3735
# This script is designed to work in the context created by the
@@ -586,7 +584,7 @@ if [[ "${CHECK_STYLE:-}" == "yes" && "${RUNNING_CI}" == "yes" ]]; then
586584
git diff --ignore-submodules=all --color --exit-code .
587585
fi
588586

589-
if command -v ccache; then
587+
if command -v ccache >/dev/null 2>&1; then
590588
echo "================================================================"
591589
io::log_yellow "ccache stats"
592590
ccache --show-stats

ci/kokoro/docker/build.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ fi
364364

365365
echo "================================================================"
366366
io::log_yellow "Creating Docker image with all the development tools."
367-
echo " docker build ${docker_build_flags[*]} ci"
368-
io::log_yellow "Logging to ${BUILD_OUTPUT}/create-build-docker-image.log"
369367
# We do not want to print the log unless there is an error, so disable the -e
370368
# flag. Later, we will want to print out the emulator(s) logs *only* if there
371369
# is an error, so disabling from this point on is the right choice.
372370
set +e
373371
mkdir -p "${BUILD_OUTPUT}"
372+
io::log_yellow "Logging to ${BUILD_OUTPUT}/create-build-docker-image.log"
373+
io::log_cmdline "docker build ${docker_build_flags[*]} ci"
374374
if timeout 3600s docker build "${docker_build_flags[@]}" ci \
375375
>"${BUILD_OUTPUT}/create-build-docker-image.log" 2>&1 </dev/null; then
376376
update_cache="true"
@@ -637,7 +637,7 @@ else
637637
fi
638638

639639
# Run the docker image with that giant collection of flags.
640-
echo docker run "${docker_flags[@]}" "${IMAGE}:latest" "${commands[@]}"
640+
io::log_cmdline docker run "${docker_flags[@]}" "${IMAGE}:latest" "${commands[@]}"
641641
docker run "${docker_flags[@]}" "${IMAGE}:latest" "${commands[@]}"
642642

643643
exit_status=$?
@@ -684,7 +684,6 @@ else
684684
io::log_yellow "Not a CI build; skipping artifact cleanup"
685685
fi
686686

687-
echo
688687
echo "================================================================"
689688
if [[ ${exit_status} -eq 0 ]]; then
690689
io::log_green "Build script finished successfully."

ci/lib/io.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,38 @@ if ((CI_LIB_IO_SH__++ != 0)); then
2626
return 0
2727
fi # include guard
2828

29-
# Callers may use these IO_COLOR_* variables directly, but it is recommended to
30-
# use the logging functions below instead. For example, prefer io::log_green
31-
# over IO_COLOR_GREEN.
29+
# Callers may use these IO_* variables directly, but should prefer to use the
30+
# logging functions below instead. For example, prefer `io::log_green "..."`
31+
# over `echo "${IO_COLOR_GREEN}...${IO_RESET}"`.
3232
if [ -t 0 ] && command -v tput >/dev/null; then
33+
readonly IO_BOLD="$(tput bold)"
3334
readonly IO_COLOR_RED="$(tput setaf 1)"
3435
readonly IO_COLOR_GREEN="$(tput setaf 2)"
3536
readonly IO_COLOR_YELLOW="$(tput setaf 3)"
36-
readonly IO_COLOR_RESET="$(tput sgr0)"
37+
readonly IO_RESET="$(tput sgr0)"
3738
else
39+
readonly IO_BOLD=""
3840
readonly IO_COLOR_RED=""
3941
readonly IO_COLOR_GREEN=""
4042
readonly IO_COLOR_YELLOW=""
41-
readonly IO_COLOR_RESET=""
43+
readonly IO_RESET=""
4244
fi
4345

44-
# Logs a message using the given color. The first argument must be one of the
45-
# IO_COLOR_* variables defined above, such as "${IO_COLOR_YELLOW}". The
46-
# remaining arguments will be logged in the given color. The log message will
47-
# also have an RFC-3339 timestamp prepended (in UTC).
46+
# Logs a message using the given terminal capability. The first argument
47+
# must be one of the IO_* variables defined above, such as "${IO_COLOR_RED}".
48+
# The remaining arguments will be logged using the given capability. The
49+
# log message will also have an RFC-3339 timestamp prepended (in UTC).
4850
function io::internal::log_impl() {
49-
local color="$1"
51+
local termcap="$1"
5052
shift
5153
local timestamp
5254
timestamp="$(date -u "+%Y-%m-%dT%H:%M:%SZ")"
53-
echo "${color}${timestamp}:" "$@" "${IO_COLOR_RESET}"
55+
echo "${termcap}${timestamp}: $*${IO_RESET}"
5456
}
5557

5658
# Logs the given message with normal coloring and a timestamp.
5759
function io::log() {
58-
io::internal::log_impl "${IO_COLOR_RESET}" "$@"
60+
io::internal::log_impl "${IO_RESET}" "$@"
5961
}
6062

6163
# Logs the given message in green with a timestamp.
@@ -73,6 +75,12 @@ function io::log_red() {
7375
io::internal::log_impl "${IO_COLOR_RED}" "$@"
7476
}
7577

78+
# Logs the arguments, in bold with a timestamp, like they were a command
79+
# executed under "set -x" in the shell (i.e., with a ${PS4} prefix).
80+
function io::log_cmdline() {
81+
io::internal::log_impl "${IO_BOLD}" "${PS4}$*"
82+
}
83+
7684
# Logs an "H1" heading. This looks like a blank line, followed by the message
7785
# in a double-lined box.
7886
#

google/cloud/storage/tools/run_emulator_utils.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ EMULATOR_PID=0
2929
# None
3030
################################################
3131
kill_emulator() {
32-
echo "${IO_COLOR_GREEN}[ -------- ]${IO_COLOR_RESET} Integration test environment tear-down."
32+
echo "${IO_COLOR_GREEN}[ -------- ]${IO_RESET} Integration test environment tear-down."
3333
echo -n "Killing emulator server [${EMULATOR_PID}] ... "
3434
kill "${EMULATOR_PID}"
3535
wait "${EMULATOR_PID}" >/dev/null 2>&1
3636
echo "done."
37-
echo "${IO_COLOR_GREEN}[ ======== ]${IO_COLOR_RESET} Integration test environment tear-down."
37+
echo "${IO_COLOR_GREEN}[ ======== ]${IO_RESET} Integration test environment tear-down."
3838

3939
}
4040

@@ -55,7 +55,7 @@ kill_emulator() {
5555
start_emulator() {
5656
local port="${1:-0}"
5757

58-
echo "${IO_COLOR_GREEN}[ -------- ]${IO_COLOR_RESET} Integration test environment set-up"
58+
echo "${IO_COLOR_GREEN}[ -------- ]${IO_RESET} Integration test environment set-up"
5959
echo "Launching Cloud Storage emulator in the background"
6060
trap kill_emulator EXIT
6161

@@ -79,7 +79,7 @@ start_emulator() {
7979
done
8080

8181
if [[ -z "${emulator_port}" ]]; then
82-
echo "${IO_COLOR_RED}Cannot find listening port for emulator.${IO_COLOR_RESET}" >&2
82+
echo "${IO_COLOR_RED}Cannot find listening port for emulator.${IO_RESET}" >&2
8383
cat gcs_emulator.log
8484
exit 1
8585
fi
@@ -99,7 +99,7 @@ start_emulator() {
9999
done
100100

101101
if [[ "${connected}" = "no" ]]; then
102-
echo "${IO_COLOR_RED}Cannot connect to emulator; aborting test.${IO_COLOR_RESET}" >&2
102+
echo "${IO_COLOR_RED}Cannot connect to emulator; aborting test.${IO_RESET}" >&2
103103
cat gcs_emulator.log
104104
exit 1
105105
else
@@ -114,11 +114,11 @@ start_emulator() {
114114
echo "Successfully connected to gRPC server at port ${grpc_port}"
115115
else
116116
echo "${IO_COLOR_RED}${grpc_port} must be an integer" >&2
117-
echo "${IO_COLOR_RED}Cannot connect to gRPC server; aborting test.${IO_COLOR_RESET}" >&2
117+
echo "${IO_COLOR_RED}Cannot connect to gRPC server; aborting test.${IO_RESET}" >&2
118118
cat gcs_emulator.log
119119
exit 1
120120
fi
121121
export CLOUD_STORAGE_GRPC_ENDPOINT="localhost:${grpc_port}"
122122

123-
echo "${IO_COLOR_GREEN}[ ======== ]${IO_COLOR_RESET} Integration test environment set-up."
123+
echo "${IO_COLOR_GREEN}[ ======== ]${IO_RESET} Integration test environment set-up."
124124
}

0 commit comments

Comments
 (0)