Skip to content

Commit

Permalink
lib/log: shfmt
Browse files Browse the repository at this point in the history
My apologies to future `git blame` hunters ♥
  • Loading branch information
gaelicWizard committed Sep 30, 2021
1 parent 1c84607 commit 0f778b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ completion/available/wpscan.completion.bash

# libraries
lib/helpers.bash
lib/log.bash
lib/search.bash
lib/utilities.bash

Expand Down
79 changes: 37 additions & 42 deletions lib/log.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,53 @@ export BASH_IT_LOG_LEVEL_ERROR=1
export BASH_IT_LOG_LEVEL_WARNING=2
export BASH_IT_LOG_LEVEL_ALL=3

function _has_colors()
{
# Check that stdout is a terminal
[[ -t 1 ]] || return 1

local -i ncolors
ncolors="$(tput colors)"
[[ -n "$ncolors" && "$ncolors" -ge 8 ]] || return 1
return 0
function _has_colors() {
# Check that stdout is a terminal
[[ -t 1 ]] || return 1

local -i ncolors
ncolors="$(tput colors)"
[[ -n "$ncolors" && "$ncolors" -ge 8 ]] || return 1
return 0
}

function _log_general()
{
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'
function _log_general() {
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
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
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'
param '1: message to log'
example '$ _log_debug "Loading plugin git..."'
group 'log'
function _log_debug() {
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'
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_ALL ]] || return 0
_log_general "${echo_green:-}" "DEBUG: " "$1"
}

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'
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'

[[ "${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
_log_general "${echo_yellow:-}" " WARN: " "$1"
}

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'
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'

[[ "${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
_log_general "${echo_red:-}" "ERROR: " "$1"
}

0 comments on commit 0f778b9

Please sign in to comment.