Do stack trace + more
authorIan Kelling <ian@iankelling.org>
Mon, 16 Feb 2015 00:43:27 +0000 (16:43 -0800)
committerIan Kelling <ian@iankelling.org>
Fri, 12 Aug 2016 22:35:09 +0000 (15:35 -0700)
README
bash-trace-function [new file with mode: 0644]
errallow-function
errcatch-function
errexit-function [new file with mode: 0644]

diff --git a/README b/README
index a960550c97db9dacb19c2a9ad9d82387010871fd..b3829e2fd029748742db8d8360579ce2e911aaed 100644 (file)
--- a/README
+++ b/README
@@ -4,12 +4,9 @@ script files which sit next to this file.
 Files ending in -function are for sourcing then calling as a function. Files
 without -function are exactly the same except they are for calling as a script.
 
-Patches and bugs are very welcome via gitlab.
-
-Questions, feedback, etc are very welcome via email to Ian Kelling
-<ian@iankelling.org>. I will add any useful questions, answers, etc. to this
-file. If a mailing list / forum is ever called for, I will start one.
+Patches, bugs, and any feedback is very welcome via gitorious or email to
+Ian Kelling <ian@iankelling.org>.
 
 This program is also part of a collection of programs,
-https://gitlab.com/iankelling/bash-programs-by-ian, which are unrelated except
+https://gitorious.org/bash-programs-by-ian, which are unrelated except
 having the same author and being being bash programs.
diff --git a/bash-trace-function b/bash-trace-function
new file mode 100644 (file)
index 0000000..5c304ae
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/bash
+# Copyright (C) 2015 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+shopt -s extdebug
+bash-trace() {
+    # shows function args when: shopt -s extdebug
+    local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent
+    local source
+    local extdebug=false
+    if [[ $(shopt -p extdebug) == *-s* ]]; then
+        extdebug=true
+    fi
+
+    for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
+        argc=${BASH_ARGC[frame]}
+        argc_index+=$argc
+        ((frame < start)) && continue
+        if (( ${#BASH_SOURCE[@]} > 1 )); then
+            source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
+        fi
+        indent=$((frame-start+1))
+        indent=$((indent < max_indent ? indent : max_indent))
+        printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
+        if $extdebug; then
+            for ((i=argc_index-1; i >= argc_index-argc; i--)); do
+                printf " %s" "${BASH_ARGV[i]}"
+            done
+        fi
+        echo \'
+    done
+}
index d44247bd96a18855f658bf3b5719098920898083..aff8593594cfc1ff17ec6b5c0e871309b4b03ebf 100644 (file)
@@ -17,6 +17,6 @@ errallow() {
     if [[ $1 ]]; then
         echo "errallow help: Undo the complimentary errcatch function."
     else
-        set +eE +o pipefail; trap ERR
+        set +E +o pipefail; trap ERR
     fi
 }
index a46dfbccf13b1f1e0beb571b88bf327345071702..b6489ac74d9a66c02c48ff7fd73633fd72c6fc7e 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# on non-zero return codes outside of if/else,etc, print failure, stack trace, and exit
+# depends on bash-trace function
 errcatch() {
-    if [[ $1 ]]; then
-        echo "errcatch help: Enable general purpose bash error handling.
-Exit on all errors and print useful information."
-    else
-        set -eE;
-        trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
-${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\"  }\$?=$?"' ERR
-        set -o pipefail
-    fi
+    set -E; shopt -s extdebug
+    _err-trap() {
+        err=$?
+        exec >&2
+        echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
+        bash-trace 2
+        echo "$0: exiting with code $err"
+        exit $err
+    }
+    trap _err-trap ERR
+    set -o pipefail
 }
diff --git a/errexit-function b/errexit-function
new file mode 100644 (file)
index 0000000..cbe2118
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/bash
+# Copyright (C) 2015 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+errexit() {
+    # usage: errexit [EXIT_CODE] [MESSAGE]
+    exec >&2
+    code=1
+    if [[ $@ ]]; then
+        if [[ ${1/[^0-9]/} == "$1" ]]; then
+            code=$1
+            if [[ $2 ]]; then
+                echo "$2"
+            fi
+        else
+            echo "$0: $1"
+        fi
+    fi
+    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
+    bash-trace 2
+    echo "$0: exiting with code $code"
+    exit $err
+}