From 5454b1ff7cfe1c2e8ebf081f2cdd51fb1e586cf4 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Tue, 10 May 2022 23:52:56 +0200 Subject: [PATCH] sequencer - Log function able to write to stderr --- sequencer.sh | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/sequencer.sh b/sequencer.sh index 41a8799..7bafba3 100755 --- a/sequencer.sh +++ b/sequencer.sh @@ -59,7 +59,7 @@ set -o pipefail col_off= ;[ -t 1 ] && col_off='\033[0m' # No Color } -# Logging +## Logging { LOG_LEVEL="${LOG_LEVEL:-3}" # 4 = debug -> 0 = fatal (stop) LOG_TIME="${LOG_TIME:-}" # 1 = show time stamps @@ -70,6 +70,7 @@ set -o pipefail # [OPTIONS] # -a : append text (no info and timestamp) # Uses color from last sqr::log call without -a + # -e : Output to stderr # -- : End of options # sqr::log () { @@ -77,6 +78,7 @@ set -o pipefail local appendText= local arg= local col_end="${col_off}" + local outp='/dev/stdout' local log_level="${1:-}" shift @@ -89,6 +91,9 @@ set -o pipefail -a) appendText=1 shift ;; + -e) + outp='/dev/stderr' + shift ;; esac done @@ -98,24 +103,25 @@ set -o pipefail local log_line="" while IFS=$'\n' read -r log_line ; do - printf '%b' "${log_color}" + color red + printf '%b' "${log_color}" >${outp} if [[ -n "${LOG_TIME}" ]] ; then if (( appendText )) ; then - printf '%24s' "" + printf '%24s' "" >${outp} else - printf '%s ' "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" + printf '%s ' "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" >${outp} fi fi if (( ! appendText )) ; then - printf " %3s " "[${log_level}]" - printf "%s" "${log_line}" # 1>&2 # send to stderr + printf " %3s " "[${log_level}]" >${outp} + printf "%s" "${log_line}" >${outp} else # +3 : "[] " - printf "%$((${#log_level} + 4))s%s" "" "${log_line}" + printf "%$((${#log_level} + 4))s%s" "" "${log_line}" >${outp} fi - printf '%b\n' "${col_end}" + printf '%b\n' "${col_end}" >${outp} done <<< "${@:-}" sqr::debugContinue } @@ -186,7 +192,7 @@ set -o pipefail } } -# Traps +## Traps { sqr::trap_exit () { exists -f seq_trapExit && seq_trapExit @@ -338,6 +344,7 @@ ask() { answer="${2:-}" fi printf '%s\n' "${answer}" + # return if answer is empty sqr::debugContinue if (( ! empty )) ; then @@ -345,6 +352,7 @@ ask() { fi } +# exe # Handle dry run and verbose output for commands without pipe and/or redirects exe() { dry && printf -- '--' @@ -357,8 +365,8 @@ exe() { fi } -# Handle dry run and verbose output for commands containing pipe and/or redirects # exep +# Handle dry run and verbose output for commands containing pipe and/or redirects exep() { if dry ; then printf -- '--++ : %s\n' "$*" @@ -409,3 +417,4 @@ sqr::main() { } sqr::main "$@" +