|
1 |
| -#!/usr/bin/env bash |
| 1 | +# shellcheck shell=bash |
| 2 | +# |
| 3 | +# A collection of logging functions. |
2 | 4 |
|
3 |
| -export BASH_IT_LOG_LEVEL_ERROR=1 |
4 |
| -export BASH_IT_LOG_LEVEL_WARNING=2 |
5 |
| -export BASH_IT_LOG_LEVEL_ALL=3 |
| 5 | +# Declare log severity levels, matching syslog numbering |
| 6 | +: "${BASH_IT_LOG_LEVEL_FATAL:=1}" |
| 7 | +: "${BASH_IT_LOG_LEVEL_ERROR:=3}" |
| 8 | +: "${BASH_IT_LOG_LEVEL_WARNING:=4}" |
| 9 | +: "${BASH_IT_LOG_LEVEL_ALL:=6}" |
| 10 | +: "${BASH_IT_LOG_LEVEL_INFO:=6}" |
| 11 | +: "${BASH_IT_LOG_LEVEL_TRACE:=7}" |
| 12 | +readonly "${!BASH_IT_LOG_LEVEL_@}" |
6 | 13 |
|
7 |
| -function _has_colors() |
8 |
| -{ |
9 |
| - # Check that stdout is a terminal |
10 |
| - test -t 1 || return 1 |
| 14 | +function _bash-it-log-prefix-by-path() { |
| 15 | + local component_path="${1?${FUNCNAME[0]}: path specification required}" |
| 16 | + local without_extension component_directory |
| 17 | + local component_filename component_type component_name |
11 | 18 |
|
12 |
| - ncolors=$(tput colors) |
13 |
| - test -n "$ncolors" && test "$ncolors" -ge 8 || return 1 |
14 |
| - return 0 |
| 19 | + # get the directory, if any |
| 20 | + component_directory="${component_path%/*}" |
| 21 | + # drop the directory, if any |
| 22 | + component_filename="${component_path##*/}" |
| 23 | + # strip the file extension |
| 24 | + without_extension="${component_filename%.bash}" |
| 25 | + # strip before the last dot |
| 26 | + component_type="${without_extension##*.}" |
| 27 | + # strip component type, but try not to strip other words |
| 28 | + # - aliases, completions, plugins, themes |
| 29 | + component_name="${without_extension%.[acpt][hlo][eimu]*[ens]}" |
| 30 | + # Finally, strip load priority prefix |
| 31 | + component_name="${component_name##[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR:----}"}" |
| 32 | + |
| 33 | + # best-guess for files without a type |
| 34 | + if [[ "${component_type:-${component_name}}" == "${component_name}" ]]; then |
| 35 | + if [[ "${component_directory}" == *'vendor'* ]]; then |
| 36 | + component_type='vendor' |
| 37 | + else |
| 38 | + component_type="${component_directory##*/}" |
| 39 | + fi |
| 40 | + fi |
| 41 | + |
| 42 | + # shellcheck disable=SC2034 |
| 43 | + BASH_IT_LOG_PREFIX="${component_type:-lib}: $component_name" |
| 44 | +} |
| 45 | + |
| 46 | +function _has_colors() { |
| 47 | + # Check that stdout is a terminal, and that it has at least 8 colors. |
| 48 | + [[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]] |
15 | 49 | }
|
16 | 50 |
|
17 |
| -function _log_general() |
18 |
| -{ |
19 |
| - about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' |
20 |
| - param '1: color of the message' |
21 |
| - param '2: log level to print before the prefix' |
22 |
| - param '3: message to log' |
23 |
| - group 'log' |
| 51 | +function _bash-it-log-message() { |
| 52 | + about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' |
| 53 | + param '1: color of the message' |
| 54 | + param '2: log level to print before the prefix' |
| 55 | + param '3: message to log' |
| 56 | + group 'log' |
24 | 57 |
|
25 |
| - message=$2${BASH_IT_LOG_PREFIX:-default: }$3 |
26 |
| - _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" |
| 58 | + message="$2${BASH_IT_LOG_PREFIX:-default: }$3" |
| 59 | + _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" |
27 | 60 | }
|
28 | 61 |
|
29 |
| -function _log_debug() |
30 |
| -{ |
31 |
| - about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' |
32 |
| - param '1: message to log' |
33 |
| - example '$ _log_debug "Loading plugin git..."' |
34 |
| - group 'log' |
| 62 | +function _log_debug() { |
| 63 | + about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_INFO' |
| 64 | + param '1: message to log' |
| 65 | + example '$ _log_debug "Loading plugin git..."' |
| 66 | + group 'log' |
35 | 67 |
|
36 |
| - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 |
37 |
| - _log_general "${echo_green:-}" "DEBUG: " "$1" |
| 68 | + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]] || return 0 |
| 69 | + _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" |
38 | 70 | }
|
39 | 71 |
|
40 |
| -function _log_warning() |
41 |
| -{ |
42 |
| - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' |
43 |
| - param '1: message to log' |
44 |
| - example '$ _log_warning "git binary not found, disabling git plugin..."' |
45 |
| - group 'log' |
| 72 | +function _log_warning() { |
| 73 | + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' |
| 74 | + param '1: message to log' |
| 75 | + example '$ _log_warning "git binary not found, disabling git plugin..."' |
| 76 | + group 'log' |
46 | 77 |
|
47 |
| - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 |
48 |
| - _log_general "${echo_yellow:-}" " WARN: " "$1" |
| 78 | + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]] || return 0 |
| 79 | + _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" |
49 | 80 | }
|
50 | 81 |
|
51 |
| -function _log_error() |
52 |
| -{ |
53 |
| - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' |
54 |
| - param '1: message to log' |
55 |
| - example '$ _log_error "Failed to load git plugin..."' |
56 |
| - group 'log' |
| 82 | +function _log_error() { |
| 83 | + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' |
| 84 | + param '1: message to log' |
| 85 | + example '$ _log_error "Failed to load git plugin..."' |
| 86 | + group 'log' |
57 | 87 |
|
58 |
| - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 |
59 |
| - _log_general "${echo_red:-}" "ERROR: " "$1" |
| 88 | + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]] || return 0 |
| 89 | + _bash-it-log-message "${echo_red:-}" "ERROR: " "$1" |
60 | 90 | }
|
0 commit comments