Skip to content

Commit

Permalink
stacktraces: introduce get_stacktrace_element_by_index() to get a s…
Browse files Browse the repository at this point in the history
…pecific stracktrace element
  • Loading branch information
rpardini authored and igorpecovnik committed Nov 25, 2023
1 parent 7e2e557 commit 2a9ec6d
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/functions/logging/stacktraces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
# @TODO this in practice is only used... ?
function get_extension_hook_stracktrace() {
[[ "${CONFIG_DEFS_ONLY}" == "yes" ]] && return 0 # don't waste time here
local sources_str="$1" # Give this ${BASH_SOURCE[*]} - expanded
local lines_str="$2" # And this # Give this ${BASH_LINENO[*]} - expanded
if [[ "${separator:-"none"}" == "none" ]]; then # can take a separator from caller
local separator="-> "
fi
local sources_str="$1" # Give this ${BASH_SOURCE[*]} - expanded
local lines_str="$2" # And this # Give this ${BASH_LINENO[*]} - expanded
local sources lines index final_stack=""
IFS=' ' read -r -a sources <<< "${sources_str}"
IFS=' ' read -r -a lines <<< "${lines_str}"
Expand All @@ -31,7 +34,7 @@ function get_extension_hook_stracktrace() {
source="${source#"lib/"}"
# add to the list
# shellcheck disable=SC2015 # i know. thanks. I won't write an if here
arrow="$([[ "$final_stack" != "" ]] && echo "-> " || true)"
arrow="$([[ "$final_stack" != "" ]] && echo "${separator}" || true)"
final_stack="${source}:${line} ${arrow} ${final_stack} "
done
# output the result, no newline
Expand Down Expand Up @@ -60,3 +63,15 @@ function show_caller_full() {
done
} || true # always success
}

# get a stacktrace element by index; fills in outer scope variable "stacktrace_element"
function get_stacktrace_element_by_index() {
declare -i index="${1}"
declare stacktrace
stacktrace="$(separator="|" get_extension_hook_stracktrace "${BASH_SOURCE[*]}" "${BASH_LINENO[*]}")"
declare -a stack_array=()
IFS='|' read -r -a stack_array <<< "${stacktrace}" # split the stacktrace into an array
declare caller="${stack_array[${index}]}" # get the second-to-last element of the array, which is the caller of this (hopefully)
display_alert "get_stacktrace_element_by_index:" "${index} :: '${caller}'" "debug"
stacktrace_element="${caller#"${caller%%[![:space:]]*}"}" # trim spaces, publish to outer scope "stacktrace_element"
}

0 comments on commit 2a9ec6d

Please sign in to comment.