2020-09-22 00:10:48 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#### LOGGING
|
|
|
|
## from https://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
|
|
|
|
[ -z "${SCRIPT_LOG+x}" ] && export SCRIPT_LOG=${HOME}/logs/scriptlog
|
|
|
|
mkdir -p "$(dirname ${SCRIPT_LOG})"
|
|
|
|
touch $SCRIPT_LOG
|
|
|
|
|
|
|
|
|
|
|
|
function SCRIPTENTRY(){
|
2020-09-22 09:08:16 +02:00
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 09:08:16 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
script_name=`basename -- "$0"`
|
|
|
|
script_name="${script_name%.*}"
|
2022-08-11 12:11:01 +02:00
|
|
|
echo "[$timeAndDate] [SCRIPTENTRY] > $script_name $FUNCNAME" >> $SCRIPT_LOG
|
2020-09-22 09:08:16 +02:00
|
|
|
fi
|
2020-09-22 00:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function SCRIPTEXIT(){
|
2020-09-22 09:08:16 +02:00
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 09:08:16 +02:00
|
|
|
script_name=`basename -- "$0"`
|
|
|
|
script_name="${script_name%.*}"
|
2022-08-11 12:11:01 +02:00
|
|
|
echo "[$timeAndDate] [SCRIPTEXIT] < $script_name $FUNCNAME" >> $SCRIPT_LOG
|
2020-09-22 09:08:16 +02:00
|
|
|
fi
|
2020-09-22 00:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ENTRY(){
|
2020-09-22 09:08:16 +02:00
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 09:08:16 +02:00
|
|
|
local cfn="${FUNCNAME[1]}"
|
|
|
|
timeAndDate=`date`
|
2022-08-11 12:11:01 +02:00
|
|
|
echo "[$timeAndDate] [ENTRY] > $cfn $FUNCNAME" >> $SCRIPT_LOG
|
2020-09-22 09:08:16 +02:00
|
|
|
fi
|
2020-09-22 00:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function EXIT(){
|
2020-09-22 09:08:16 +02:00
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 09:08:16 +02:00
|
|
|
local cfn="${FUNCNAME[1]}"
|
|
|
|
timeAndDate=`date`
|
2022-08-11 12:11:01 +02:00
|
|
|
echo "[$timeAndDate] [EXIT] < $cfn $FUNCNAME" >> $SCRIPT_LOG
|
2020-09-22 09:08:16 +02:00
|
|
|
fi
|
2020-09-22 00:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function logerror(){
|
|
|
|
local function_name="${FUNCNAME[1]}"
|
2020-09-22 01:06:27 +02:00
|
|
|
case $1 in
|
|
|
|
-n)
|
|
|
|
local opts="-n"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-09-22 00:10:48 +02:00
|
|
|
local msg="$1"
|
2021-12-03 09:00:40 +01:00
|
|
|
[ -z "${msg:+x}" ] && return 0
|
2020-09-22 00:10:48 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=()
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo $opts "[ERROR] (${FUNCNAME[1]}) $msg" >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo "[$timeAndDate] [ERROR] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logwarn(){
|
|
|
|
local function_name="${FUNCNAME[1]}"
|
2020-09-22 01:06:27 +02:00
|
|
|
case $1 in
|
|
|
|
-n)
|
|
|
|
local opts="-n"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-09-22 00:10:48 +02:00
|
|
|
local msg="$1"
|
2021-12-03 09:00:40 +01:00
|
|
|
[ -z "${msg:+x}" ] && return 0
|
2020-09-22 00:10:48 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" )
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo $opts "[WARN] (${FUNCNAME[1]}) $msg" >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo "[$timeAndDate] [WARN] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function loginfo(){
|
|
|
|
local function_name="${FUNCNAME[1]}"
|
2020-09-22 01:06:27 +02:00
|
|
|
case $1 in
|
|
|
|
-n)
|
|
|
|
local opts="-n"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-09-22 00:10:48 +02:00
|
|
|
local msg="$1"
|
2021-12-03 09:00:40 +01:00
|
|
|
[ -z "${msg:+x}" ] && return 0
|
2020-09-22 00:10:48 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" )
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo $opts "[INFO] (${FUNCNAME[1]}) $msg" >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo "[$timeAndDate] [INFO] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function logdebug(){
|
2022-08-11 12:11:01 +02:00
|
|
|
local function_name="${FUNCNAME[1]}"
|
2020-09-22 01:06:27 +02:00
|
|
|
case $1 in
|
|
|
|
-n)
|
|
|
|
local opts="-n"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-09-22 00:10:48 +02:00
|
|
|
local msg="$1"
|
2021-12-03 09:00:40 +01:00
|
|
|
[ -z "${msg:+x}" ] && return 0
|
2020-09-22 00:10:48 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo $opts "[DEBUG] (${FUNCNAME[1]}) $msg" >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo "[$timeAndDate] [DEBUG] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logtrace(){
|
2022-08-11 12:11:01 +02:00
|
|
|
local function_name="${FUNCNAME[1]}"
|
2020-09-22 01:06:27 +02:00
|
|
|
case $1 in
|
|
|
|
-n)
|
|
|
|
local opts="-n"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
2020-09-22 00:10:48 +02:00
|
|
|
local msg="$1"
|
2021-12-03 09:00:40 +01:00
|
|
|
[ -z "${msg:+x}" ] && return 0
|
2020-09-22 00:10:48 +02:00
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo $opts "[TRACE] (${FUNCNAME[1]}) $msg" >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2022-08-11 12:11:01 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
|
|
echo "[$timeAndDate] [TRACE] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
|
|
|
}
|
2020-09-22 00:29:18 +02:00
|
|
|
|
|
|
|
export -f logerror
|
|
|
|
export -f logwarn
|
|
|
|
export -f loginfo
|
|
|
|
export -f logdebug
|
|
|
|
export -f logtrace
|
|
|
|
export -f SCRIPTENTRY
|
|
|
|
export -f ENTRY
|
|
|
|
export -f SCRIPTEXIT
|
|
|
|
export -f EXIT
|