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")
|
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
timeAndDate=`date`
|
|
|
|
script_name=`basename -- "$0"`
|
|
|
|
script_name="${script_name%.*}"
|
|
|
|
echo "[$timeAndDate] [DEBUG] > $script_name $FUNCNAME" >> $SCRIPT_LOG
|
|
|
|
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")
|
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
script_name=`basename -- "$0"`
|
|
|
|
script_name="${script_name%.*}"
|
|
|
|
echo "[$timeAndDate] [DEBUG] < $script_name $FUNCNAME" >> $SCRIPT_LOG
|
|
|
|
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")
|
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
local cfn="${FUNCNAME[1]}"
|
|
|
|
timeAndDate=`date`
|
|
|
|
echo "[$timeAndDate] [DEBUG] > $cfn $FUNCNAME" >> $SCRIPT_LOG
|
|
|
|
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")
|
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
|
|
local cfn="${FUNCNAME[1]}"
|
|
|
|
timeAndDate=`date`
|
|
|
|
echo "[$timeAndDate] [DEBUG] < $cfn $FUNCNAME" >> $SCRIPT_LOG
|
|
|
|
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"
|
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=()
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 01:06:27 +02:00
|
|
|
echo $opts $msg >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 00:10:48 +02:00
|
|
|
echo "[$timeAndDate] [ERROR] $msg" >> $SCRIPT_LOG
|
|
|
|
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"
|
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" )
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 01:06:27 +02:00
|
|
|
echo $opts $msg >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 00:10:48 +02:00
|
|
|
echo "[$timeAndDate] [WARN] $msg" >> $SCRIPT_LOG
|
|
|
|
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"
|
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" )
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 01:06:27 +02:00
|
|
|
echo $opts $msg >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 00:10:48 +02:00
|
|
|
echo "[$timeAndDate] [INFO] $msg" >> $SCRIPT_LOG
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function logdebug(){
|
|
|
|
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"
|
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO")
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 01:06:27 +02:00
|
|
|
echo $opts $msg >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 00:10:48 +02:00
|
|
|
echo "[$timeAndDate] [DEBUG] $msg" >> $SCRIPT_LOG
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logtrace(){
|
|
|
|
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"
|
|
|
|
timeAndDate=`date`
|
|
|
|
local loglevels
|
|
|
|
declare -a loglevels
|
|
|
|
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 01:06:27 +02:00
|
|
|
echo $opts $msg >&2
|
2020-09-22 00:10:48 +02:00
|
|
|
fi
|
2020-09-22 09:08:16 +02:00
|
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
2020-09-22 00:10:48 +02:00
|
|
|
echo "[$timeAndDate] [TRACE] $msg" >> $SCRIPT_LOG
|
|
|
|
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
|