Merge pull request #2088 from gaelicWizard/lib/log

lib/log: //echo/printf
pull/2096/head
Noah Gorny 2022-02-16 22:18:22 +02:00 committed by GitHub
commit 9d6fe72267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 25 deletions

View File

@ -49,42 +49,52 @@ function _has_colors() {
} }
function _bash-it-log-message() { function _bash-it-log-message() {
about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' : _about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix'
param '1: color of the message' : _param '1: color of the message'
param '2: log level to print before the prefix' : _param '2: log level to print before the prefix'
param '3: message to log' : _param '3: message to log'
group 'log' : _group 'log'
message="$2${BASH_IT_LOG_PREFIX:-default: }$3" local prefix="${BASH_IT_LOG_PREFIX:-default}"
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" 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() { function _log_debug() {
about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_INFO' : _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' : _param '1: message to log'
example '$ _log_debug "Loading plugin git..."' : _example '$ _log_debug "Loading plugin git..."'
group 'log' : _group 'log'
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]] || return 0 if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]]; then
_bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1"
fi
} }
function _log_warning() { function _log_warning() {
about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_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' : _param '1: message to log'
example '$ _log_warning "git binary not found, disabling git plugin..."' : _example '$ _log_warning "git binary not found, disabling git plugin..."'
group 'log' : _group 'log'
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]] || return 0 if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]]; then
_bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1"
fi
} }
function _log_error() { function _log_error() {
about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_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' : _param '1: message to log'
example '$ _log_error "Failed to load git plugin..."' : _example '$ _log_error "Failed to load git plugin..."'
group 'log' : _group 'log'
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]] || return 0 if [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]]; then
_bash-it-log-message "${echo_red:-}" "ERROR: " "$1" _bash-it-log-message "${echo_red:-}" "ERROR: " "$1"
fi
} }