[prev in list] [next in list] [prev in thread] [next in thread] 

List:       voyage-linux
Subject:    Re: [Voyage-linux] [PATCH] debug: add debug.rc and demo script mydbg.sh
From:       Kim-man 'Punky' TSE <punkytse () punknix ! com>
Date:       2012-09-21 5:45:23
Message-ID: 505BFEF3.7060405 () punknix ! com
[Download RAW message or body]

Applied to voyage-util.  Thanks.

http://www2.voyage.hk/dists/experimental/voyage-util/voyage-util_0.12-1_all.deb

On 8/21/2012 10:09 PM, Jim Cromie wrote:
> 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.cromie@gmail.com>
> ---
>   usr/local/sbin/debug.rc | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
>   usr/local/sbin/mydbg.sh | 15 +++++++++++++++
>   2 files changed, 66 insertions(+)
>   create mode 100644 usr/local/sbin/debug.rc
>   create mode 100755 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..455a059
> --- /dev/null
> +++ b/usr/local/sbin/debug.rc
> @@ -0,0 +1,51 @@
> +# -*- 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-bar.* into foo_bar
> +script=`basename $0 | sed -e 's/\.\w*//' | sed -e 's/-/_/g' `
> +
> +# auto-export DBG_* vars passed on cmdline
> +export ${!DBG_*}
> +if [ -n $VDBG ] ; then
> +    for n in ${!DBG_*}; do
> +	echo "exported $n=${!n}"
> +    done
> +fi
> +
> +# 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 100755
> index 0000000..8a1ce60
> --- /dev/null
> +++ b/usr/local/sbin/mydbg.sh
> @@ -0,0 +1,15 @@
> +#!/bin/bash
> +
> +# test/demo script for debug.rc
> +
> +source debug.rc  # will need $EXECDIR/ in install-scripts
> +
> +dbg_echo "ok cool" shell-level-$SHLVL $*
> +dbg_echo_v 2 "ok very cool" shell-level=$SHLVL $*
> +
> +# DBG_* vars are exported by debug.rc
> +# so subshells get them automatically
> +
> +if (( $SHLVL < 4 )) ; then
> +    sh mydbg.sh woohoo $*
> +fi


-- 
Regards,
Kim-man "Punky" Tse

* Open Source Embedded Solutions and Systems
   - Voyage Linux (http://linux.voyage.hk)
   - Voyage ONE   (http://linux.voyage.hk/voyage-one)
   - Voyage MPD   (http://linux.voyage.hk/voyage-mpd)
* Voyage Store   (http://store.voyage.hk)


_______________________________________________
Voyage-linux mailing list
Voyage-linux@list.voyage.hk
http://list.voyage.hk/mailman/listinfo/voyage-linux
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic