Skip to content

Commit

Permalink
refactor: use var BASH_LINENO instead of `trap_error_info::get_call…
Browse files Browse the repository at this point in the history
…er_line_no` 🐚
  • Loading branch information
oldratlee committed Aug 27, 2024
1 parent 5f66e68 commit a98cb8a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a href="https://github.com/foldright/bash-buddy"><img src="https://img.shields.io/github/repo-size/foldright/bash-buddy" alt="GitHub repo size"></a>
</p>

🚼 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.

-----------------------------------

Expand Down
40 changes: 3 additions & 37 deletions lib/trap_error_info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down

0 comments on commit a98cb8a

Please sign in to comment.