rwx/sh/log.sh

69 lines
1.3 KiB
Bash
Raw Normal View History

2024-11-29 18:08:06 +00:00
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
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
RWX_LOG_LEVEL=${RWX_LOG_LEVEL_INFO}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log() { rwx_log_info "${@}"; }
2024-11-18 11:17:47 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_debug() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_DEBUG}" ]; then
_rwx_log "[DEBUG]" "${@}"
2024-11-12 06:20:25 +00:00
fi
}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_error() {
2024-11-19 08:58:15 +00:00
local code="${1}"
shift
2024-11-29 18:08:06 +00:00
[ -n "${code}" ] || rwx_log_fatal 1 "No error code"
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_ERROR}" ]; then
_rwx_log "[ERROR]" "${@}" >&2
2024-11-19 08:58:15 +00:00
return "${code}"
2024-11-12 06:20:25 +00:00
fi
}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_fatal() {
2024-11-19 08:58:15 +00:00
local code="${1}"
shift
2024-11-29 18:08:06 +00:00
[ -n "${code}" ] || rwx_log_fatal 1 "No error code"
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_FATAL}" ]; then
_rwx_log "[FATAL]" "${@}" >&2
2024-11-19 08:58:15 +00:00
exit "${code}"
2024-11-12 06:20:25 +00:00
fi
}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_info() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_INFO}" ]; then
_rwx_log "" "${@}"
2024-11-12 06:20:25 +00:00
fi
}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_trace() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_TRACE}" ]; then
_rwx_log "[TRACE]" "${@}"
2024-11-12 06:20:25 +00:00
fi
}
2023-05-14 13:51:38 +00:00
2024-11-29 18:08:06 +00:00
rwx_log_warn() {
if [ "${RWX_LOG_LEVEL}" -ge "${RWX_LOG_LEVEL_WARN}" ]; then
_rwx_log "[ WARN]" "${@}"
2024-11-12 06:20:25 +00:00
fi
}
2024-11-19 11:21:29 +00:00
2024-11-29 18:08:06 +00:00
_rwx_log() {
2024-11-19 11:21:29 +00:00
local prefix="${1}"
shift
2024-11-25 17:30:35 +00:00
[ ${#} -gt 0 ] || set -- ""
2024-11-19 11:21:29 +00:00
local line
for line in "${@}"; do
2024-11-25 17:20:08 +00:00
if [ -n "${prefix}" ]; then
2024-11-29 18:08:06 +00:00
_rwx_main_log "${prefix} ${line}"
2024-11-25 17:20:08 +00:00
else
2024-11-29 18:08:06 +00:00
_rwx_main_log "${line}"
2024-11-25 17:20:08 +00:00
fi
2024-11-19 11:21:29 +00:00
done
2024-11-19 11:29:30 +00:00
}