refcodes-batch.1.0.2.source-code.lib-logging.inc Maven / Gradle / Ivy
#!/bin/bash
# ------------------------------------------------------------------------------
# This lib relies on the "apply.sh" / "lib-common.inc" shell script.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Log levels for logging:
# ------------------------------------------------------------------------------
DEBUG=0
INFO=1
WARN=2
ERROR=3
FATAL=4
# ANSI colors:
RED="\E[31m"
GREEN="\E[32m"
YELLOW="\E[33m"
BLUE="\E[34m"
MAGENTA="\E[35m"
CYAN="\E[36m"
WHITE="\E[37m"
BOLD_ON="\E[1m"
BOLD_OFF="\E[22m"
CLEAR_FORMAT="\E[0m"
# ------------------------------------------------------------------------------
# Sets the color to use when prining text to the terminal.
#
# $1: The color to use
# ------------------------------------------------------------------------------
function setForegroundColor {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"setColor\" expects one argument !!!"
exit $EXIT_BUG
fi
color="$1"
if [[ "$color" == "red" ]] ; then
echo -en "$RED"
elif [[ "$color" == "green" ]] ; then
echo -en "$GREEN"
elif [[ "$color" == "yellow" ]] ; then
echo -en "$YELLOW"
elif [[ "$color" == "blue" ]] ; then
echo -en "$BLUE"
elif [[ "$color" == "magenta" ]] ; then
echo -en "$MAGENTA"
elif [[ "$color" == "cyan" ]] ; then
echo -en "$CYAN"
elif [[ "$color" == "white" ]] ; then
echo -en "$WHITE"
else
showError "Wrong argument: The function \"setForegroundColor\" expects a known color as argument !!!"
fi
}
# ------------------------------------------------------------------------------
# Sets the color to use when prining text to the terminal.
#
# $1: '0' = bold is off, '1' = bold is on
# ------------------------------------------------------------------------------
function setBold {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"setBold\" expects one argument !!!"
exit $EXIT_BUG
fi
bold="$1"
if [[ "$bold" == "1" ]] ; then
echo -en "$BOLD_ON"
elif [[ "$bold" == "0" ]] ; then
echo -en "$BOLD_OFF"
else
showError "Wrong argument: The function \"setBold\" expects '0' ( = off ) of '1' ( = on ) argument !!!"
fi
}
# ------------------------------------------------------------------------------
# Resets the color to use to the default terminal color.
# ------------------------------------------------------------------------------
function resetFormat {
if [[ $# != 0 ]] ; then
showError "Too many arguments: The function \"resetFormat\" expects none arguments !!!"
exit $EXIT_BUG
fi
tput sgr0
}
# ------------------------------------------------------------------------------
# Logs out a plain line feed.
# ------------------------------------------------------------------------------
function logLineFeed {
if [[ $# != 0 ]] ; then
showError "Too many arguments: The function \"logLineFeed\" expects none arguments !!!"
exit $EXIT_BUG
fi
echo "" >&2
}
# ------------------------------------------------------------------------------
# Logs a line with the given char and the width of the current terminal (if no
# explicit width was specified) or 80 if none with was specified.
#
# $1: The char to use
# $2: The width to use (optional)
# ------------------------------------------------------------------------------
function logLine {
if [[ $# > 2 ]] ; then
showError "Too many arguments: The function \"logLine\" expects at most two arguments !!!"
exit $EXIT_BUG
fi
char="$1"
width="$2"
if [[ $width == '' ]] ; then
width="$(tput cols)"
if [[ $? != 0 ]] ; then
width="80"
fi
if [[ $LOG_LINE_WIDTH > '0' ]] ; then
width="$LOG_LINE_WIDTH"
fi
fi
for (( i=0 ; i < $width ; i++ )); do echo -n "$char" >&2 ; done
echo "" >&2
}
# ------------------------------------------------------------------------------
# Logs a separator line
# ------------------------------------------------------------------------------
function logSeparator {
if [[ $# != 0 ]] ; then
showError "Too many arguments: The function \"logSeparator\" expects none arguments !!!"
exit $EXIT_BUG
fi
setForegroundColor "white"
logLine "-"
resetFormat
}
# ------------------------------------------------------------------------------
# Logs a title with a bold separator on top and on bottom:
#
# $1: The title text
# ------------------------------------------------------------------------------
function logTitle {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"logTitle\" expects one argument !!!"
exit $EXIT_BUG
fi
logLineFeed
text="$1"
setBold "1"
setForegroundColor "magenta"
logLine "#"
echo -n "# " >&2
setForegroundColor "white"
echo "$text" >&2
setForegroundColor "magenta"
logLine "#"
resetFormat
}
# ------------------------------------------------------------------------------
# Logs a title with a bold separator on top and on bottom:
#
# $1: The title text
# ------------------------------------------------------------------------------
function logHeadline {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"logSubTitle\" expects one argument !!!"
exit $EXIT_BUG
fi
logLineFeed
text="$1"
setBold "1"
setForegroundColor "yellow"
logLine "="
echo -n "= " >&2
setForegroundColor "white"
echo "$text" >&2
setForegroundColor "yellow"
logLine "="
resetFormat
}
# ------------------------------------------------------------------------------
# Sets the log level for the logs to be logged. The log level may be $DEBUG,
# $INFO, $WARN, $FATAL.
#
# $1: The log level.
# ------------------------------------------------------------------------------
function setLogLevel {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"setLogLevel\" expects one argument !!!"
exit $EXIT_BUG
fi
LOG_LEVEL=$1
}
# ------------------------------------------------------------------------------
# Sets the log file for the logs to be logged
#
# $1: The log file where to log to.
# ------------------------------------------------------------------------------
function setLogFile {
if [[ $# != 1 ]] ; then
showError "Wrong number of arguments: The function \"setLogFile\" expects one argument !!!"
exit $EXIT_BUG
LOG_FILE=$1
fi
}
# ------------------------------------------------------------------------------
# Log a message: First argument is the loglevel in case two arguments were
# passed,then the second is the message. In case of one argument, then the first
# is the message which is logged as logLevel $INFO.
#
# $1: The message (one argument), logLevel (two arguments)
# $2: The message (in case of two arguments)
# ------------------------------------------------------------------------------
function log {
if [[ $# -eq 1 ]] ; then
logLevel="$INFO"
message="$1"
elif [[ $# -eq 2 ]] ; then
logLevel="$1"
message="$2"
else
showError "Wrong number of arguments: The function \"log\" expects at least one and at most two arguments !!!"
exit $EXIT_BUG
fi
if [[ "$logLevel" != "$DEBUG" && "$logLevel" != "$INFO" && "$logLevel" != "$WARN" && "$logLevel" != "$ERROR" && "$logLevel" != "$FATAL" ]] ; then
showError "Wrong usage: The function \"log\" expects valid \"logLevel\" argument (you provided \"$logLevel\" !!!"
exit $EXIT_BUG
fi
if [[ "$logLevel" -ge "$LOG_LEVEL" ]] ; then
if [[ "$logLevel" == "$DEBUG" ]] ; then
logLevelText="DEBUG"
logLevelAnsiText="${YELLOW}DEBUG${CLEAR_FORMAT}"
elif [[ "$logLevel" == "$INFO" ]] ; then
logLevelText="INFO"
logLevelAnsiText="${WHITE}INFO${CLEAR_FORMAT}"
elif [[ "$logLevel" == "$WARN" ]] ; then
logLevelText="WARN"
logLevelAnsiText="${CYAN}WARN${CLEAR_FORMAT}"
elif [[ "$logLevel" == "$ERROR" ]] ; then
logLevelText="ERROR"
logLevelAnsiText="${RED}${BOLD_ON}ERROR${CLEAR_FORMAT}"
elif [[ "$logLevel" == "$FATAL" ]] ; then
logLevelText="FATAL"
logLevelAnsiText="${MAGENTA}${BOLD_ON}FATAL${CLEAR_FORMAT}"
fi
if [[ "$VERBOSE" = "y" || "${LOG_FILE}" == "" ]] ; then
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\t$logLevelAnsiText\t$message"
fi
if [[ "${LOG_FILE}" != "" ]] ; then
# rotate LOG_FILE if required, ignore any errors
local MODDATE=$( date -r ${LOG_FILE} +%F 2>/dev/null )
local TODAY=$( date +%F 2>/dev/null )
local ROTATED_LOG_FILE=$( dirname ${LOG_FILE} 2>/dev/null )/$( basename ${LOG_FILE} 2>/dev/null ).${MODDATE}
if [ "${MODDATE}" != "${TODAY}" ] && [ ! -e "${RENAMED_LOG_FILE}" ] && [ ! -e "${RENAMED_LOG_FILE}.gz" ]; then
mv -f "${LOG_FILE}" "${ROTATED_LOG_FILE}" 2>/dev/null
if [ "$?"="0" ] ; then
gzip -f "${ROTATED_LOG_FILE}" 2>/dev/null
fi
fi
# append message to logfile
touch "${LOG_FILE}" 2>/dev/null
chmod a+rw "{LOG_FILE}" 2>/dev/null
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\t$logLevelText\t$message" 1>>${LOG_FILE} 2>/dev/null ;
if [ $? -ne 0 ] ; then
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\t$logLevelText\t$message" 2>&1
fi
fi
fi
}
# ------------------------------------------------------------------------------
# Show error:
# ------------------------------------------------------------------------------
function showError {
if [ -z "$1" ] ; then
echo "Argument missing: The function \"showError\" expects an argument !!!" >&2
logSeparator
exit $EXIT_BUG
fi
setForegroundColor "red"
echo "ERROR >>> $1" >&2
logSeparator
}
# ------------------------------------------------------------------------------
# Show warning:
# ------------------------------------------------------------------------------
function showWarning {
if [ -z "$1" ] ; then
echo "Argument missing: The function \"showWarning\" expects an argument !!!" >&2
logSeparator
exit $EXIT_BUG
fi
setForegroundColor "cyan"
echo "WARNING >>> $1" >&2
logSeparator
}
# ------------------------------------------------------------------------------
# Show success:
# ------------------------------------------------------------------------------
function showSuccess {
if [ -z "$1" ] ; then
echo "Argument missing: The function \"showSuccess\" expects an argument !!!" >&2
logSeparator
exit $EXIT_BUG
fi
setForegroundColor "green"
echo "SUCCESS >>> $1" >&2
logSeparator
}