Simplify _alerting_ logic

This commit is contained in:
Nathaniel Landau
2021-07-15 10:59:55 -04:00
parent beaa4f11e1
commit d205420ce6
2 changed files with 29 additions and 43 deletions

View File

@@ -50,36 +50,28 @@ _mainScript_() {
fi fi
_alert_() { _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 # ARGS: $1 (required) - The type of alert to print
# (success, header, notice, dryrun, debug, warning, error, # (success, header, notice, dryrun, debug, warning, error,
# fatal, info, input) # fatal, info, input)
# $2 (required) - The message to be printed to stdout and/or a log file # $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 # OUTS: None
# USAGE: [ALERTTYPE] "[MESSAGE]" "$LINENO" # USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
# NOTES: The colors of each alert type are set in this function # NOTES: The colors of each alert type are set in this function
# For specified alert types, the funcstac will be printed # For specified alert types, the funcstac will be printed
local function_name color local function_name color
local alertType="${1}" local alertType="${1}"
local message="${2}" local message="${2}"
local line="${3-}" local line="${3-}" # Optional line number
[ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" message="${message} (line: ${line}) $(_functionStack_)"
elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
if [ -z "${line}" ]; then message="${message} (line: ${line})"
[[ "$1" =~ ^(fatal|error|debug|warning) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
&& message="${message} $(_functionStack_)" 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)"
fi fi
if [[ "${alertType}" =~ ^(error|fatal) ]]; then if [[ "${alertType}" =~ ^(error|fatal) ]]; then
@@ -115,8 +107,10 @@ _alert_() {
_writeToScreen_ _writeToScreen_
_writeToLog_() { _writeToLog_() {
[[ "${alertType}" == "input" ]] && return 0 [[ "${alertType}" == "input" ]] && return 0
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && 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}" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
# Don't use colors in logs # Don't use colors in logs

View File

@@ -25,38 +25,28 @@ else
fi fi
_alert_() { _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 # ARGS: $1 (required) - The type of alert to print
# (success, header, notice, dryrun, debug, warning, error, # (success, header, notice, dryrun, debug, warning, error,
# fatal, info, input) # fatal, info, input)
# $2 (required) - The message to be printed to stdout and/or a log file # $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 # OUTS: None
# USAGE: [ALERTTYPE] "[MESSAGE]" "$LINENO" # USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
# NOTES: - Requires the variable LOGFILE to be set prior to # NOTES: The colors of each alert type are set in this function
# calling this function. # For specified alert types, the funcstac will be printed
# - 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 alertType="${1}"
local message="${2}" local message="${2}"
local line="${3-}" local line="${3-}" # Optional line number
[ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" message="${message} (line: ${line}) $(_functionStack_)"
elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
if [ -z "${line}" ]; then message="${message} (line: ${line})"
[[ "$1" =~ ^(fatal|error|debug|warning) && "${FUNCNAME[2]}" != "_trapCleanup_" ]] \ elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
&& message="${message} $(_functionStack_)" 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)"
fi fi
if [[ "${alertType}" =~ ^(error|fatal) ]]; then if [[ "${alertType}" =~ ^(error|fatal) ]]; then
@@ -92,8 +82,10 @@ _alert_() {
_writeToScreen_ _writeToScreen_
_writeToLog_() { _writeToLog_() {
[[ "${alertType}" == "input" ]] && return 0 [[ "${alertType}" == "input" ]] && return 0
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && 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}" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
# Don't use colors in logs # Don't use colors in logs