rwx/sh/log/log.sh
2024-12-02 01:18:40 +01:00

68 lines
1.3 KiB
Bash

RWX_LOG_LEVEL_FATAL=0
RWX_LOG_LEVEL_ERROR=1
RWX_LOG_LEVEL_WARN=2
RWX_LOG_LEVEL_INFO=3
RWX_LOG_LEVEL_DEBUG=4
RWX_LOG_LEVEL_TRACE=5
RWX_LOG_LEVEL=${RWX_LOG_LEVEL_INFO}
rwx_log() { rwx_log_info "${@}"; }
rwx_log_debug() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_DEBUG}" ]; then
_rwx_log "[DEBUG]" "${@}"
fi
}
rwx_log_error() {
local code="${1}"
shift
[ -n "${code}" ] || rwx_log_fatal 1 "No error code"
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_ERROR}" ]; then
_rwx_log "[ERROR]" "${@}" >&2
return "${code}"
fi
}
rwx_log_fatal() {
local code="${1}"
shift
[ -n "${code}" ] || rwx_log_fatal 1 "No error code"
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_FATAL}" ]; then
_rwx_log "[FATAL]" "${@}" >&2
exit "${code}"
fi
}
rwx_log_info() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_INFO}" ]; then
_rwx_log "" "${@}"
fi
}
rwx_log_trace() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_TRACE}" ]; then
_rwx_log "[TRACE]" "${@}"
fi
}
rwx_log_warn() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_WARN}" ]; then
_rwx_log "[ WARN]" "${@}"
fi
}
_rwx_log() {
local prefix="${1}"
shift
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
if [ -n "${prefix}" ]; then
__rwx_log "${prefix} ${line}"
else
__rwx_log "${line}"
fi
done
}