From 72da6d23c52209800d186ee97734e4cac11b7190 Mon Sep 17 00:00:00 2001 From: Dan Bornstein Date: Wed, 4 Oct 2023 12:28:28 -0700 Subject: [PATCH] Add a `depth` argument. --- scripts/lib/bashy-core/stderr-messages.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/scripts/lib/bashy-core/stderr-messages.sh b/scripts/lib/bashy-core/stderr-messages.sh index 3b18894..41f9cee 100644 --- a/scripts/lib/bashy-core/stderr-messages.sh +++ b/scripts/lib/bashy-core/stderr-messages.sh @@ -17,8 +17,9 @@ # when "manually" turning on or off messages. # --exec -- Execute the arguments as a command, instead of treating them as # arguments to print. The command's output gets redirected to `stderr`. -# --file-line -- Include the caller-of-the-caller's file and line number. That -# is, a user of the command can use this to talk about what called it. +# --file-line= -- Include the caller's file and line number, at the +# indicated stack depth up from the caller, with `0` being the caller. That +# is, a user of the command can use this to refer to what called it. # --set=0|1 -- Enable or disable printing of this kind of message. # --status -- Prints `1` or `0` to stdout, to indicate enabled status. (This is # to make it easy to propagate the enabled state down into another command.) @@ -106,7 +107,7 @@ function _stderr_print-handler { shift 2 local doExec=0 - local doFileLine=0 + local fileLineDepth=-1 local printName=1 local wasCmd=0 @@ -123,8 +124,8 @@ function _stderr_print-handler { --exec) doExec=1 ;; - --file-line) - doFileLine=1 + --file-line=*) + fileLineDepth="${1#*=}" ;; --no-name) if [[ ${anyMessagesVarName} == '' ]]; then @@ -162,12 +163,12 @@ function _stderr_print-handler { eval "${anyMessagesVarName}=1" fi - if (( doFileLine )); then + if (( fileLineDepth >= 0 )); then if (( didPrintName )); then printf 1>&2 '\n' fi - # `3` (and not `2`) because there's an internal caller layer to elide. - [[ "$(caller 3)" =~ ^([0-9]+).*/(.*)$ ]] + # `+ 1` because there's an internal caller layer to elide. + [[ "$(caller "$((fileLineDepth + 1))")" =~ ^([0-9]+).*/(.*)$ ]] printf 1>&2 '%s:%s: ' "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" fi