-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.bash
52 lines (46 loc) · 1.34 KB
/
stack.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash
stack::print() {
local err=$?
set +o xtrace
local code="${1:$err}"
local up=${2:-0}
# Ignore errors detected in bashdb
if [[ ${FUNCNAME[$up+1]} == _Dbg_* ]];then
return
fi
echo -e "\n\e[91mRuntime failure at ${BASH_SOURCE[$up+1]}:${BASH_LINENO[$up]} exit code $code\n"
stack::point_line "${BASH_SOURCE[$up+1]}" "${BASH_LINENO[$up]}"
if [ ${#FUNCNAME[@]} -gt $((up+2)) ];then
echo -e "\n\e[91mStack trace:"
for ((a=0;a<${#FUNCNAME[@]}-up-1;a++));do
local line
line=$(printf " %-30s %s" "${BASH_SOURCE[$a+$up+1]}:${BASH_LINENO[$up+$a]}" "${FUNCNAME[$up+$a]}")
echo -e "\e[91m$line"
done
fi
echo -e "\n\e[91mExiting with error status $code"
wait
exit "$code"
}
stack::point_line() {
local file=$1
local line=$2
local start=$((line-2))
local end=$((line+3))
if [ $start -lt 1 ];then
start=1
fi
for ((a=start;a<end;a++));do
lineno=$(printf "%3s" "$a")
if [ "$a" = "$line" ];then
echo -e "\e[93m${lineno}\e[37m \e[91m>\e[37m| \e[91m$(sed "${a}q;d" "$file")"
else
echo -e "\e[93m${lineno}\e[37m | \e[33m$(sed "${a}q;d" "$file")"
fi
done
}
trap stack::print ERR
set -o errtrace
set -E
import traps
traps::add_err_trap "stack::print 1 1"