Merge commit 'c0dcd45c3ecac33376e06b7ca470ae56f2ed5e19' into cgilite
[confetti] / cgilite / logging.sh
1 #!/bin/sh
2
3 # LOGLEVEL 1: Crash condition
4 # LOGLEVEL 2: Unexpected condition
5 # LOGLEVEL 3: Failed action (i.e. due to config error)
6 # LOGLEVEL 4: Debug
7
8 [ -n "$include_logging" ] && return 0
9 include_logging="$0"
10
11 LOGLEVEL="${LOGLEVEL:-3}"
12 LOGFILE="${LOGFILE:-/dev/stderr}"
13
14 logmsg(){
15   local ll="${1:-3}"
16   shift 1
17   if [ "$ll" -le "$LOGLEVEL" -a "$#" -gt 0 ]; then
18     printf %s\\n "$*" >>"${LOGFILE}"
19   elif [ "$ll" -le "$LOGLEVEL" ]; then
20     tee -a "${LOGFILE}"
21   elif [ ! "$#" -gt 0 ]; then
22     cat
23   fi
24 }
25
26 die(){
27   [ "$#" -gt 0 ] && logmsg 1 "$@"
28   exit 1
29 }
30 panic(){ logmsg 2 "$@"; }
31 error(){ logmsg 3 "$@"; }
32 debug(){ logmsg 4 "$@"; }