Make sure loh_helpers is sourced only once

This commit is contained in:
Gerhard Hoffmann 2022-06-04 14:58:09 +02:00
parent 27086af023
commit 8a655ae1a2

View File

@ -1,38 +1,35 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
#if [ ${log_helpers_sourced:-1} = "1" ]; then if [ ${log_helpers_sourced:-1} = "1" ]; then # include ony once
# readonly log_helpers_sourced=${BASH_SOURCE[0]} readonly log_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
readonly log_file=/var/log/update_controller.log readonly log_file=/var/log/update_controller.log
if ! [ -f "$log_file" ]; then if ! [ -f "$log_file" ]; then
touch $log_file touch $log_file
fi fi
readonly DEBUG=0 readonly DEBUG=0
readonly INFO=1 readonly INFO=1
readonly WARN=2 readonly WARN=2
readonly CRIT=3 readonly CRIT=3
readonly ERROR=4 readonly ERROR=4
readonly FATAL=5 readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6 readonly MAX_DEBUG_LEVEL=6
log_level=0 log_level=0
set_dbg_level () { set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1 log_level=$1
fi fi
} }
dbg_level () { dbg_level () {
return $log_level return $log_level
} }
log() { log() {
if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
# remove first line # remove first line
sed -e 1d -i $log_file sed -e 1d -i $log_file
@ -40,44 +37,44 @@ log() {
local msg="$(date +'%Y-%m-%d_%T'): $*" local msg="$(date +'%Y-%m-%d_%T'): $*"
printf "log:$msg\n" >&2 printf "log:$msg\n" >&2
printf "$msg\n" >> $log_file printf "$msg\n" >> $log_file
} }
log_debug() { log_debug() {
if [ $log_level = $DEBUG ]; then if [ $log_level = $DEBUG ]; then
log "DEBUG $*" log "DEBUG $*"
fi fi
} }
log_info() { log_info() {
if [ $log_level -le $INFO ]; then if [ $log_level -le $INFO ]; then
log "INFO $*" log "INFO $*"
fi fi
} }
log_warn() { log_warn() {
if [ $log_level -le $WARN ]; then if [ $log_level -le $WARN ]; then
log "WARN $*" log "WARN $*"
fi fi
} }
log_crit() { log_crit() {
if [ $log_level -le $CRIT ]; then if [ $log_level -le $CRIT ]; then
log "CRIT $*" log "CRIT $*"
fi fi
} }
log_error() { log_error() {
if [ $log_level -le $ERROR ]; then if [ $log_level -le $ERROR ]; then
log "ERROR $*" log "ERROR $*"
fi fi
} }
log_fatal() { log_fatal() {
if [ $log_level -le $FATAL ]; then if [ $log_level -le $FATAL ]; then
log "FATAL $*" log "FATAL $*"
log "exiting ..." log "exiting ..."
exit 1 exit 1
fi fi
} }
###############################################################################
# log "test message1" "test message2" fi ### include guard