From 908811992363d46e285b63177e95f8fc85d6352d Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Fri, 2 Feb 2024 02:28:04 -0800 Subject: [PATCH] output debug message in a better format. --- src/docker_hub_rate.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/docker_hub_rate.sh b/src/docker_hub_rate.sh index 1f4ffa0..69a7ae9 100755 --- a/src/docker_hub_rate.sh +++ b/src/docker_hub_rate.sh @@ -49,15 +49,25 @@ _docker_hub_rate_read_rate() { docker_hub_rate() { local IMAGE="${1:-ratelimitpreview/test}" local USER_AND_PASS="${2}" - if ! log INFO "" 1>/dev/null 2>/dev/null; then - # Assume the error is due to log function is not available. + if ! type log 1>/dev/null 2>&1; then log() { echo "${*}" >&2 } fi + if ! type log_lines 1>/dev/null 2>&1; then + # Usage: echo "${LOGS}" | log_lines LEVLE + log_lines() { + local LEVEL="${1}"; + while read -r LINE; do + [ -z "${LINE}" ] && continue; + log "${LEVEL}" "${LINE}"; + done + } + fi local RESPONSE= if ! RESPONSE=$(_docker_hub_rate_token "${IMAGE}" "${USER_AND_PASS}"); then - log DEBUG "_docker_hub_rate_token error: RESPONSE=${RESPONSE}" + log DEBUG "_docker_hub_rate_token error: RESPONSE=" + echo "${RESPONSE}" | log_lines DEBUG echo "[GET TOKEN RESPONSE ERROR]" return 1 fi @@ -73,7 +83,8 @@ docker_hub_rate() { echo "0" return 0 fi - log DEBUG "_docker_hub_rate_read_rate error: RESPONSE=${RESPONSE}" + log DEBUG "_docker_hub_rate_read_rate error: RESPONSE=" + echo "${RESPONSE}" | log_lines DEBUG echo "[GET RATE RESPONSE ERROR]" return 1 fi