X-Git-Url: https://git.plutz.net/?p=confetti;a=blobdiff_plain;f=cgilite%2Flogging.sh;h=31bb24d3c4d3b9abbcb7bb175bcd455d5a078d2c;hp=2638ffcf8dc901606f3cdb249fab75e14ca47875;hb=3ea5e66a2f2cac72cfde653f56330370e0b170de;hpb=d5bfa99e6a890b69210658f399a5a7d488bf8750 diff --git a/cgilite/logging.sh b/cgilite/logging.sh new file mode 100755 index 0000000..31bb24d --- /dev/null +++ b/cgilite/logging.sh @@ -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 "$@"; }