From 3b7b94847986cbe13835d878fa4289b7140d7235 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 29 Jul 2026 13:04:28 +0400 Subject: [PATCH] miaou_debug auto-hierarchy --- poc | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/poc b/poc index 1e3fc4e..3cbb239 100755 --- a/poc +++ b/poc @@ -1,4 +1,5 @@ -#!/usr/bin/env bash +#!/usr/bin/env -S -- bash +#!/usr/bin/env -S -- bash -x # constants @@ -47,7 +48,16 @@ function is_true { # HINT: define an alias to bypass ARG evaluation when unnecessary! # example: alias toto="bash -c \"[[ \\\$MIAOU_DEBUG == 'true' ]] && echo yes \\\$* || echo nope\"" function miaou_debug { - is_true "${MIAOU_DEBUG}" && >&4 builtin echo -e "\e[90m <== DEBUG ==> \e[33m$*\e[0m" || true + if is_true "${MIAOU_DEBUG}"; then + [[ ! -v miaou_debug_hierarchy ]] && miaou_debug_hierarchy=0 + local i message="$*" padding='' + regex='^&4 builtin echo -e "\e[90m <== DEBUG ==> ${padding}\e[33m${message}\e[0m" + regex='^<[^/].*[^/]>$' # markup starting + [[ "${message}" =~ $regex ]] && ((++miaou_debug_hierarchy)) || true + fi } function on_return { @@ -59,6 +69,7 @@ function on_return { print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" "${failure_type}" "${failure_payload}" miaou_debug "" fi + true } # overridden function which permits backtracing of the original `exit` statement @@ -77,7 +88,7 @@ function exit { failure_type="EXIT" failure_payload="${error_funcname[0]}" - miaou_debug "" + miaou_debug "" print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" "${failure_type}" "${failure_payload}" builtin exit "$failure_code" } @@ -105,7 +116,7 @@ function on_error { failure_code="${1:-200}" >&4 printf '\r' # TODO: force carriage return explicitly, is it really useful? - miaou_debug "" + miaou_debug "" if [[ $$ == "$BASHPID" ]]; then if [[ ! -v failure_type ]]; then @@ -129,14 +140,14 @@ function on_error { error_funcname=("${error_funcname[@]}") if [[ -v failure_payload ]]; then - miaou_debug "" + miaou_debug "" else - miaou_debug "" + miaou_debug "" fi builtin exit "$failure_code" else - miaou_debug "" + miaou_debug "" print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" 'SUBSHELL_ERROR' "${BASH_COMMAND}" kill -PIPE $$ fi @@ -169,7 +180,7 @@ function on_pipe { failure_code=141 failure_type="PIPE_ERROR" fi - miaou_debug "" + miaou_debug "" builtin exit "$failure_code" } @@ -207,7 +218,7 @@ function dump_failure { } function dump_stacktrace { - miaou_debug "" + miaou_debug "" local i source lineno funcname for i in "${!error_source[@]}"; do tty_ansi FG_MAGENTA @@ -229,7 +240,7 @@ function on_exit { trap - RETURN local code="${1:-0}" last_error regex - miaou_debug "" + miaou_debug "" [[ ! -v tmp_error ]] && mapfile -t tmp_error <"$MIAOU_BASH_ERROR" @@ -237,7 +248,7 @@ function on_exit { last_error="${tmp_error[-1]}" regex='^(.*): line ([0-9]+): miaou_error: RETURN [0-9]+' if [[ "$last_error" =~ $regex ]]; then - miaou_debug "" + miaou_debug "" local source line source="${BASH_REMATCH[1]}" line="${BASH_REMATCH[2]}" @@ -251,7 +262,7 @@ function on_exit { else if [[ $code -gt 0 && ${#tmp_error[@]} -gt 0 ]]; then last_error="${tmp_error[-1]}" - miaou_debug "" + miaou_debug "" if [[ $code -eq 1 ]]; then regex='^(.*): line ([0-9]+): (.*): unbound variable$' if [[ "$last_error" =~ $regex ]]; then @@ -259,7 +270,7 @@ function on_exit { source="${BASH_REMATCH[1]}" line="${BASH_REMATCH[2]}" payload="${BASH_REMATCH[3]}" - miaou_debug "" + miaou_debug "" failure_code=$code failure_type='UNBOUND_VARIABLE' failure_payload="${BASH_REMATCH[3]}" @@ -284,9 +295,9 @@ function on_exit { 2>&3 >&4 dump_stacktrace fi if [[ $code -gt 0 ]]; then - miaou_debug "" + miaou_debug "" else - miaou_debug "" + miaou_debug "" fi }