added Jamie McClelland as author in src/*
[monkeysphere.git] / src / common
index 34d9b5adfcace1cf8cf4858788a03d602ade4e4a..16235a2881f7b7c2c9480abe5e93c6d17681f28b 100644 (file)
@@ -4,6 +4,8 @@
 #
 # Written by
 # Jameson Rollins <jrollins@fifthhorseman.net>
+# Jamie McClelland <jm@mayfirst.org>
+# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
 #
 # Copyright 2008, released under the GPL, version 3 or later
 
@@ -26,15 +28,53 @@ failure() {
     exit ${2:-'255'}
 }
 
-# write output to stderr
+# write output to stderr based on specified LOG_LEVEL the first
+# parameter is the priority of the output, and everything else is what
+# is echoed to stderr
 log() {
+    local priority
     local level
+    local output
+    # don't include SILENT in alllevels: it's handled separately
+    # list in decreasing verbosity (all caps)
+    local alllevels="DEBUG INFO ERROR"
+    local found=
 
-    level="$1"
+    # translate lowers to uppers in global log level
+    LOG_LEVEL=$(echo "$LOG_LEVEL" | tr "[:lower:]" "[:upper:]")
+
+    # just go ahead and return if the log level is silent
+    if [ "$LOG_LEVEL" = 'SILENT' ] ; then
+       return
+    fi
+
+    for level in $alllevels; do 
+       if [ "$LOG_LEVEL" = "$level" ] ; then
+           found=true
+       fi
+    done
+    if [ -z "$found" ] ; then
+       # default to INFO:
+       LOG_LEVEL=INFO
+    fi
+
+    # get priority from first parameter, translating all lower to
+    # uppers
+    priority=$(echo "$1" | tr "[:lower:]" "[:upper:]")
     shift
 
-    echo -n "ms: " >&2
-    echo "$@" >&2
+    # scan over available levels
+    for level in $alllevels ; do
+       # output if the log level matches, set output to true
+       # this will output for all subsequent loops as well.
+       if [ "$LOG_LEVEL" = "$level" ] ; then
+           output=true
+       fi
+       if [ "$priority" = "$level" -a "$output" = 'true' ] ; then
+           echo -n "ms: " >&2
+           echo "$@" >&2
+       fi
+    done
 }
 
 # cut out all comments(#) and blank lines from standard input