1 #!/bin/bash
 2 
 3 # Source this file to get DebugEcho and more
 4 
 5 # $Revision: 1.3 $  $Author: schwehr $  $Date: 2004/11/04 18:27:17 $
 6 
 7 ##############################################################################
 8 #     Copyright (C) 2004  Kurt Schwehr
 9 #
10 #     This program is free software; you can redistribute it and/or modify
11 #     it under the terms of the GNU General Public License as published by
12 #     the Free Software Foundation; either version 2 of the License, or
13 #     (at your option) any later version.
14 #
15 #     This program is distributed in the hope that it will be useful,
16 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #     GNU General Public License for more details.
19 #
20 #     You should have received a copy of the GNU General Public License
21 #     along with this program; if not, write to the Free Software
22 #     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 ###############################################################################
24 
25 
26 ######################################################################
27 # Debugging
28 ######################################################################
29 
30 declare -ri EXIT_FAILURE=1
31 declare -ri EXIT_SUCCESS=0
32 
33 
34 declare -ri TERSE=1
35 declare -ri TRACE=4
36 declare -ri VERBOSE=8
37 declare -ri BOMBASTIC=16
38 
39 if [ -z "$VERBOSITY" ]; then
40     declare -i debugLevel=4
41 else
42     declare -i debugLevel=$VERBOSITY
43 fi
44 
45 # Twisted way to get down to the fundamental script name.
46 tmp=${0##/*/}
47 tmp=${tmp%%.bash}
48 tmp=${tmp##*.}
49 tmp=${tmp##*/}
50 declare -r SCRIPT_NAME=$tmp
51 
52 # $1 is the level to compare against debugLevel
53 # $2 is line number
54 # $3 is the string to echo to stdout.
55 DebugEcho()
56 {
57     declare -ir val=$1
58     if [ "$debugLevel" -ge "$val" ]; then
59 	#echo $2
60 	echo "${SCRIPT_NAME}.bash:$2: (`printf "%02d" $1`) $3"
61     fi
62 }
63 
64 #DebugEcho $TERSE     "Terse is on"
65 #DebugEcho $TRACE     "Trace is on"
66 DebugEcho $VERBOSE    $LINENO  "Verbose is on"
67 DebugEcho $BOMBASTIC  $LINENO  "Bombastic is on"
68 
69 DebugEcho $TERSE $LINENO "debugLevel           = $debugLevel"
70 
71 
72 
73 ######################################################################
74 # Like perl's die command
75 ######################################################################
76 die()
77 {
78     declare -ir line=$1
79     echo "ERROR: Command failed at line $line"
80     exit $EXIT_FAILURE
81 }


syntax highlighted by Code2HTML, v. 0.9.1