Skip to content

Commit

Permalink
Remove LOG_FILE arg in favor of LOG_FILE env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed May 1, 2017
1 parent b3ba667 commit 38091f2
Showing 1 changed file with 62 additions and 34 deletions.
96 changes: 62 additions & 34 deletions lib/core_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,71 +174,99 @@ make_temp_file () {
# Prints a message to a log file.
#
# Usage:
# $ print_log_message [-DWIE] LOG_MESSAGE LOG_FILE
# $ print_log_message [-TDIWEF] LOG_MESSAGE
#
# Computes a timestamp and prepends the timestamp to the message
# before appending the message to the log file.
# Computes and prepends a timestamp to the LOG_MESSAGE
# before appending the message to a log file.
#
# This script is compatible with Mac OS and GNU/Linux.
#######################################################################

print_log_message () {

# local vars
local prefix
# determine the log file
if [ -z "$LOG_FILE" ]; then
echo "FATAL: $FUNCNAME requires env var LOG_FILE" >&2
exit 2
fi
if [ ! -f "$LOG_FILE" ]; then
echo "FATAL: $FUNCNAME: file does not exist: $LOG_FILE" >&2
exit 2
fi

local error_message
local log_message
local log_file
local tstamp

# compute timestamp
tstamp=$( /bin/date -u +%Y-%m-%dT%TZ )
if [ $? -ne 0 ]; then
tstamp=0000-00-00T00:00:00Z
error_message="$FUNCNAME: unable to compute timestamp"
printf "%s %s %s\n" "$tstamp" ERROR "$error_message" >> "$LOG_FILE"
fi

# default log level
local prefix=INFO
local log_level=3

# process command-line options and arguments
local opt
local OPTARG
local OPTIND
while getopts ":DWIE" opt; do
while getopts ":TDIWEF" opt; do
case $opt in
D)
prefix=DEBUG
T)
prefix=TRACE; log_level=5
;;
W)
prefix=WARN
D)
prefix=DEBUG; log_level=4
;;
I)
prefix=INFO
prefix=INFO; log_level=3
;;
W)
prefix=WARN; log_level=2
;;
E)
prefix=ERROR
prefix=ERROR; log_level=1
;;
F)
prefix=FATAL; log_level=0
;;
\?)
echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2
return 2
error_message="$FUNCNAME: Unrecognized option: -$OPTARG"
printf "%s %s %s\n" "$tstamp" ERROR "$error_message" >> "$LOG_FILE"
;;
:)
echo "ERROR: $FUNCNAME: Option -$OPTARG requires an argument" >&2
return 2
error_message="$FUNCNAME: Option -$OPTARG requires an argument"
printf "%s %s %s\n" "$tstamp" ERROR "$error_message" >> "$LOG_FILE"
;;
esac
done

# default to INFO
if [ -z "$prefix" ]; then
prefix=INFO
fi

# make sure there are two command-line arguments
# make sure there is one command-line argument
shift $(( OPTIND - 1 ))
if [ $# -ne 2 ]; then
echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (2 required)" >&2
return 2
if [ $# -lt 1 ]; then
error_message="$FUNCNAME: no arguments (1 required)"
printf "%s %s %s\n" "$tstamp" ERROR "$error_message" >> "$LOG_FILE"
log_message=NULL
elif [ $# -gt 1 ]; then
error_message="$FUNCNAME: too many arguments: $# (1 required)"
printf "%s %s %s\n" "$tstamp" ERROR "$error_message" >> "$LOG_FILE"
log_message="$1"
else
log_message="$1"
fi

# get the log message
log_message="$1"
log_file="$2"

# compute timestamp
tstamp=$( /bin/date -u +%Y-%m-%dT%TZ )
# if insufficient log level, short-circuit the log operation
if [ -z "$LOG_LEVEL" ]; then
if [ "$log_level" -gt 3 ]; then return 0; fi
else
if [ "$log_level" -gt "$LOG_LEVEL" ]; then return 0; fi
fi

# prepend timestamp to the log message
printf "%s %s %s\n" "$tstamp" "$prefix" "$log_message" >> "$log_file"
printf "%s %s %s\n" "$tstamp" "$prefix" "$log_message" >> "$LOG_FILE"
}

#######################################################################
Expand Down

0 comments on commit 38091f2

Please sign in to comment.