diff --git a/lib/core_lib.sh b/lib/core_lib.sh index 2c18d6c..3c2dcc7 100755 --- a/lib/core_lib.sh +++ b/lib/core_lib.sh @@ -174,7 +174,7 @@ make_temp_file () { # Prints a message to a log file. # # Usage: -# $ print_log_message LOG_MESSAGE LOG_FILE +# $ print_log_message [-DWIE] LOG_MESSAGE LOG_FILE # # Computes a timestamp and prepends the timestamp to the message # before appending the message to the log file. @@ -185,11 +185,46 @@ make_temp_file () { print_log_message () { # local vars + local prefix local log_message local log_file local tstamp + + local opt + local OPTARG + local OPTIND + while getopts ":DWIE" opt; do + case $opt in + D) + prefix=DEBUG + ;; + W) + prefix=WARN + ;; + I) + prefix=INFO + ;; + E) + prefix=ERROR + ;; + \?) + echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 + return 2 + ;; + :) + echo "ERROR: $FUNCNAME: Option -$OPTARG requires an argument" >&2 + return 2 + ;; + esac + done + + # default to INFO + if [ -z "$prefix" ]; then + prefix=INFO + fi # make sure there are two command-line arguments + shift $(( OPTIND - 1 )) if [ $# -ne 2 ]; then echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (2 required)" >&2 return 2 @@ -203,7 +238,7 @@ print_log_message () { tstamp=$( /bin/date -u +%Y-%m-%dT%TZ ) # prepend timestamp to the log message - printf "%s %s\n" "$tstamp" "$log_message" >> "$log_file" + printf "%s %s %s\n" "$tstamp" "$prefix" "$log_message" >> "$log_file" } ####################################################################### diff --git a/lib/http_tools.sh b/lib/http_tools.sh index a8fdcb6..c4f08fe 100755 --- a/lib/http_tools.sh +++ b/lib/http_tools.sh @@ -247,7 +247,7 @@ conditional_get () { # quiet failure mode if $cache_only_mode; then - print_log_message "$FUNCNAME: ERROR: resource not cached: $location" "$log_file" + print_log_message -E "$FUNCNAME: resource not cached: $location" "$log_file" return 1 fi @@ -403,7 +403,7 @@ conditional_get () { elif [ "$response_code" = "304" ]; then # quiet failure mode if $force_output_mode; then - print_log_message "$FUNCNAME: ERROR: resource not modified: $location" "$log_file" + print_log_message -E "$FUNCNAME: resource not modified: $location" "$log_file" return 1 fi