lib/log: `shellcheck` && `shfmt`
Alsö, fix tests to load `lib/colors` instead of `lib/appearance`...wut Alsö, `short-circuit _has_colors()`: If we already looked up colors, and we already have them, then don't run `tput` again. My apologies to future `git blame` hunters ♥pull/1959/head
parent
86c1e3c043
commit
b772e6ace7
|
|
@ -78,6 +78,7 @@ completion/available/vuejs.completion.bash
|
||||||
completion/available/wpscan.completion.bash
|
completion/available/wpscan.completion.bash
|
||||||
|
|
||||||
# libraries
|
# libraries
|
||||||
|
lib/log.bash
|
||||||
lib/utilities.bash
|
lib/utilities.bash
|
||||||
|
|
||||||
# plugins
|
# plugins
|
||||||
|
|
|
||||||
77
lib/log.bash
77
lib/log.bash
|
|
@ -1,60 +1,53 @@
|
||||||
#!/usr/bin/env bash
|
# shellcheck shell=bash
|
||||||
|
#
|
||||||
|
# A collection of logging functions.
|
||||||
|
|
||||||
export BASH_IT_LOG_LEVEL_ERROR=1
|
export BASH_IT_LOG_LEVEL_ERROR=1
|
||||||
export BASH_IT_LOG_LEVEL_WARNING=2
|
export BASH_IT_LOG_LEVEL_WARNING=2
|
||||||
export BASH_IT_LOG_LEVEL_ALL=3
|
export BASH_IT_LOG_LEVEL_ALL=3
|
||||||
|
|
||||||
function _has_colors()
|
function _has_colors() {
|
||||||
{
|
# Check that stdout is a terminal, and that it has at least 8 colors.
|
||||||
# Check that stdout is a terminal
|
[[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]]
|
||||||
test -t 1 || return 1
|
|
||||||
|
|
||||||
ncolors=$(tput colors)
|
|
||||||
test -n "$ncolors" && test "$ncolors" -ge 8 || return 1
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _log_general()
|
function _log_general() {
|
||||||
{
|
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
|
message=$2${BASH_IT_LOG_PREFIX:-default: }$3
|
||||||
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
|
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
|
||||||
}
|
}
|
||||||
|
|
||||||
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_ALL'
|
||||||
about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL'
|
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_ALL ]] || return 0
|
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0
|
||||||
_log_general "${echo_green:-}" "DEBUG: " "$1"
|
_log_general "${echo_green:-}" "DEBUG: " "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0
|
||||||
_log_general "${echo_yellow:-}" " WARN: " "$1"
|
_log_general "${echo_yellow:-}" " WARN: " "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
[[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0
|
||||||
_log_general "${echo_red:-}" "ERROR: " "$1"
|
_log_general "${echo_red:-}" "ERROR: " "$1"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue