From d205420ce61cd93c6eb54b5a1aca2da457b2c858 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Thu, 15 Jul 2021 10:59:55 -0400 Subject: [PATCH] Simplify _alerting_ logic --- scriptTemplate.sh | 32 +++++++++++++------------------- utilities/alerts.bash | 40 ++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 43 deletions(-) diff --git a/scriptTemplate.sh b/scriptTemplate.sh index bee535f..b8a92a8 100755 --- a/scriptTemplate.sh +++ b/scriptTemplate.sh @@ -50,36 +50,28 @@ _mainScript_() { fi _alert_() { - # DESC: Controls all printing of messages to log files and stdout. + # DESC: Controls all printing of messages to log files and stdout. # ARGS: $1 (required) - The type of alert to print # (success, header, notice, dryrun, debug, warning, error, # fatal, info, input) # $2 (required) - The message to be printed to stdout and/or a log file - # $3 (optional) - Pass '$LINENO' to print the line number where the _alert_ was triggered + # $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered # OUTS: None - # USAGE: [ALERTTYPE] "[MESSAGE]" "$LINENO" + # USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}" # NOTES: The colors of each alert type are set in this function # For specified alert types, the funcstac will be printed local function_name color local alertType="${1}" local message="${2}" - local line="${3-}" + local line="${3-}" # Optional line number - [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" - [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" - - if [ -z "${line}" ]; then - [[ "$1" =~ ^(fatal|error|debug|warning) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} $(_functionStack_)" - else - [[ "$1" =~ ^(fatal|error|debug) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} (line: $line) $(_functionStack_)" - fi - - if [ -n "${line}" ]; then - [[ "$1" =~ ^(warning|info|notice|dryrun) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} (line: $line)" + if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} (line: ${line}) $(_functionStack_)" + elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} (line: ${line})" + elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} $(_functionStack_)" fi if [[ "${alertType}" =~ ^(error|fatal) ]]; then @@ -115,8 +107,10 @@ _alert_() { _writeToScreen_ _writeToLog_() { - [[ "${alertType}" == "input" ]] && return 0 + [[ "${alertType}" == "input" ]] && return 0 [[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0 + [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" + [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}" # Don't use colors in logs diff --git a/utilities/alerts.bash b/utilities/alerts.bash index 4ac445d..906bd70 100644 --- a/utilities/alerts.bash +++ b/utilities/alerts.bash @@ -25,38 +25,28 @@ else fi _alert_() { - # DESC: Controls all printing of messages to log files and stdout. + # DESC: Controls all printing of messages to log files and stdout. # ARGS: $1 (required) - The type of alert to print # (success, header, notice, dryrun, debug, warning, error, # fatal, info, input) # $2 (required) - The message to be printed to stdout and/or a log file - # $3 (optional) - Pass '$LINENO' to print the line number where the _alert_ was triggered + # $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered # OUTS: None - # USAGE: [ALERTTYPE] "[MESSAGE]" "$LINENO" - # NOTES: - Requires the variable LOGFILE to be set prior to - # calling this function. - # - The colors of each alert type are set in this function - # - For specified alert types, the funcstac will be printed + # USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}" + # NOTES: The colors of each alert type are set in this function + # For specified alert types, the funcstac will be printed - local scriptName logLocation logName function_name color + local function_name color local alertType="${1}" local message="${2}" - local line="${3-}" + local line="${3-}" # Optional line number - [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" - [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" - - if [ -z "${line}" ]; then - [[ "$1" =~ ^(fatal|error|debug|warning) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} $(_functionStack_)" - else - [[ "$1" =~ ^(fatal|error|debug) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} (line: $line) $(_functionStack_)" - fi - - if [ -n "${line}" ]; then - [[ "$1" =~ ^(warning|info|notice|dryrun) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ - && message="${message} (line: $line)" + if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} (line: ${line}) $(_functionStack_)" + elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} (line: ${line})" + elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then + message="${message} $(_functionStack_)" fi if [[ "${alertType}" =~ ^(error|fatal) ]]; then @@ -92,8 +82,10 @@ _alert_() { _writeToScreen_ _writeToLog_() { - [[ "${alertType}" == "input" ]] && return 0 + [[ "${alertType}" == "input" ]] && return 0 [[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0 + [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" + [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}" # Don't use colors in logs