Rework script_framework, add log-levels

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-12-13 18:57:02 +01:00
parent 721a28bef4
commit 498e139266
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -6,15 +6,16 @@ COLOR_PLAIN="\033[0m"
COLOR_PURPLE="\033[35m" COLOR_PURPLE="\033[35m"
function check_util() { function check_util() {
which ${1} >/dev/null 2>&1 || fail "Missing ${1} util" command -v ${1} >/dev/null 2>&1 || fail "Missing ${1} util"
} }
function debug() { function debug() {
[[ ${DEBUG:-false} != false ]] || return 0 log_level_matches 0 || return 0
echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}" echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}"
} }
function error() { function error() {
log_level_matches 3 || return 0
echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2 echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2
} }
@ -28,17 +29,31 @@ function fatal() {
} }
function info() { function info() {
log_level_matches 1 || return 0
echo -e "${COLOR_CYAN}$@${COLOR_PLAIN}" >&2 echo -e "${COLOR_CYAN}$@${COLOR_PLAIN}" >&2
} }
function log_level_matches() {
declare -A log_levels=(
[debug]=0
[info]=1
[warn]=2
[warning]=2
[error]=3
)
[ ${log_levels[${LOG_LEVEL:-UNDEF}]:-1} -le ${1} ] && return 0 || return 1
}
function step() { function step() {
info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@" info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@"
} }
function success() { function success() {
log_level_matches 1 || return 0
echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2 echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2
} }
function warn() { function warn() {
log_level_matches 2 || return 0
echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2 echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2
} }