From bb2427c28bf40179c4881b22c23f23f9bea78f55 Mon Sep 17 00:00:00 2001
From: Jameson Graef Rollins <jrollins@phys.columbia.edu>
Date: Tue, 2 Sep 2008 00:38:27 -0700
Subject: [PATCH] Finalize new log level changes.  This is more or less doing
 what it should.  The only problem, I think, is that it doesn't handle
 improperly specified LOG_LEVEL well, effectively resorting to silent.

---
 debian/changelog                         |  4 +--
 etc/monkeysphere-server.conf             |  4 +++
 etc/monkeysphere.conf                    |  4 +++
 man/man1/monkeysphere-ssh-proxycommand.1 |  5 ++++
 man/man1/monkeysphere.1                  |  4 +++
 man/man8/monkeysphere-server.8           |  4 +++
 src/common                               | 33 +++++++++++++++++++++---
 src/monkeysphere-server                  |  2 +-
 8 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 76e4d04..1cc1dd8 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,10 +1,10 @@
 monkeysphere (0.12-1) UNRELEASED; urgency=low
 
-  * Improved output handling.
+  * Improved output handling.  New LOG_LEVEL variable.
   * debian/control: switched Homepage: and Vcs-Git: to canonicalized
     upstream hostnames.
 
- -- Jameson Graef Rollins <jrollins@phys.columbia.edu>  Sun, 24 Aug 2008 23:49:23 -0700
+ -- Jameson Graef Rollins <jrollins@phys.columbia.edu>  Mon, 01 Sep 2008 23:55:56 -0700
 
 monkeysphere (0.11-1) experimental; urgency=low
 
diff --git a/etc/monkeysphere-server.conf b/etc/monkeysphere-server.conf
index 15f43b1..1b0ed9f 100644
--- a/etc/monkeysphere-server.conf
+++ b/etc/monkeysphere-server.conf
@@ -6,6 +6,10 @@
 # prefeced by "MONKEYSPHERE_" will take precedence over the values
 # specified here.
 
+# Log level.  Can be SILENT, ERROR, INFO, DEBUG, in increasing order
+# of verbosity.
+#LOG_LEVEL=INFO
+
 # GPG keyserver to search for keys.
 #KEYSERVER=subkeys.pgp.net
 
diff --git a/etc/monkeysphere.conf b/etc/monkeysphere.conf
index 2648fa9..d9bbad3 100644
--- a/etc/monkeysphere.conf
+++ b/etc/monkeysphere.conf
@@ -6,6 +6,10 @@
 # prefeced by "MONKEYSPHERE_" will take precedence over the values
 # specified here.
 
+# Log level.  Can be SILENT, ERROR, INFO, DEBUG, in increasing order
+# of verbosity.
+#LOG_LEVEL=INFO
+
 # GPG home directory.  If not specified either here or in the
 # MONKEYSPHERE_GNUPGHOME environment variable, then the value of the
 # GNUPGHOME environment variable will be used.  If GNUPGHOME is not
diff --git a/man/man1/monkeysphere-ssh-proxycommand.1 b/man/man1/monkeysphere-ssh-proxycommand.1
index 9aad232..c3c7993 100644
--- a/man/man1/monkeysphere-ssh-proxycommand.1
+++ b/man/man1/monkeysphere-ssh-proxycommand.1
@@ -54,6 +54,11 @@ will be properly checked.
 All environment variables defined in monkeysphere(1) can also be used
 for the proxy command, with one note:
 
+.TP
+MONKEYSPHERE_LOG_LEVEL
+Set the log level.  Can be SILENT, ERROR, INFO, DEBUG, in increasing
+order of verbosity.
+
 .TP
 MONKEYSPHERE_CHECK_KEYSERVER
 Setting this variable (to `true' or `false') will override the policy
diff --git a/man/man1/monkeysphere.1 b/man/man1/monkeysphere.1
index 92ba2fa..26327f4 100644
--- a/man/man1/monkeysphere.1
+++ b/man/man1/monkeysphere.1
@@ -83,6 +83,10 @@ Output a brief usage summary.  `h' or `?' may be used in place of
 The following environment variables will override those specified in
 the monkeysphere.conf configuration file (defaults in parentheses):
 .TP
+MONKEYSPHERE_LOG_LEVEL
+Set the log level.  Can be SILENT, ERROR, INFO, DEBUG, in increasing
+order of verbosity.
+.TP
 MONKEYSPHERE_GNUPGHOME, GNUPGHOME
 GnuPG home directory (~/.gnupg).
 .TP
diff --git a/man/man8/monkeysphere-server.8 b/man/man8/monkeysphere-server.8
index 5985f24..b63f659 100644
--- a/man/man8/monkeysphere-server.8
+++ b/man/man8/monkeysphere-server.8
@@ -182,6 +182,10 @@ The following environment variables will override those specified in
 the monkeysphere-server.conf configuration file (defaults in
 parentheses):
 .TP
+MONKEYSPHERE_LOG_LEVEL
+Set the log level.  Can be SILENT, ERROR, INFO, DEBUG, in increasing
+order of verbosity.
+.TP
 MONKEYSPHERE_KEYSERVER
 OpenPGP keyserver to use (subkeys.pgp.net).
 .TP
diff --git a/src/common b/src/common
index 34d9b5a..f93793e 100644
--- a/src/common
+++ b/src/common
@@ -26,15 +26,40 @@ 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
 
-    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
+
+    # 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
+    # list in decreasing verbosity (all caps)
+    for level in DEBUG INFO ERROR ; do
+	# output if the log level matches, set output to true
+	# this will output for all subsequenty 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
diff --git a/src/monkeysphere-server b/src/monkeysphere-server
index c81c066..e3ffc4b 100755
--- a/src/monkeysphere-server
+++ b/src/monkeysphere-server
@@ -855,7 +855,7 @@ unset MONKEYSPHERE_USER
 
 # set empty config variable with ones from the environment, or with
 # defaults
-LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="info"}}
+LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
-- 
2.34.1