]> arthur.barton.de Git - ax-unix.git/blobdiff - lib/ax/ax-common.sh
Enhance ax_error(): Set "err" level for syslog, detect $NAME
[ax-unix.git] / lib / ax / ax-common.sh
index f060e4c9460fd95030b923acafc898610c778b8a..d2b244199a0f6acadccf53a3db87b46bb68e312e 100644 (file)
@@ -1,6 +1,7 @@
+#!/bin/sh
 #
 # ax-common.sh -- Common Functions for Shell Scripts
-# Copyright (c)2013-2015 Alexander Barton (alex@barton.de)
+# Copyright (c)2013-2017 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -8,24 +9,31 @@
 # (at your option) any later version.
 #
 
-ax_common_sourced=1
+# "ax_common_sourced" is a flag and stores the "API version" of "ax-common.sh",
+# too. It should be incremented each time the public API changes!
+# shellcheck disable=SC2034
+ax_common_sourced=2
 
-# Display a colored message.
+# Display a colored message (a plain message, when not writing to a terminal).
 #  $1    Level: -=title, 0=ok, 1=warning, 2=error.
 #  $2    Word(s) to highlight.
 #  $3-n  Remaining word(s). [optional]
 ax_msg1() {
-       case "$1" in
-               "0")    c="32"; shift; ;;       # green
-               "1")    c="33"; shift; ;;       # yellow
-               "2")    c="31"; shift; ;;       # red
-               "-")    c="1";  shift; ;;       # bold
-               *)      c="0";
-       esac
-       # print colored word(s):
-       printf "\x1b[0;${c}m"
-       /bin/echo -n "${1}"
-       printf "\x1b[0m "
+       if [ -t 1 ]; then
+               # writing to a terminal ...
+               case "$1" in
+                       "0")    c="32"; shift; ;;       # green
+                       "1")    c="33"; shift; ;;       # yellow
+                       "2")    c="31"; shift; ;;       # red
+                       "-")    c="1";  shift; ;;       # bold
+                       *)      c="0";
+               esac
+               # print colored word(s):
+               printf "\\033[0;%sm%s\\033[0m " "${c}" "${1}"
+       else
+               # print plain text:
+               printf "%s " "${1}"
+       fi
        shift
        # print remaining word(s) and trailing newline:
        echo "${*}"
@@ -40,12 +48,38 @@ 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
+               logger -t "${NAME:-${0##*/}}" -p err "$*"
+       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"
+               shift
+       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:" "$*"
+}