From a98cb8aaf12aab62ecbd895df0f771a58cb30336 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 23 Aug 2024 19:49:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20use=20var=20`BASH=5FLINENO`=20inste?= =?UTF-8?q?ad=20of=20`trap=5Ferror=5Finfo::get=5Fcaller=5Fline=5Fno`=20?= =?UTF-8?q?=F0=9F=90=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- lib/trap_error_info.sh | 40 +++------------------------------------- 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 6ce4ac7..ed66e9b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ GitHub repo size

-🚼 Bash Buddy(aka. BaBy) contains `bash` libs and tools that extracted from `CI` scripts of my projects. +🚼 Bash Buddy(aka. BaBy) contains `bash` libs and tools that extracted from `CI` scripts of my open-source projects. ----------------------------------- diff --git a/lib/trap_error_info.sh b/lib/trap_error_info.sh index 1fbd4ed..d4917eb 100644 --- a/lib/trap_error_info.sh +++ b/lib/trap_error_info.sh @@ -23,36 +23,6 @@ readonly __source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=${__source_guard_84 set -eEu -o pipefail -o functrace -# trap_error_info::get_caller_line_no $level -# -# level 0 is caller of `trap_error_info::get_caller_line_no`. -# -# CAUTION: do NOT call this function in sub-shell! -# e.g. $(trap_error_info::get_caller_line_no) -# -# -# related info: -# -# What is the "caller" command? -# https://unix.stackexchange.com/questions/19323 -# Bash - Caller - Stack Trace (Builtin command) -# https://datacadamia.com/lang/bash/caller -# Get the name of the caller script in bash script -# https://stackoverflow.com/questions/20572934 -# -trap_error_info::get_caller_line_no() { - local level="$1" - - TRAP_ERROR_INFO_CALLER_LINE_NO='' - - # level 0 of caller means this `trap_error_info::get_caller_line_no` self - # set level 1 to skip `trap_error_info::get_caller_line_no` self - local line_no _ - read -r line_no _ < <(caller $((level + 1))) - - TRAP_ERROR_INFO_CALLER_LINE_NO="$line_no" -} - # show stack trace. # # usage: @@ -67,20 +37,16 @@ trap_error_info::get_caller_line_no() { # example: # foo_function(bar.sh:42) # -# CAUTION: do NOT call this function in sub-shell! -# e.g. $(trap_error_info::get_stack_trace) -# trap_error_info::get_stack_trace() { local indentation="${1:-}" hide_level="${2:-0}" local func_stack_size="${#FUNCNAME[@]}" TRAP_ERROR_INFO_STACK_TRACE='' - local i stack_trace nl=$'\n' + local i stack_trace= for ((i = hide_level + 1; i < func_stack_size; i++)); do - trap_error_info::get_caller_line_no "$((i - 1))" - - stack_trace="${stack_trace:+$stack_trace$nl}${indentation}${FUNCNAME[i]}(${BASH_SOURCE[i]}:${TRAP_ERROR_INFO_CALLER_LINE_NO})" + [ -n "${stack_trace:-}" ] && printf -v stack_trace '%s\n' "$stack_trace" + printf -v stack_trace '%s%s%s(%s:%s)' "$stack_trace" "$indentation" "${FUNCNAME[i]}" "${BASH_SOURCE[i]}" "${BASH_LINENO[i - 1]}" done TRAP_ERROR_INFO_STACK_TRACE="$stack_trace"