# Function: Setup logfile and redirect stdout/stderr. log_setup() { # Check if logfile exists and is writable. ( [ -e "$logfile" ] || touch "$logfile" ) && [ ! -w "$logfile" ] && echo "ERROR: Cannot write to $logfile. Check permissions or sudo access." && exit 1 tmplog=$(tail -n $logfile_max_lines $logfile 2>/dev/null) && echo "${tmplog}" > $logfile exec > >(tee -a $logfile) exec 2>&1 } # Function: Log an event. log() { echo "[$(date +"%Y-%m-%d"+"%T")]: $*" } # check json file by jq function check_json(){JSON_FILE=$1if [ ! -f $JSON_FILE ]; then printf "${JSON_FILE} is not exits. Please check. \n" >> $OUTPUT_FILE cat $OUTPUT_FILE >> $ARCHIVE_LOG exit 2elif ! jq . $JSON_FILE >/dev/null 2>&1; then printf "jq cannot parse ${JSON_FILE}, Please check. \n" >> $OUTPUT_FILE exit 2fi}# Function: Log an event.log() { echo "[$(date +"%Y-%m-%d"+"%T")]: $*"}