cfg/bin/script_framework.sh
Knut Ahlers 0093c732dc
Add configurable default log level
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-12-13 19:01:55 +01:00

60 lines
1.1 KiB
Bash

COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[0;32m"
COLOR_CYAN="\033[0;36m"
COLOR_YELLOW="\033[0;33m"
COLOR_PLAIN="\033[0m"
COLOR_PURPLE="\033[35m"
function check_util() {
command -v ${1} >/dev/null 2>&1 || fail "Missing ${1} util"
}
function debug() {
log_level_matches 0 || return 0
echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}"
}
function error() {
log_level_matches 3 || return 0
echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2
}
function fail() {
error "$@"
exit 1
}
function fatal() {
fail "$@"
}
function info() {
log_level_matches 1 || return 0
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
)
local fb_ll=${log_levels[${DEFAULT_LOG_LEVEL:-info}]}
[ ${log_levels[${LOG_LEVEL:-UNDEF}]:-${fb_ll}} -le ${1} ] && return 0 || return 1
}
function step() {
info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@"
}
function success() {
log_level_matches 1 || return 0
echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2
}
function warn() {
log_level_matches 2 || return 0
echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2
}