From 636e28cf904dad924e91367e11a7c8a7339b5b34 Mon Sep 17 00:00:00 2001 From: belliash Date: Thu, 29 Dec 2011 23:58:25 +0100 Subject: [PATCH] Implement some new functions --- libraries/common | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/libraries/common b/libraries/common index 20f14ab..ad8d4b3 100644 --- a/libraries/common +++ b/libraries/common @@ -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 #-------------------------------------------------------------------------------