Skip to content

Commit a98cb8a

Browse files
committed
refactor: use var BASH_LINENO instead of trap_error_info::get_caller_line_no 🐚
1 parent 5f66e68 commit a98cb8a

File tree

2 files changed

+4
-38
lines changed

2 files changed

+4
-38
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<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>
1010
</p>
1111

12-
🚼 Bash Buddy(aka. BaBy) contains `bash` libs and tools that extracted from `CI` scripts of my projects.
12+
🚼 Bash Buddy(aka. BaBy) contains `bash` libs and tools that extracted from `CI` scripts of my open-source projects.
1313

1414
-----------------------------------
1515

lib/trap_error_info.sh

+3-37
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,6 @@ readonly __source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=${__source_guard_84
2323

2424
set -eEu -o pipefail -o functrace
2525

26-
# trap_error_info::get_caller_line_no $level
27-
#
28-
# level 0 is caller of `trap_error_info::get_caller_line_no`.
29-
#
30-
# CAUTION: do NOT call this function in sub-shell!
31-
# e.g. $(trap_error_info::get_caller_line_no)
32-
#
33-
#
34-
# related info:
35-
#
36-
# What is the "caller" command?
37-
# https://unix.stackexchange.com/questions/19323
38-
# Bash - Caller - Stack Trace (Builtin command)
39-
# https://datacadamia.com/lang/bash/caller
40-
# Get the name of the caller script in bash script
41-
# https://stackoverflow.com/questions/20572934
42-
#
43-
trap_error_info::get_caller_line_no() {
44-
local level="$1"
45-
46-
TRAP_ERROR_INFO_CALLER_LINE_NO=''
47-
48-
# level 0 of caller means this `trap_error_info::get_caller_line_no` self
49-
# set level 1 to skip `trap_error_info::get_caller_line_no` self
50-
local line_no _
51-
read -r line_no _ < <(caller $((level + 1)))
52-
53-
TRAP_ERROR_INFO_CALLER_LINE_NO="$line_no"
54-
}
55-
5626
# show stack trace.
5727
#
5828
# usage:
@@ -67,20 +37,16 @@ trap_error_info::get_caller_line_no() {
6737
# example:
6838
# foo_function(bar.sh:42)
6939
#
70-
# CAUTION: do NOT call this function in sub-shell!
71-
# e.g. $(trap_error_info::get_stack_trace)
72-
#
7340
trap_error_info::get_stack_trace() {
7441
local indentation="${1:-}" hide_level="${2:-0}"
7542
local func_stack_size="${#FUNCNAME[@]}"
7643

7744
TRAP_ERROR_INFO_STACK_TRACE=''
7845

79-
local i stack_trace nl=$'\n'
46+
local i stack_trace=
8047
for ((i = hide_level + 1; i < func_stack_size; i++)); do
81-
trap_error_info::get_caller_line_no "$((i - 1))"
82-
83-
stack_trace="${stack_trace:+$stack_trace$nl}${indentation}${FUNCNAME[i]}(${BASH_SOURCE[i]}:${TRAP_ERROR_INFO_CALLER_LINE_NO})"
48+
[ -n "${stack_trace:-}" ] && printf -v stack_trace '%s\n' "$stack_trace"
49+
printf -v stack_trace '%s%s%s(%s:%s)' "$stack_trace" "$indentation" "${FUNCNAME[i]}" "${BASH_SOURCE[i]}" "${BASH_LINENO[i - 1]}"
8450
done
8551

8652
TRAP_ERROR_INFO_STACK_TRACE="$stack_trace"

0 commit comments

Comments
 (0)