From 61b6393a4a0cb161d26d875626a09c0a6faf57d4 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 14 Feb 2022 15:40:37 -0800 Subject: [PATCH 1/2] lib/log: //echo/printf - Replace `echo -e` with `printf` in `_bash-it-log-message()`. - Local positional parameters to allow for defaults. - Use `if`/`then` properly. - Clean up use of `$BASH_IT_LOG_PREFIX` slightly (eliminate duplicate colons). --- lib/log.bash | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/log.bash b/lib/log.bash index 444a6854..d37859cc 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -55,8 +55,15 @@ function _bash-it-log-message() { param '3: message to log' group 'log' - message="$2${BASH_IT_LOG_PREFIX:-default: }$3" - _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" + local prefix="${BASH_IT_LOG_PREFIX:-default}" + local color="${1-${echo_cyan:-}}" + local level="${2:-TRACE}" + local message="${level%: }: ${prefix%: }: ${3?}" + if _has_colors; then + printf '%b%s%b\n' "${color}" "${message}" "${echo_normal:-}" + else + printf '%s\n' "${message}" + fi } function _log_debug() { @@ -65,8 +72,9 @@ function _log_debug() { example '$ _log_debug "Loading plugin git..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]] || return 0 - _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" + if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]]; then + _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" + fi } function _log_warning() { @@ -75,8 +83,9 @@ 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 - _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" + if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]]; then + _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" + fi } function _log_error() { @@ -85,6 +94,7 @@ 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 - _bash-it-log-message "${echo_red:-}" "ERROR: " "$1" + if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]]; then + _bash-it-log-message "${echo_red:-}" "ERROR: " "$1" + fi } From e7b91e7be5255255fafdf7cac51d8e5352975e33 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 15 Feb 2022 22:20:19 -0800 Subject: [PATCH 2/2] lib/log: use newly supported `composure.sh` feature - these functions can now run even if `composure.sh` has *not* been loaded at all! --- lib/log.bash | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/log.bash b/lib/log.bash index d37859cc..87b9ddf1 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -49,11 +49,11 @@ function _has_colors() { } 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' + : _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' local prefix="${BASH_IT_LOG_PREFIX:-default}" local color="${1-${echo_cyan:-}}" @@ -67,10 +67,10 @@ function _bash-it-log-message() { } function _log_debug() { - 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' + : _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' if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]]; then _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" @@ -78,10 +78,10 @@ function _log_debug() { } function _log_warning() { - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' - param '1: message to log' - example '$ _log_warning "git binary not found, disabling git plugin..."' - group 'log' + : _about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' + : _param '1: message to log' + : _example '$ _log_warning "git binary not found, disabling git plugin..."' + : _group 'log' if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]]; then _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" @@ -89,10 +89,10 @@ function _log_warning() { } function _log_error() { - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' - param '1: message to log' - example '$ _log_error "Failed to load git plugin..."' - group 'log' + : _about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' + : _param '1: message to log' + : _example '$ _log_error "Failed to load git plugin..."' + : _group 'log' if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]]; then _bash-it-log-message "${echo_red:-}" "ERROR: " "$1"