]> git.plutz.net Git - cgilite/blobdiff - logging.sh
functions for logging and session handling
[cgilite] / logging.sh
diff --git a/logging.sh b/logging.sh
new file mode 100755 (executable)
index 0000000..f10ea4c
--- /dev/null
@@ -0,0 +1,29 @@
+#!/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(){
+  if [ "${1:-3}" -le "$LOGLEVEL" ]; then
+    [ "$#" -gt 1 ] \
+    && printf %s\\n "$*" >>"${LOGFILE}" \
+    || tee -a "${LOGFILE}"
+  fi
+}
+
+
+die(){
+  [ "$#" -gt 0 ] && logmsg 1 "$@"
+  exit 1
+}
+panic(){ logmsg 2 "$@"; }
+error(){ logmsg 3 "$@"; }
+debug(){ logmsg 4 "$@"; }