Implement some new functions

This commit is contained in:
belliash 2011-12-29 23:58:25 +01:00
parent 7fe14bba5a
commit 636e28cf90
1 changed files with 66 additions and 0 deletions

View File

@ -63,6 +63,32 @@ function loadLibraries() {
source ${EZROOT}/libraries/colors &> /dev/null || panic
}
#-------------------------------------------------------------------------------
# Saves specified message into a log file
# Parameters: %message% %level%
#-------------------------------------------------------------------------------
function logMessage() {
if isEnabled ${LOGGING}; then
MESSAGE="${1}"
TYPE="${2}"
DATE=$(date +"${LOGDATEFORMAT}")
if [ -z ${TYPE} ]; then
TYPE=" - "
fi
echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}
fi
}
#-------------------------------------------------------------------------------
# Saves executed command multiline output into a log file
# Parameters: %command_output%
#-------------------------------------------------------------------------------
function logOutput() {
if isEnabled ${LOGGING}; then
echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
fi
}
#-------------------------------------------------------------------------------
# Outputs error message and aborts program execution
#-------------------------------------------------------------------------------
@ -72,6 +98,46 @@ function panic() {
exit 1
}
#-------------------------------------------------------------------------------
# Outputs formatted error message to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printError() {
logMessage "${1}" "ERROR"
echo -e " ${MESSAGE_ERROR} ${@}"
}
#-------------------------------------------------------------------------------
# Outputs formatted information to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printInfo() {
logMessage "${1}" "INFO"
echo -e " ${MESSAGE_INFO} ${@}"
}
#-------------------------------------------------------------------------------
# Outputs formatted warning to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printWarn() {
logMessage "${1}" "WARN"
echo -e " ${MESSAGE_WARN} ${@}"
}
#-------------------------------------------------------------------------------
# Silently executes given command and saves its output to log file if enabled
# Parameters: %command%
#-------------------------------------------------------------------------------
function run() {
COMMAND="${1}"
logMessage "Executing: \"${COMMAND}\"" "DEBUG"
OUTPUT=$(${COMMAND} 2>&1)
RESULT=${?}
logOutput "${OUTPUT}"
return ${RESULT}
}
#-------------------------------------------------------------------------------
# Returns a UNIX timestamp
#-------------------------------------------------------------------------------