sh/sh/log.sh
2024-11-19 09:58:15 +01:00

54 lines
1 KiB
Bash

SH_LOG_LEVEL_FATAL=0
SH_LOG_LEVEL_ERROR=1
SH_LOG_LEVEL_WARN=2
SH_LOG_LEVEL_INFO=3
SH_LOG_LEVEL_DEBUG=4
SH_LOG_LEVEL_TRACE=5
SH_LOG_LEVEL=${SH_LOG_LEVEL_INFO}
sh_log() { sh_log_info "${@}"; }
sh_log_debug() {
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_DEBUG}" ]; then
echo "[DEBUG]" "${@}"
fi
}
sh_log_error() {
local code="${1}"
shift
[ -n "${code}" ] || sh_log_fatal 1 "No error code"
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_ERROR}" ]; then
echo "[ERROR]" "${@}" >&2
return "${code}"
fi
}
sh_log_fatal() {
local code="${1}"
shift
[ -n "${code}" ] || sh_log_fatal 1 "No error code"
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_FATAL}" ]; then
echo "[FATAL]" "${@}" >&2
exit "${code}"
fi
}
sh_log_info() {
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_INFO}" ]; then
echo "${@}"
fi
}
sh_log_trace() {
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_TRACE}" ]; then
echo "[TRACE]" "${@}"
fi
}
sh_log_warn() {
if [ "${SH_LOG_LEVEL}" -ge "${SH_LOG_LEVEL_WARN}" ]; then
echo " [WARN]" "${@}"
fi
}