From: Alexander Barton Date: Sun, 22 Oct 2017 19:17:14 +0000 (+0200) Subject: ax-common: Add ax_debug() and ax_error() X-Git-Url: https://arthur.barton.de/gitweb/?p=ax-unix.git;a=commitdiff_plain;h=ac5614288300a467f2cad6d0a67153c00b8a7505 ax-common: Add ax_debug() and ax_error() And update ax_abort() to use ax_error(), including the ability to write the error message to the system log (new "-l" option). --- diff --git a/lib/ax/ax-common.sh b/lib/ax/ax-common.sh index b50cab6..189d7ca 100644 --- a/lib/ax/ax-common.sh +++ b/lib/ax/ax-common.sh @@ -40,12 +40,41 @@ ax_msg() { ax_msg1 "$level" "$*" } +# Display an error message to stderr. +# [-l] Log message to syslog, too. +# $1-n Error message. +ax_error() { + if [ "$1" = "-l" ]; then + shift + if [ -n "$NAME" ]; then + logger -t "$NAME" "$*" + else + logger "$*" + fi + fi + ax_msg1 2 "$*" >&2 +} + # Abort the script with an error message and exit code 1. -# $1 Error message [optional]. Will be formatted as "Error: %s Aborting!". -# if no error message is given, "Aborting!" will be printed. +# [-l] Log message to syslog, too. +# $1 Error message [optional]. Will be formatted as "Error: %s Aborting!". +# if no error message is given, "Aborting!" will be printed. ax_abort() { - [ $# -gt 0 ] \ - && ax_msg 2 "Error: $* Aborting!" \ - || ax_msg 2 "Aborting!" + if [ "$1" = "-l" ]; then + log_param="-l" + else + unset log_param + fi + if [ $# -gt 0 ]; then + ax_error $log_param "Error: $* Aborting!" + else + ax_error $log_param "Aborting!" + fi exit 1 } + +# Display a debug message, when debug mode is enabled, that is, the environment +# variable "DEBUG" is set. +ax_debug() { + [ -n "$DEBUG" ] && ax_msg1 1 "DEBUG:" "$*" +}