]> git.plutz.net Git - confetti/blobdiff - cgilite/logging.sh
Merge commit '36d5feed172cb12ccd469abddebf04eaf9a7c9ae' as 'cgilite'
[confetti] / cgilite / logging.sh
diff --git a/cgilite/logging.sh b/cgilite/logging.sh
new file mode 100755 (executable)
index 0000000..31bb24d
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+# LOGLEVEL 1: Crash condition
+# LOGLEVEL 2: Unexpected condition
+# LOGLEVEL 3: Failed action (i.e. due to config error)
+# LOGLEVEL 4: Debug
+
+[ -n "$include_logging" ] && return 0
+include_logging="$0"
+
+LOGLEVEL="${LOGLEVEL:-3}"
+LOGFILE="${LOGFILE:-/dev/stderr}"
+
+logmsg(){
+  local ll="${1:-3}"
+  shift 1
+  if [ "$ll" -le "$LOGLEVEL" -a "$#" -gt 0 ]; then
+    printf %s\\n "$*" >>"${LOGFILE}"
+  elif [ "$ll" -le "$LOGLEVEL" ]; then
+    tee -a "${LOGFILE}"
+  elif [ ! "$#" -gt 0 ]; then
+    cat
+  fi
+}
+
+die(){
+  [ "$#" -gt 0 ] && logmsg 1 "$@"
+  exit 1
+}
+panic(){ logmsg 2 "$@"; }
+error(){ logmsg 3 "$@"; }
+debug(){ logmsg 4 "$@"; }