debug.rc defines dbg_echo() and dbg_echo_v() as either silent functions
or as echos, depending upon envars.

If DBG_ALL or DBG_<script-basename-w/o-suffix> are defined as non-null,
the functions are active, otherwise they do nothing.

dbg_echo_v() is active if DBG_<script> is >= 1st arg passed in,
and silent otherwise.

See mydbg.sh for simple usage.

There are other ways to do this selective echoing, for example:
- DBG_="name1:name2:name3", where names must match script-names
  (modulo trimming as done in DBG_<script...>)
- substring matching on $DBG_ so that some symbolic-ish names can be used
- others
- combos of above

So this is just a prototype, and fodder for discussion.

Signed-off-by: Jim Cromie <jim.cro...@gmail.com>
---
 usr/local/sbin/debug.rc | 44 ++++++++++++++++++++++++++++++++++++++++++++
 usr/local/sbin/mydbg.sh |  7 +++++++
 2 files changed, 51 insertions(+)
 create mode 100644 usr/local/sbin/debug.rc
 create mode 100644 usr/local/sbin/mydbg.sh

diff --git a/usr/local/sbin/debug.rc b/usr/local/sbin/debug.rc
new file mode 100644
index 0000000..a1c4f2a
--- /dev/null
+++ b/usr/local/sbin/debug.rc
@@ -0,0 +1,44 @@
+# -*- sh -*-
+
+# Defines dbg_echo (and others?) as either noops or useful functions
+# if DBG_ALL or DBG_$script are defined as non-empty strings.  
+
+# shell resource for debugging, must be sourced (dotted) into user
+# scripts.
+
+# convert $path-to/foo.* into foo
+script=`basename $0 | sed -e 's/\.\w*//' `
+
+# and get envar name which would enable $script debugging
+DBG_ME=DBG_$script
+
+
+# define shell functions accordingly
+if [ -z "$DBG_ALL" -a -z "${!DBG_ME}" ] ; then
+
+    # noop shell functions
+
+    function dbg_echo() {
+       : NO-OP
+    }
+    function dbg_echo_v() {
+       : NO-OP
+    }
+    # insert other noop funcs here
+
+    return;    # only allowed in func or sourced scripts
+fi
+
+# define useful shell functions
+
+dbg_echo() {
+    echo "$script: $*"
+}
+
+dbg_echo_v() {
+    let lvl=$1
+    shift 1
+    if (( $DBG_ME >= $lvl )) ; then
+    echo "$script: $*"
+    fi
+}
diff --git a/usr/local/sbin/mydbg.sh b/usr/local/sbin/mydbg.sh
new file mode 100644
index 0000000..8c47a3d
--- /dev/null
+++ b/usr/local/sbin/mydbg.sh
@@ -0,0 +1,7 @@
+
+
+. debug.rc
+
+dbg_echo "ok cool"
+
+dbg_echo_v 2 "ok very cool"
-- 
1.7.11.4


_______________________________________________
Voyage-linux mailing list
Voyage-linux@list.voyage.hk
http://list.voyage.hk/mailman/listinfo/voyage-linux

Reply via email to