From decd3d768599fabfa97e1516029b8a5d49e83ac7 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 11 Jul 2026 13:36:46 +0400 Subject: [PATCH] stacktrace as array OK --- bin/bash-back | 40 ++++++++++++++++++++++++++++++---------- lib/utility.bash | 10 +++++++--- useless.sh | 4 ++++ 3 files changed, 41 insertions(+), 13 deletions(-) mode change 100644 => 100755 lib/utility.bash diff --git a/bin/bash-back b/bin/bash-back index c7bdcf8..b0186aa 100755 --- a/bin/bash-back +++ b/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: " + stack_summary+=("ERROR $code: ") 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 } diff --git a/lib/utility.bash b/lib/utility.bash old mode 100644 new mode 100755 index 092c863..7a92cf6 --- a/lib/utility.bash +++ b/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 diff --git a/useless.sh b/useless.sh index 9d310c2..adde5b1 100755 --- a/useless.sh +++ b/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