Merge pull request #1628 from NoahGorny/add-load-logs

Add load/reloader logs
pull/1634/head
Nils Winkler 2020-07-07 13:10:32 +02:00 committed by GitHub
commit 2814946d86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 124 additions and 46 deletions

View File

@ -4,12 +4,19 @@ This page summarizes a couple of rules to keep in mind when developing features
## Debugging and Logging ## Debugging and Logging
### General Logging
While developing feature or making changes in general, you can log error/warning/debug While developing feature or making changes in general, you can log error/warning/debug
using `_log_error` `_log_warning` and `_log_debug`. This will help you solve problems quicker using `_log_error` `_log_warning` and `_log_debug`. This will help you solve problems quicker
and also propagate important notes to other users of Bash-it. and also propagate important notes to other users of Bash-it.
You can see the logs by using `bash-it doctor` command to reload and see the logs. You can see the logs by using `bash-it doctor` command to reload and see the logs.
Alternatively, you can set `BASH_IT_LOG_LEVEL` to `BASH_IT_LOG_LEVEL_ERROR`, `BASH_IT_LOG_LEVEL_WARNING` or `BASH_IT_LOG_LEVEL_ALL`. Alternatively, you can set `BASH_IT_LOG_LEVEL` to `BASH_IT_LOG_LEVEL_ERROR`, `BASH_IT_LOG_LEVEL_WARNING` or `BASH_IT_LOG_LEVEL_ALL`.
### Log Prefix/Context
You can define `BASH_IT_LOG_PREFIX` in your files in order to a have a constant prefix before your logs.
Note that we prefer to uses "tags" based logging, i.e `plugins: git: DEBUG: Loading git plugin`.
## Load Order ## Load Order

View File

