]> 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 a5b4efdc57a95456b7cafc17b7c22ad60e8ccf7f..d2b244199a0f6acadccf53a3db87b46bb68e312e 100644 (file)
 # 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 "\033[0;%sm%s\033[0m " "${c}" "${1}"
+       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 "${*}"
@@ -48,11 +54,7 @@ ax_msg() {
 ax_error() {
        if [ "$1" = "-l" ]; then
                shift
-               if [ -n "$NAME" ]; then
-                       logger -t "$NAME" "$*"
-               else
-                       logger "$*"
-               fi
+               logger -t "${NAME:-${0##*/}}" -p err "$*"
        fi
        ax_msg1 2 "$*" >&2
 }
@@ -64,6 +66,7 @@ ax_error() {
 ax_abort() {
        if [ "$1" = "-l" ]; then
                log_param="-l"
+               shift
        else
                unset log_param
        fi