From b1d24ef063024cf52096f9b634c769635c690823 Mon Sep 17 00:00:00 2001 From: pika <759349196@qq.com> Date: Wed, 1 Apr 2026 16:19:36 +0800 Subject: [PATCH] adjust script log output --- scripts/all_build.sh | 8 ++++---- scripts/install.sh | 29 ++++++++++++++++++++++++----- scripts/lib/log.sh | 29 +++++++++++++++++++++-------- scripts/uninstall.sh | 29 ++++++++++++++++++++++++----- 4 files changed, 73 insertions(+), 22 deletions(-) diff --git a/scripts/all_build.sh b/scripts/all_build.sh index 39e44c3..8a2aa17 100755 --- a/scripts/all_build.sh +++ b/scripts/all_build.sh @@ -29,7 +29,7 @@ run_step() { log_info "[$STEP_NO] $title" if "$@" >"$log_path" 2>&1; then - log_success "$title completed" + log_info "$title completed" return 0 fi @@ -47,9 +47,9 @@ STEP_NO="1/3" log_info "[$STEP_NO] Clean build artifacts" if [[ -d "$BIN_DIR" ]]; then rm -rf "$BIN_DIR" - log_success "Removed $BIN_DIR" + log_info "Removed $BIN_DIR" else - log_success "No existing build artifacts" + log_info "No existing build artifacts" fi STEP_NO="2/3" @@ -58,5 +58,5 @@ run_step "Run tests" "test" make test STEP_NO="3/3" run_step "Build CLI" "build" make build -log_success "Binary ready at $BIN_PATH" +log_info "Binary ready at $BIN_PATH" print_run_summary "Build completed" diff --git a/scripts/install.sh b/scripts/install.sh index 4c05384..4077aee 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -5,13 +5,32 @@ set -euo pipefail _log() { local level="$1" local color="$2" - shift 2 - printf '\033[%sm[%s]\033[0m %s %s\n' "$color" "$level" "$(date '+%Y-%m-%d %H:%M:%S')" "$*" + local stream="${3:-stdout}" + shift 3 + + local line + line="$(date '+%Y-%m-%d %H:%M:%S') [${level}] $*" + + if [[ "$stream" == "stderr" ]]; then + if [[ -t 2 ]]; then + printf '\033[%sm%s\033[0m\n' "$color" "$line" >&2 + else + printf '%s\n' "$line" >&2 + fi + return + fi + + if [[ -t 1 ]]; then + printf '\033[%sm%s\033[0m\n' "$color" "$line" + return + fi + + printf '%s\n' "$line" } -log_info() { _log "INFO" "34" "$@"; } -log_success() { _log "SUCCESS" "32" "$@"; } -log_error() { _log "ERROR" "31" "$@" >&2; } +log_info() { _log "INFO" "32" "stdout" "$@"; } +log_success() { log_info "$@"; } +log_error() { _log "ERROR" "31" "stderr" "$@"; } default_shell_rc() { case "$(basename "${SHELL:-}")" in diff --git a/scripts/lib/log.sh b/scripts/lib/log.sh index 795ac19..1649bab 100644 --- a/scripts/lib/log.sh +++ b/scripts/lib/log.sh @@ -5,21 +5,34 @@ SCRIPT_START_TS="$(date +%s)" _log() { local level="$1" local color="$2" - shift 2 + local stream="${3:-stdout}" + shift 3 local now now="$(date '+%Y-%m-%d %H:%M:%S')" + local line + line="${now} [${level}] $*" + + if [[ "$stream" == "stderr" ]]; then + if [[ -t 2 ]]; then + printf '\033[%sm%s\033[0m\n' "$color" "$line" >&2 + else + printf '%s\n' "$line" >&2 + fi + return + fi if [[ -t 1 ]]; then - printf '\033[%sm[%s]\033[0m %s %s\n' "$color" "$level" "$now" "$*" - else - printf '[%s] %s %s\n' "$level" "$now" "$*" + printf '\033[%sm%s\033[0m\n' "$color" "$line" + return fi + + printf '%s\n' "$line" } -log_info() { _log "INFO" "34" "$@"; } -log_success() { _log "SUCCESS" "32" "$@"; } -log_error() { _log "ERROR" "31" "$@" >&2; } +log_info() { _log "INFO" "32" "stdout" "$@"; } +log_success() { log_info "$@"; } +log_error() { _log "ERROR" "31" "stderr" "$@"; } print_run_summary() { local message="$1" @@ -27,7 +40,7 @@ print_run_summary() { elapsed="$(( $(date +%s) - SCRIPT_START_TS ))" end_at="$(date '+%Y-%m-%d %H:%M:%S %Z')" - log_success "$message" + log_info "$message" log_info "Elapsed: ${elapsed}s" log_info "Completed at: ${end_at}" } diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index 6ad451b..14deb4f 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -5,13 +5,32 @@ set -euo pipefail _log() { local level="$1" local color="$2" - shift 2 - printf '\033[%sm[%s]\033[0m %s %s\n' "$color" "$level" "$(date '+%Y-%m-%d %H:%M:%S')" "$*" + local stream="${3:-stdout}" + shift 3 + + local line + line="$(date '+%Y-%m-%d %H:%M:%S') [${level}] $*" + + if [[ "$stream" == "stderr" ]]; then + if [[ -t 2 ]]; then + printf '\033[%sm%s\033[0m\n' "$color" "$line" >&2 + else + printf '%s\n' "$line" >&2 + fi + return + fi + + if [[ -t 1 ]]; then + printf '\033[%sm%s\033[0m\n' "$color" "$line" + return + fi + + printf '%s\n' "$line" } -log_info() { _log "INFO" "34" "$@"; } -log_success() { _log "SUCCESS" "32" "$@"; } -log_error() { _log "ERROR" "31" "$@" >&2; } +log_info() { _log "INFO" "32" "stdout" "$@"; } +log_success() { log_info "$@"; } +log_error() { _log "ERROR" "31" "stderr" "$@"; } default_shell_rc() { case "$(basename "${SHELL:-}")" in