@ -1,37 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Initialize Bash It # Initialize Bash It
BASH_IT_LOG_PREFIX="core: main: "
# Only set $BASH_IT if it's not already set # Only set $BASH_IT if it's not already set
if [ -z "$BASH_IT" ]; if [ -z "$BASH_IT" ];
then then
# Setting $BASH to maintain backwards compatibility # Setting $BASH to maintain backwards compatibility
# TODO: warn users that they should upgrade their .bash_profile
export BASH_IT=$BASH export BASH_IT=$BASH
BASH="$(bash -c 'echo $BASH')" BASH="$(bash -c 'echo $BASH')"
export BASH export BASH
fi BASH_IT_OLD_BASH_SETUP=true
# For backwards compatibility, look in old BASH_THEME location
if [ -z "$BASH_IT_THEME" ];
then
# TODO: warn users that they should upgrade their .bash_profile
export BASH_IT_THEME="$BASH_THEME";
unset BASH_THEME;
fi fi
# Load composure first, so we support function metadata # Load composure first, so we support function metadata
# shellcheck source=./lib/composure.bash # shellcheck source=./lib/composure.bash
source "${BASH_IT}/lib/composure.bash" source "${BASH_IT}/lib/composure.bash"
# We need to load logging module first as well in order to be able to log
# shellcheck source=./lib/log.bash
source "${BASH_IT}/lib/log.bash"
# We can only log it now
[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!"
# For backwards compatibility, look in old BASH_THEME location
if [ -z "$BASH_IT_THEME" ];
then
_log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!"
export BASH_IT_THEME="$BASH_THEME";
unset BASH_THEME;
fi
# support 'plumbing' metadata # support 'plumbing' metadata
cite _about _param _example _group _author _version cite _about _param _example _group _author _version
# libraries, but skip appearance (themes) for now # libraries, but skip appearance (themes) for now
_log_debug "Loading libraries(except appearance)..."
LIB="${BASH_IT}/lib/*.bash" LIB="${BASH_IT}/lib/*.bash"
APPEARANCE_LIB="${BASH_IT}/lib/appearance.bash" APPEARANCE_LIB="${BASH_IT}/lib/appearance.bash"
for _bash_it_config_file in $LIB for _bash_it_config_file in $LIB
do do
if [ "$_bash_it_config_file" != "$APPEARANCE_LIB" ]; then if [ "$_bash_it_config_file" != "$APPEARANCE_LIB" ]; then
filename=${_bash_it_config_file##*/}
filename=${filename%.bash}
BASH_IT_LOG_PREFIX="lib: ${filename}: "
_log_debug "Loading library file..."
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$_bash_it_config_file" source "$_bash_it_config_file"
fi fi
@ -51,36 +63,51 @@ done
# Load theme, if a theme was set # Load theme, if a theme was set
if [[ ! -z "${BASH_IT_THEME}" ]]; then if [[ ! -z "${BASH_IT_THEME}" ]]; then
_log_debug "Loading \"${BASH_IT_THEME}\" theme..."
# Load colors and helpers first so they can be used in base theme # Load colors and helpers first so they can be used in base theme
BASH_IT_LOG_PREFIX="themes: colors: "
# shellcheck source=./themes/colors.theme.bash # shellcheck source=./themes/colors.theme.bash
source "${BASH_IT}/themes/colors.theme.bash" source "${BASH_IT}/themes/colors.theme.bash"
BASH_IT_LOG_PREFIX="themes: githelpers: "
# shellcheck source=./themes/githelpers.theme.bash # shellcheck source=./themes/githelpers.theme.bash
source "${BASH_IT}/themes/githelpers.theme.bash" source "${BASH_IT}/themes/githelpers.theme.bash"
BASH_IT_LOG_PREFIX="themes: p4helpers: "
# shellcheck source=./themes/p4helpers.theme.bash # shellcheck source=./themes/p4helpers.theme.bash
source "${BASH_IT}/themes/p4helpers.theme.bash" source "${BASH_IT}/themes/p4helpers.theme.bash"
BASH_IT_LOG_PREFIX="themes: base: "
# shellcheck source=./themes/base.theme.bash # shellcheck source=./themes/base.theme.bash
source "${BASH_IT}/themes/base.theme.bash" source "${BASH_IT}/themes/base.theme.bash"
BASH_IT_LOG_PREFIX="lib: appearance: "
# appearance (themes) now, after all dependencies # appearance (themes) now, after all dependencies
# shellcheck source=./lib/appearance.bash # shellcheck source=./lib/appearance.bash
source "$APPEARANCE_LIB" source "$APPEARANCE_LIB"
fi fi
# Load custom aliases, completion, plugins BASH_IT_LOG_PREFIX="core: main: "
_log_debug "Loading custom aliases, completion, plugins..."
for file_type in "aliases" "completion" "plugins" for file_type in "aliases" "completion" "plugins"
do do
if [ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ] if [ -e "${BASH_IT}/${file_type}/custom.${file_type}.bash" ]
then then
BASH_IT_LOG_PREFIX="${file_type}: custom: "
_log_debug "Loading component..."
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${BASH_IT}/${file_type}/custom.${file_type}.bash" source "${BASH_IT}/${file_type}/custom.${file_type}.bash"
fi fi
done done
# Custom # Custom
BASH_IT_LOG_PREFIX="core: main: "
_log_debug "Loading general custom files..."
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash" CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash"
for _bash_it_config_file in $CUSTOM for _bash_it_config_file in $CUSTOM
do do
if [ -e "${_bash_it_config_file}" ]; then if [ -e "${_bash_it_config_file}" ]; then
filename=$(basename "${_bash_it_config_file}")
filename=${filename%*.bash}
BASH_IT_LOG_PREFIX="custom: $filename"
_log_debug "Loading custom file..."
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$_bash_it_config_file" source "$_bash_it_config_file"
fi fi

View File

@ -1,15 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function _has_colors()
{
# Check that stdout is a terminal
test -t 1 || return 1
ncolors=$(tput colors)
test -n "$ncolors" && test "$ncolors" -ge 8 || return 1
return 0
}
# colored ls # colored ls
export LSCOLORS='Gxfxcxdxdxegedabagacad' export LSCOLORS='Gxfxcxdxdxegedabagacad'

View File

@ -21,7 +21,7 @@ function _command_exists ()
_param '1: command to check' _param '1: command to check'
_example '$ _command_exists ls && echo exists' _example '$ _command_exists ls && echo exists'
_group 'lib' _group 'lib'
type "$1" &> /dev/null ; type "$1" &> /dev/null || (_log_warning "Command $1 does not exist!" && return 1) ;
} }
function _make_reload_alias() { function _make_reload_alias() {

View File

@ -4,45 +4,57 @@ 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()
{
# Check that stdout is a terminal
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' 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: message to log' param '2: log level to print before the prefix'
_group 'log' param '3: message to log'
group 'log'
_has_colors && echo -e "$1$2${echo_normal}" || echo -e "$2" message=$2${BASH_IT_LOG_PREFIX}$3
_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" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return [[ "$BASH_IT_LOG_LEVEL" -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" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return [[ "$BASH_IT_LOG_LEVEL" -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" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0
_log_general "${echo_red}" "ERROR: $1" _log_general "${echo_red}" "ERROR: " "$1"
} }

View File

@ -6,6 +6,24 @@
# Generic utilies # Generic utilies
########################################################################### ###########################################################################
_bash-it-get-component-name-from-path() {
# filename without path
filename=${1##*/}
# filename without path or priority
filename=${filename##*---}
# filename without path, priority or extension
echo ${filename%.*.bash}
}
_bash-it-get-component-type-from-path() {
# filename without path
filename=${1##*/}
# filename without path or priority
filename=${filename##*---}
# extension
echo ${filename} | cut -d '.' -f 2
}
# This function searches an array for an exact match against the term passed # This function searches an array for an exact match against the term passed
# as the first argument to the function. This function exits as soon as # as the first argument to the function. This function exits as soon as
# a match is found. # a match is found.

View File

@ -1,15 +1,26 @@
#!/bin/bash #!/bin/bash
BASH_IT_LOG_PREFIX="core: reloader: "
pushd "${BASH_IT}" >/dev/null || exit 1 pushd "${BASH_IT}" >/dev/null || exit 1
# TODO: Add debugging output function _set-prefix-based-on-path()
{
filename=$(_bash-it-get-component-name-from-path "$1")
extension=$(_bash-it-get-component-type-from-path "$1")
BASH_IT_LOG_PREFIX="$extension: $filename: "
}
if [ "$1" != "skip" ] && [ -d "./enabled" ]; then if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
_bash_it_config_type="" _bash_it_config_type=""
if [[ "${1}" =~ ^(alias|completion|plugin)$ ]]; then if [[ "${1}" =~ ^(alias|completion|plugin)$ ]]; then
_bash_it_config_type=$1 _bash_it_config_type=$1
_log_debug "Loading enabled $1 components..."
else
_log_debug "Loading all enabled components..."
fi fi
for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do
if [ -e "${_bash_it_config_file}" ]; then if [ -e "${_bash_it_config_file}" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
# shellcheck source=/dev/null # shellcheck source=/dev/null
source $_bash_it_config_file source $_bash_it_config_file
else else
@ -20,9 +31,11 @@ fi
if [ ! -z "${2}" ] && [[ "${2}" =~ ^(aliases|completion|plugins)$ ]] && [ -d "${2}/enabled" ]; then if [ ! -z "${2}" ] && [[ "${2}" =~ ^(aliases|completion|plugins)$ ]] && [ -d "${2}/enabled" ]; then
# TODO: We should warn users they're using legacy enabling _log_warning "Using legacy enabling for $2, please update your bash-it version and migrate"
for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do
if [ -e "$_bash_it_config_file" ]; then if [ -e "$_bash_it_config_file" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}"
_log_debug "Loading component..."
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "$_bash_it_config_file" source "$_bash_it_config_file"
else else

View File

@ -2,6 +2,7 @@
load ../test_helper load ../test_helper
load ../../lib/composure load ../../lib/composure
load ../../lib/log
load ../../lib/utilities load ../../lib/utilities
load ../../lib/search load ../../lib/search
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin

View File

@ -77,3 +77,10 @@ load ../../lib/log
run _log_error "test test test" run _log_error "test test test"
refute_output refute_output
} }
@test "lib log: logging with prefix" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
BASH_IT_LOG_PREFIX="nice: prefix: "
run _log_debug "test test test"
assert_output "DEBUG: nice: prefix: test test test"
}

View File

@ -2,6 +2,7 @@
load ../test_helper load ../test_helper
load ../../lib/composure load ../../lib/composure
load ../../lib/log
cite _about _param _example _group _author _version cite _about _param _example _group _author _version

View File

@ -2,6 +2,7 @@
load ../test_helper load ../test_helper
load ../../lib/composure load ../../lib/composure
load ../../lib/log
cite _about _param _example _group _author _version cite _about _param _example _group _author _version

View File

@ -2,6 +2,7 @@
load ../test_helper load ../test_helper
load ../../lib/composure load ../../lib/composure
load ../../lib/log
cite _about _param _example _group _author _version cite _about _param _example _group _author _version