Skip to content

Commit

Permalink
Add a depth argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Oct 4, 2023
1 parent bfe71b3 commit 72da6d2
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions scripts/lib/bashy-core/stderr-messages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=<depth> -- 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.)
Expand Down Expand Up @@ -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

Expand All @@ -123,8 +124,8 @@ function _stderr_print-handler {
--exec)
doExec=1
;;
--file-line)
doFileLine=1
--file-line=*)
fileLineDepth="${1#*=}"
;;
--no-name)
if [[ ${anyMessagesVarName} == '' ]]; then
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 72da6d2

Please sign in to comment.