dotfiles/lib/log.lib.sh
2023-09-01 01:40:41 +02:00

60 lines
No EOL
759 B
Bash

#!/bin/sh
__log_lvl=1
__log_get_lvl() {
case ${1:-$__log_lvl} in
0)
echo debug
;;
1)
echo info
;;
2)
echo warn
;;
3)
echo err
;;
4)
echo fatal
;;
esac
}
__log() {
[ $1 -ge $__log_lvl ] || return
level=$(__log_get_lvl $1)
shift
printf "[%s] %s\n" "$level" "$@"
}
debug() {
__log 0 "$@"
}
info() {
__log 1 "$@"
}
warn() {
__log 2 "$@"
}
err() {
__log 3 "$@"
}
fatal() {
while getopts "e:" opt; do
case $opt in
e)
exitnum=$OPTARG
;;
esac
done
shift $((OPTIND-1))
__log 4 "$@"
exit ${exitnum:-1}
}