#!/bin/bash # set -x RED='\e[0;31m' GREEN='\e[0;32m' NC='\e[0m' # No Color if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once readonly log_helpers_sourced=${BASH_SOURCE[0]} readonly log_file=/var/log/update_controller.log if ! [ -f "$log_file" ]; then touch $log_file fi readonly DEBUG=0 readonly INFO=1 readonly WARN=2 readonly CRIT=3 readonly ERROR=4 readonly FATAL=5 readonly MAX_DEBUG_LEVEL=6 log_level=0 set_dbg_level () { if [ $1 < $MAX_DEBUG_LEVEL ]; then log_level=$1 fi } dbg_level () { return $log_level } log() { if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then # remove first line sed -e 1d -i $log_file fi local msg="$(date +'%Y-%m-%d_%T'): $*" echo "log:$msg" >&2 echo "$msg" >> $log_file } log_debug() { if [ $log_level = $DEBUG ]; then log "DEBUG $*" fi } log_info() { if [ $log_level -le $INFO ]; then log "${GREEN}INFO $*" fi } log_warn() { if [ $log_level -le $WARN ]; then log "${RED}WARN $*" fi } log_crit() { if [ $log_level -le $CRIT ]; then log "${RED}CRIT $*" fi } log_error() { if [ $log_level -le $ERROR ]; then log "${RED}ERROR $*" fi } log_fatal() { if [ $log_level -le $FATAL ]; then log "${RED}FATAL $*" log "${RED}exiting ..." exit 1 fi } ############################################################################### fi ### include guard