Skip to content

Commit

Permalink
make log consistent with other loggers
Browse files Browse the repository at this point in the history
The three-letter-log-levels have been removed in favor of well-known
log level names.
  • Loading branch information
georglauterbach committed Feb 18, 2024
1 parent d7310ad commit 084be15
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 104 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ When you use `libbash`, you don't have to use all the code that `libbash` contai
To load a module, just specify its name after the `source` command, as shown in the examples above in [the usage section](#usage). All modules are located in the `modules` directory, and their name is just the file name without `.sh` at the end. When you open the file, you will see all the functions the module provides. These functions have Rust-like documentation comments above their definitions to give you a concise overview of what the function does.
1. `tra` or `trace`
2. `deb` or `debug`
3. `inf` or `info`
4. `war` or `warn`
5. `err` or `error`
### [`cri`](./modules/cri.sh)
This module provides the `setup_container_runtime` function to detect the container runtime. It will set the `CRI` variable to `docker` or `podman` or return with exit status 1 if no container runtime could be identified.
Expand Down
36 changes: 18 additions & 18 deletions load
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# executed by scripts that use `libbash`
# task perform library initialization

if [[ ! -v __LIBBASH_IS_LOADED ]]
if [[ ! -v __LIBBASH__IS_LOADED ]]
then
# ### Show an Error
#
Expand All @@ -27,7 +27,7 @@ then
# ### Exit With Conditions
#
# This function will exit when not running interactively or when
# 'LIBBASH_EXIT_IN_INTERACTIVE_MODE' is set to 'true'.
# 'LIBBASH_EXIT_IN_INTERACTIVE_MODE' is set to '1'.
#
# #### Special
#
Expand All @@ -37,22 +37,22 @@ then
function __libbash__exit_checked() {
local CODE=${1:-1}
if [[ ${-} != *i* ]] \
|| ${LIBBASH_EXIT_IN_INTERACTIVE_MODE:-false} \
|| [[ ${LIBBASH_EXIT_IN_INTERACTIVE_MODE:-0} -eq 1 ]] \
|| [[ ${*} == *--force* ]]
then
if [[ ${CODE} =~ ^[0-9]$ ]]
then
exit "${CODE}"
else
log 'err' "Supplied non-number code ('${CODE}') to __libbash__exit_checked"
log 'error' "Supplied non-number code ('${CODE}') to __libbash__exit_checked"
exit 1
fi
else
if [[ ${CODE} =~ ^[0-9]$ ]]
then
return "${CODE}"
else
log 'err' "Supplied non-number code ('${CODE}') to __libbash__exit_checked"
log 'error' "Supplied non-number code ('${CODE}') to __libbash__exit_checked"
return 1
fi
fi
Expand Down Expand Up @@ -101,21 +101,21 @@ then
#
# Shows some debug information that `libbash` has collected.
function libbash__debug() {
if [[ ! -v __LIBBASH_IS_LOADED_LOG ]]
if [[ ! -v __LIBBASH__IS_LOADED_LOG ]]
then
__libbash__exit_with_error_and_callstack "Debug functionality requires the log module to be loaded and log level set to 'deb' or 'tra'"
__libbash__exit_with_error_and_callstack "Debug functionality requires the log module to be loaded and log level set to 'debug' or 'trace'"
return 1
fi

log 'deb' "Showing 'libbash' debug information"
log 'deb' " -> Loaded modules: ${LIBBASH_LOADED_MODULES[*]}"
log 'debug' "Showing 'libbash' debug information"
log 'debug' " -> Loaded modules: ${LIBBASH_LOADED_MODULES[*]}"
}
export -f libbash__debug

export LIBBASH_EXIT_IN_INTERACTIVE_MODE=${LIBBASH_EXIT_IN_INTERACTIVE_MODE:-false}
export __LIBBASH_IS_LOADED=true
readonly __LIBBASH_IS_LOADED
fi # if ! ${__LIBBASH_IS_LOADED:-false}
export LIBBASH_EXIT_IN_INTERACTIVE_MODE=${LIBBASH_EXIT_IN_INTERACTIVE_MODE:-0}
export __LIBBASH__IS_LOADED=1
readonly __LIBBASH__IS_LOADED
fi # if ! ${__LIBBASH__IS_LOADED:-false}

# ### The Start of `libbash`
#
Expand Down Expand Up @@ -174,14 +174,14 @@ function __libbash__main() {
# ### Fallback `log`
#
# If the `log` module was not sourced, provide a fallback
# `log` implementation, but only for `err` messages.
# `log` implementation, but only for error messages.
function libbash__setup_default_notify_error() {
function log() {
if [[ ${1:-} != 'err' ]]
if [[ ${1:-} != 'error' ]]
then
__libbash__exit_with_error_and_callstack \
__libbash__exit_with_error_and_callstack \
"log module was not loaded but 'log' was called" \
"with log level not 'err'" \
"with log level not 'error'" \
"(arguments were: ${*})"
return 1
fi
Expand Down Expand Up @@ -210,7 +210,7 @@ function __libbash__main() {
return 0
fi

log 'tra' "Finished 'libbash' initialization"
log 'trace' "Finished 'libbash' initialization"
}

__libbash__main "${@}"
Expand Down
2 changes: 1 addition & 1 deletion modules/cri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function setup_container_runtime() {
command -v 'docker' &>/dev/null && export CRI='docker' && return 0
command -v 'podman' &>/dev/null && export CRI='podman' && return 0

log 'err' \
log 'error' \
'Could not identify Container Runtime.' \
"Is 'docker' or 'podman' in \${PATH}?"
return 1
Expand Down
4 changes: 2 additions & 2 deletions modules/errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ shopt -s inherit_errexit

trap '__log_unexpected_error "${FUNCNAME[0]:-}" "${BASH_COMMAND:-}" "${LINENO:-}" "${?:-}"' ERR

# ### Log the `ERR` Event
# ### Log the Error Event
#
# This function is called when an unhandled `ERR` signal is thrown.
# This function is called when an unhandled error signal is thrown.
# It prints information about the error (where it originated, etc.)
# and also calls `__libbash__show_call_stack` to possibly print a
# call stack if `__libbash__show_call_stack` deems it useful.
Expand Down
50 changes: 25 additions & 25 deletions modules/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ export LIBBASH__LOG_COLOR_INF='\e[34m'
export LIBBASH__LOG_COLOR_WAR='\e[93m'
export LIBBASH__LOG_COLOR_ERR='\e[91m'

if [[ ! -v __LIBBASH_IS_LOADED_LOG ]]
if [[ ! -v __LIBBASH__IS_LOADED_LOG ]]
then
export __LIBBASH_IS_LOADED_LOG=true
readonly __LIBBASH_IS_LOADED_LOG
export __LIBBASH__IS_LOADED_LOG=1
readonly __LIBBASH__IS_LOADED_LOG
fi

# ### The Logging Functions
#
# `log` is used for logging. It uses five different log levels
#
# `err` | `war` | `inf` | `deb` | `tra`
# `error` | `warn` | `info` | `debug` | `trace`
#
# and behaves as you would expect from a log function: you provide
# the log level as the first argument and the message in the con-
Expand All @@ -48,13 +48,13 @@ function log() {
#
# Can be one of
#
# value => meaning - what to log
# meaning - what to log
# -------------------------------------------------
# tra => trace - log trace information
# deb => debug - log debug information
# inf => info - log informational output
# war => warning - log warnings
# err => error - log critical errors and aborts
# trace - log trace information
# debug - log debug information
# info - log informational output
# warn - log warnings
# error - log critical errors and aborts
#
# where a higher level includes the level below. The
# default log level is 'inf' (2).
Expand All @@ -63,11 +63,11 @@ function log() {

case "${LOG_LEVEL:-inf}"
in
( 'err' | 'error' ) LOG_LEVEL_AS_INTEGER=0 ;;
( 'war' | 'warn' ) LOG_LEVEL_AS_INTEGER=1 ;;
( 'inf' | 'info' ) LOG_LEVEL_AS_INTEGER=2 ;;
( 'deb' | 'debug' ) LOG_LEVEL_AS_INTEGER=3 ;;
( 'tra' | 'trace' ) LOG_LEVEL_AS_INTEGER=4 ;;
( 'error' ) LOG_LEVEL_AS_INTEGER=0 ;;
( 'warn' ) LOG_LEVEL_AS_INTEGER=1 ;;
( 'info' ) LOG_LEVEL_AS_INTEGER=2 ;;
( 'debug' ) LOG_LEVEL_AS_INTEGER=3 ;;
( 'trace' ) LOG_LEVEL_AS_INTEGER=4 ;;
( * )
local OLD_LOG_LEVEL=${LOG_LEVEL}
LOG_LEVEL='info'
Expand All @@ -77,28 +77,28 @@ function log() {

case "${MESSAGE_LOG_LEVEL}"
in
( 'tra' | 'trace' )
( 'trace' )
[[ ${LOG_LEVEL_AS_INTEGER} -lt 4 ]] && return 0
__log_generic 'tra' 'TRACE' "${*}"
__log_generic 'trace' 'TRACE' "${*}"
;;

( 'deb' | 'debug' )
( 'debug' )
[[ ${LOG_LEVEL_AS_INTEGER} -lt 3 ]] && return 0
__log_generic 'deb' 'DEBUG' "${*}"
__log_generic 'debug' 'DEBUG' "${*}"
;;

( 'inf' | 'info' )
( 'info' )
[[ "${LOG_LEVEL_AS_INTEGER}" -lt 2 ]] && return 0
__log_generic 'inf' 'INFO ' "${*}"
__log_generic 'info' 'INFO ' "${*}"
;;

( 'war' | 'warning' )
( 'warn' )
[[ "${LOG_LEVEL_AS_INTEGER}" -lt 1 ]] && return 0
__log_generic 'war' 'WARN ' "${*}"
__log_generic 'warn' 'WARN ' "${*}"
;;

( 'err' | 'error' )
__log_generic 'err' 'ERROR' "${*}"
( 'error' )
__log_generic 'error' 'ERROR' "${*}"
;;

( * )
Expand Down
30 changes: 15 additions & 15 deletions modules/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ function line_is_comment_or_blank() {
# $2 :: character that must be escaped
function escape() {
[[ ${2} =~ .*\\.* ]] && {
log 'err' \
log 'error' \
"Escape character is not allowed to be or contain a backslash"\
"(use 'escape_backslash')"
return 1
}

var_is_set_and_not_empty "${1}" || {
log 'err' 'No string to be escaped provided'
log 'error' 'No string to be escaped provided'
return 1
}

var_is_set_and_not_empty "${2}" || {
log 'err' 'No escape character(s) provided'
log 'error' 'No escape character(s) provided'
return 1
}

[[ ${#2} -ge 2 ]] && {
log 'err' 'More than two parameters provided'
log 'error' 'More than two parameters provided'
return 1
}

Expand Down Expand Up @@ -124,19 +124,19 @@ function exit_failure() {

if [[ ! ${CODE} =~ ^[0-9]+$ ]]
then
log 'err' "'exit_failure' was called with non-number exit code"
log 'error' "'exit_failure' was called with non-number exit code"
__libbash__show_call_stack
exit 1
fi

if [[ ${CODE} -eq 0 ]] || [[ ${CODE} -ge 127 ]]
then
log 'err' "'exit_failure' was called with exit code 0 or >127"
log 'error' "'exit_failure' was called with exit code 0 or >127"
__libbash__show_call_stack
exit 1
fi

var_is_set_and_not_empty "${1}" && log 'err' "${1}"
var_is_set_and_not_empty "${1}" && log 'error' "${1}"
exit "${CODE}"
}

Expand All @@ -161,19 +161,19 @@ function return_success() { return 0 ; }
function return_failure() {
if [[ ! ${1:-1} =~ ^[0-9]+$ ]]
then
log 'err' "'return_failure' was called with non-number exit code"
log 'error' "'return_failure' was called with non-number exit code"
__libbash__show_call_stack
exit 1
fi

if [[ ${1:-1} -eq 0 ]] || [[ ${1:-1} -ge 128 ]]
then
log 'err' "'return_failure' was called with exit code 0 or >127"
log 'error' "'return_failure' was called with exit code 0 or >127"
__libbash__show_call_stack
exit 1
fi

var_is_set_and_not_empty "${*}" && log 'err' "${*}"
var_is_set_and_not_empty "${*}" && log 'error' "${*}"
return "${1:-1}"
}

Expand All @@ -187,7 +187,7 @@ function return_failure() {
#
# Same as `exit_failure`.
function exit_failure_show_callstack() {
var_is_set_and_not_empty "${2:-}" && log 'err' "${2}"
var_is_set_and_not_empty "${2:-}" && log 'error' "${2}"
__libbash__show_call_stack
exit_failure "${1:-}"
}
Expand Down Expand Up @@ -215,12 +215,12 @@ function var_is_set_and_not_empty() {
# $2 :: variable name to store the result in
function ask_question() {
var_is_set_and_not_empty "${1}" || {
log 'err' 'No question provided'
log 'error' 'No question provided'
return 1
}

var_is_set_and_not_empty "${2}" || {
log 'err' 'No variable to store result in provided'
log 'error' 'No variable to store result in provided'
return 1
}

Expand All @@ -244,7 +244,7 @@ function ask_question() {
# $2 :: default (optional, default=no)
function ask_yes_no_question() {
var_is_set_and_not_empty "${1}" || {
log 'err' 'No question provided'
log 'error' 'No question provided'
return 1
}

Expand Down Expand Up @@ -283,7 +283,7 @@ function ask_yes_no_question() {
# $1 :: executable to check
function is_in_path() {
var_is_set_and_not_empty "${1}" || {
log 'err' 'No name for an executable provided'
log 'error' 'No name for an executable provided'
return 1
}

Expand Down
2 changes: 1 addition & 1 deletion tests/10-sourcing.bats
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function setup_file() {
assert_failure

source load 'log' 'utils'
LOG_LEVEL='tra'
LOG_LEVEL='trace'
run libbash__debug
assert_success
assert_output --partial 'Loaded modules: log utils'
Expand Down
Loading

0 comments on commit 084be15

Please sign in to comment.