From 7184c49b1c7f6b3382952bf362e0b33d9746e940 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 16 Oct 2021 14:40:54 -0700 Subject: [PATCH] lib/log: rename `_log_general()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ...to `_bash-it-log-message()`. alsö, add common log levels with common names. --- lib/log.bash | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/log.bash b/lib/log.bash index 35564b99..9c035467 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -2,9 +2,14 @@ # # A collection of logging functions. -export BASH_IT_LOG_LEVEL_ERROR=1 -export BASH_IT_LOG_LEVEL_WARNING=2 -export BASH_IT_LOG_LEVEL_ALL=3 +# Declare log severity levels, matching syslog numbering +: "${BASH_IT_LOG_LEVEL_FATAL:=1}" +: "${BASH_IT_LOG_LEVEL_ERROR:=3}" +: "${BASH_IT_LOG_LEVEL_WARNING:=4}" +: "${BASH_IT_LOG_LEVEL_ALL:=6}" +: "${BASH_IT_LOG_LEVEL_INFO:=6}" +: "${BASH_IT_LOG_LEVEL_TRACE:=7}" +readonly "${!BASH_IT_LOG_LEVEL_@}" function _has_colors() { # Check that stdout is a terminal @@ -20,25 +25,25 @@ function _has_colors() { return 1 } -function _log_general() { +function _bash-it-log-message() { about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' param '1: color of the message' param '2: log level to print before the prefix' param '3: message to log' group 'log' - message=$2${BASH_IT_LOG_PREFIX:-default: }$3 + message="$2${BASH_IT_LOG_PREFIX:-default: }$3" _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" } function _log_debug() { - about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' + about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_INFO' param '1: message to log' example '$ _log_debug "Loading plugin git..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 - _log_general "${echo_green:-}" "DEBUG: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]] || return 0 + _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" } function _log_warning() { @@ -47,8 +52,8 @@ function _log_warning() { example '$ _log_warning "git binary not found, disabling git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 - _log_general "${echo_yellow:-}" " WARN: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]] || return 0 + _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" } function _log_error() { @@ -57,6 +62,6 @@ function _log_error() { example '$ _log_error "Failed to load git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 - _log_general "${echo_red:-}" "ERROR: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]] || return 0 + _bash-it-log-message "${echo_red:-}" "ERROR: " "$1" }