]> arthur.barton.de Git - ax-unix.git/commitdiff
ax-common: Add ax_debug() and ax_error()
authorAlexander Barton <alex@barton.de>
Sun, 22 Oct 2017 19:17:14 +0000 (21:17 +0200)
committerAlexander Barton <alex@barton.de>
Sun, 22 Oct 2017 19:17:14 +0000 (21:17 +0200)
And update ax_abort() to use ax_error(), including the ability to write
the error message to the system log (new "-l" option).

lib/ax/ax-common.sh

index b50cab6f23081f875ee2df2e9277f95d364d27e7..189d7ca20aa6be39122e5e98b00d32d09a08143a 100644 (file)
@@ -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:" "$*"
+}