sequencer - Log function able to write to stderr

This commit is contained in:
2022-05-10 23:52:56 +02:00
parent a0f596eaa2
commit 5454b1ff7c

View File

@@ -59,7 +59,7 @@ set -o pipefail
col_off= ;[ -t 1 ] && col_off='\033[0m' # No Color col_off= ;[ -t 1 ] && col_off='\033[0m' # No Color
} }
# Logging ## Logging
{ {
LOG_LEVEL="${LOG_LEVEL:-3}" # 4 = debug -> 0 = fatal (stop) LOG_LEVEL="${LOG_LEVEL:-3}" # 4 = debug -> 0 = fatal (stop)
LOG_TIME="${LOG_TIME:-}" # 1 = show time stamps LOG_TIME="${LOG_TIME:-}" # 1 = show time stamps
@@ -70,6 +70,7 @@ set -o pipefail
# [OPTIONS] # [OPTIONS]
# -a : append text (no info and timestamp) # -a : append text (no info and timestamp)
# Uses color from last sqr::log call without -a # Uses color from last sqr::log call without -a
# -e : Output to stderr
# -- : End of options # -- : End of options
# #
sqr::log () { sqr::log () {
@@ -77,6 +78,7 @@ set -o pipefail
local appendText= local appendText=
local arg= local arg=
local col_end="${col_off}" local col_end="${col_off}"
local outp='/dev/stdout'
local log_level="${1:-}" local log_level="${1:-}"
shift shift
@@ -89,6 +91,9 @@ set -o pipefail
-a) -a)
appendText=1 appendText=1
shift ;; shift ;;
-e)
outp='/dev/stderr'
shift ;;
esac esac
done done
@@ -98,24 +103,25 @@ set -o pipefail
local log_line="" local log_line=""
while IFS=$'\n' read -r log_line ; do 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 [[ -n "${LOG_TIME}" ]] ; then
if (( appendText )) ; then if (( appendText )) ; then
printf '%24s' "" printf '%24s' "" >${outp}
else 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
fi fi
if (( ! appendText )) ; then if (( ! appendText )) ; then
printf " %3s " "[${log_level}]" printf " %3s " "[${log_level}]" >${outp}
printf "%s" "${log_line}" # 1>&2 # send to stderr printf "%s" "${log_line}" >${outp}
else else
# +3 : "[] " # +3 : "[] "
printf "%$((${#log_level} + 4))s%s" "" "${log_line}" printf "%$((${#log_level} + 4))s%s" "" "${log_line}" >${outp}
fi fi
printf '%b\n' "${col_end}" printf '%b\n' "${col_end}" >${outp}
done <<< "${@:-}" done <<< "${@:-}"
sqr::debugContinue sqr::debugContinue
} }
@@ -186,7 +192,7 @@ set -o pipefail
} }
} }
# Traps ## Traps
{ {
sqr::trap_exit () { sqr::trap_exit () {
exists -f seq_trapExit && seq_trapExit exists -f seq_trapExit && seq_trapExit
@@ -338,6 +344,7 @@ ask() {
answer="${2:-}" answer="${2:-}"
fi fi
printf '%s\n' "${answer}" printf '%s\n' "${answer}"
# return if answer is empty # return if answer is empty
sqr::debugContinue sqr::debugContinue
if (( ! empty )) ; then if (( ! empty )) ; then
@@ -345,6 +352,7 @@ ask() {
fi fi
} }
# exe <COMMAND>
# Handle dry run and verbose output for commands without pipe and/or redirects # Handle dry run and verbose output for commands without pipe and/or redirects
exe() { exe() {
dry && printf -- '--' dry && printf -- '--'
@@ -357,8 +365,8 @@ exe() {
fi fi
} }
# Handle dry run and verbose output for commands containing pipe and/or redirects
# exep <COMMAND AS STRING(S)> # exep <COMMAND AS STRING(S)>
# Handle dry run and verbose output for commands containing pipe and/or redirects
exep() { exep() {
if dry ; then if dry ; then
printf -- '--++ : %s\n' "$*" printf -- '--++ : %s\n' "$*"
@@ -409,3 +417,4 @@ sqr::main() {
} }
sqr::main "$@" sqr::main "$@"