Browse Source

stacktrace as array OK

main
pvincent 1 week ago
parent
commit
decd3d7685
  1. 40
      bin/bash-back
  2. 10
      lib/utility.bash
  3. 4
      useless.sh

40
bin/bash-back

@ -8,9 +8,9 @@ SCRIPT=$1
# FUNCTIONS
function on_return {
if [[ "${BASH_COMMAND}" == return* ]]; then
if [[ "${BASH_COMMAND}" == 'return '* ]]; then
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) # regex does not work here unfortunately! thus use of basic cut
if [[ -n "$code" ]] && [[ $code -ne 0 ]]; then
if [[ -n "$code" ]] && [[ "$code" -ne 0 ]]; then
trap - DEBUG
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}"
fi
@ -27,7 +27,6 @@ function exit {
function on_exit {
code=${1:-0}
>&4 dump_stderr
stack_lines=("${BASH_LINENO[@]}")
stack_functions=("${FUNCNAME[@]}")
@ -52,8 +51,9 @@ function on_exit {
stack_functions=("${stack_functions[@]}")
stack_sources=("${stack_sources[@]}")
stack_summary=()
if [[ $code -gt 0 ]]; then
$DEBUG && >/dev/tty echo '---stacktrace---'
last_error=$(tail -n1 $TMP_ERROR)
last_source=${stack_sources[0]}
last_source=${last_source//./"\."} # replace . by \.
@ -73,25 +73,45 @@ function on_exit {
stack_lines=("$line" "${stack_lines[@]}")
stack_functions=("$funcname" "${stack_functions[@]}")
fi
>&4 echo -e "ERROR $code: [$message]"
stack_summary+=("ERROR $code: [$message]")
else
>&4 echo "ERROR $code: <undefined>"
stack_summary+=("ERROR $code: <undefined>")
fi
for i in "${!stack_functions[@]}"; do
>&4 printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}"
stack_detail=$(printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}")
stack_summary+=("$stack_detail")
done
>&4 dump_stderr true
dump_summary
else
>&4 dump_stderr false
fi
cleanup
builtin exit $code
}
function dump_summary {
$DEBUG && >/dev/tty echo '---stacktrace---'
for i in "${stack_summary[@]}"; do
>&4 echo -e "$i"
done
}
function dump_stderr {
if [[ -s $TMP_ERROR ]]; then
last_trim=${1:-false}
mapfile -t tmp_error <$TMP_ERROR
if $last_trim; then
echo "${#tmp_error[@]}"
unset tmp_error[-1]
tmp_error=("${tmp_error[@]}")
fi
if [[ "${#tmp_error[@]}" -gt 0 ]]; then
$DEBUG && >/dev/tty echo '---stderr---'
cat $TMP_ERROR
for i in "${tmp_error[@]}"; do
echo -e "$i"
done
fi
}

10
lib/utility.bash

@ -1,13 +1,17 @@
#!/usr/bin/env bash
function blabla {
echo IN blabla OUT
suite
}
function suite {
return
return 7
command_not_found
echo $nope
exit 10 # youpi
exit
command_not_found
return 7
return
}
blabla

4
useless.sh

@ -3,6 +3,7 @@
function F2 {
echo "stdout A"
# echo toto >>/tmp/toto
return
>&2 echo "This is stderr"
blabla
@ -30,7 +31,10 @@ function F3 {
echo start of useless
F3
# lib/utility.bash
. lib/utility.bash
>&2 echo some trace in stderr
main
error
echo end of useless
Loading…
Cancel
Save