100 lines
3.2 KiB
Bash
100 lines
3.2 KiB
Bash
##########################
|
|
# An associative array containing varnames that the user want's to keep track of in log output (specific to each function log is being called from)
|
|
#
|
|
# [$FUNCNAME]="string of varnames"
|
|
#
|
|
# It will keep track of vars seperately of each function that log will be used with
|
|
declare -A LOG_WATCH_VARS
|
|
|
|
|
|
######################
|
|
# Function: log_import
|
|
# Usage: This is used to import logging functions in an embedded shell or over ssh
|
|
log_import () {
|
|
declare -f log_watch_vars log_unwatch_vars log
|
|
declare -p LOG_WATCH_VARS
|
|
}
|
|
|
|
#####################
|
|
# Function: log_watch_vars
|
|
# Usage: Provide a list of variable names to add to the watchlist for the calling function
|
|
log_watch_vars () {
|
|
local -a REQUESTED_WATCH_VARS=( $* )
|
|
local CALLING_FUNCTION=${FUNCNAME[1]}
|
|
local FUNC_VARS=${LOG_WATCH_VARS[$CALLING_FUNCTION]}
|
|
local MATCH_REGEX=${FUNC_VARS//[[:space:]]/|}
|
|
|
|
for VARNAME in ${REQUESTED_WATCH_VARS[@]}; do
|
|
if [[ ! "$VARNAME" =~ ^$MATCH_REGEX$ ]]; then
|
|
FUNC_VARS+=" $VARNAME"
|
|
fi
|
|
done
|
|
|
|
LOG_WATCH_VARS[$CALLING_FUNCTION]=$FUNC_VARS
|
|
}
|
|
|
|
#####################
|
|
# Function: log_unwatch_vars
|
|
# Usage: Provide a list of variable names to remove from the watchlist
|
|
log_unwatch_vars () {
|
|
## Serialize the variable names provided by user into REGEX filter "(var1|var2|var3|var4|var_n)"
|
|
local MATCH_REGEX="(${@// /|})"
|
|
local CALLING_FUNCTION=${FUNCNAME[1]}
|
|
local FUNC_VARS=${LOG_WATCH_VARS[$CALLING_FUNCTION]}
|
|
local -a TEMP_ARRAY
|
|
|
|
for VARNAME in $FUNC_VARS; do
|
|
if [[ ! "$VARNAME" =~ ^$MATCH_REGEX$ ]]; then
|
|
TEMP_ARRAY+=( $VARNAME )
|
|
fi
|
|
done
|
|
LOG_WATCH_VARS[$CALLING_FUNCTION]="${TEMP_ARRAY[@]}"
|
|
}
|
|
|
|
###########
|
|
# Function: log
|
|
# Usage: Use to print out debug statements for the developer (or user) to display a log output
|
|
# including variable values & names
|
|
log () {
|
|
local MESSAGE=${1:?"$FUNCNAME: No message provided"}
|
|
local CALLING_FUNCTION=${FUNCNAME[1]}
|
|
local FUNC_VARS=( ${LOG_WATCH_VARS[$CALLING_FUNCTION]} )
|
|
local LEVEL=${2:-0}
|
|
local DATE=$(date)
|
|
local VAR_WATCH_STRING="" OUTPUT_MESSAGE=""
|
|
|
|
if [[ ${CONFIG[debug]} -eq 1 ]]; then
|
|
case ${LEVEL,,} in
|
|
0 | i | info) LEVEL="INFO";;
|
|
1 | w | warn) LEVEL="WARN";;
|
|
2 | c | crit) LEVEL="CRIT";;
|
|
3 | f | fail) LEVEL="FAIL";;
|
|
*) LEVEL="UNDEF";;
|
|
esac
|
|
|
|
local VARNAME VALUE
|
|
if [[ ${#FUNC_VARS[@]} -gt 0 ]]; then
|
|
for VARNAME in ${FUNC_VARS[@]}; do
|
|
local -n VARVALUE=$VARNAME
|
|
if [[ $VARVALUE == "" ]]; then
|
|
VALUE='N/A'
|
|
else
|
|
VALUE=$VARVALUE
|
|
fi
|
|
if [[ $VAR_WATCH_STRING == "" ]]; then
|
|
VAR_WATCH_STRING+="$VARNAME=$VALUE "
|
|
else
|
|
VAR_WATCH_STRING+=": $VARNAME=$VALUE "
|
|
fi
|
|
done
|
|
|
|
OUTPUT_MESSAGE="$DATE : $HOSTNAME : $LEVEL : $VAR_WATCH_STRING : $MESSAGE"
|
|
else
|
|
OUTPUT_MESSAGE="$DATE : $HOSTNAME : $LEVEL : $MESSAGE"
|
|
fi
|
|
|
|
echo -e "$OUTPUT_MESSAGE"
|
|
fi
|
|
} >&2
|
|
|