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
}
# 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 <COMMAND>
# 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 <COMMAND AS STRING(S)>
# 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 "$@"