mirror of
https://github.com/m8tin/cis.git
synced 2026-11-02 14:17:24 +01:00
Improved function base.log()
This commit is contained in:
1 file changed
+43
-10
+43
-10
@@ -296,18 +296,51 @@ function base.loadModule() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function base.log() {
|
function base.log() {
|
||||||
local _LOGLEVEL _LOGLEVEL_UPPER
|
local _LOGMODE; base.set _LOGMODE "${1}" '^(line|string)$'
|
||||||
base.set _LOGLEVEL "${1}" '^(error|warn|info|debug)$'
|
shift
|
||||||
_LOGLEVEL_UPPER="${_LOGLEVEL:?"base.log(): Missing valid first parameter LOGLEVEL"}"
|
local _LOGLEVEL; base.set _LOGLEVEL "${1}" '^(error|warn|info|debug)$'
|
||||||
_LOGLEVEL_UPPER="${_LOGLEVEL_UPPER^^}"
|
shift
|
||||||
readonly _LOGLEVEL_UPPER
|
local -r _MESSAGE="${*}"
|
||||||
|
|
||||||
|
if [ "${_LOGMODE}" == "line" ]; then
|
||||||
|
if [ "${CIS[INTERACTIVE]}" == true ]; then
|
||||||
|
if [ "${CIS[COLORIZE]}" == true ]; then
|
||||||
|
# TODO color
|
||||||
case "${CIS[LOGLEVEL]:-warn}" in
|
case "${CIS[LOGLEVEL]:-warn}" in
|
||||||
debug) [ "${_LOGLEVEL_UPPER}" = "DEBUG" ] && echo "[${_LOGLEVEL_UPPER}] $(date +%H:%M:%S) - ${2}" >&2 ;& # Forces execution to continue in the next block
|
debug) [ "${_LOGLEVEL}" = "debug" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;& # Forces execution to continue in the next block
|
||||||
info) [ "${_LOGLEVEL_UPPER}" = "INFO" ] && echo "[${_LOGLEVEL_UPPER}] $(date +%H:%M:%S) - ${2}" >&2 ;&
|
info) [ "${_LOGLEVEL}" = "info" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
warn) [ "${_LOGLEVEL_UPPER}" = "WARN" ] && echo "[${_LOGLEVEL_UPPER}] $(date +%H:%M:%S) - ${2}" >&2 ;&
|
warn) [ "${_LOGLEVEL}" = "warn" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
error) [ "${_LOGLEVEL_UPPER}" = "ERROR" ] && echo "[${_LOGLEVEL_UPPER}] $(date +%H:%M:%S) - ${2}" >&2 ;;
|
error) [ "${_LOGLEVEL}" = "error" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;;
|
||||||
esac
|
esac
|
||||||
|
else
|
||||||
|
case "${CIS[LOGLEVEL]:-warn}" in
|
||||||
|
debug) [ "${_LOGLEVEL}" = "debug" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;& # Forces execution to continue in the next block
|
||||||
|
info) [ "${_LOGLEVEL}" = "info" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
warn) [ "${_LOGLEVEL}" = "warn" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
error) [ "${_LOGLEVEL}" = "error" ] && printf -- "%s - %s\n" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ "${CIS[INTERACTIVE]}" == true ]; then
|
||||||
|
if [ "${CIS[COLORIZE]}" == true ]; then
|
||||||
|
# TODO color
|
||||||
|
case "${CIS[LOGLEVEL]:-warn}" in
|
||||||
|
debug) [ "${_LOGLEVEL}" = "debug" ] && printf -- "%s - %s" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;& # Forces execution to continue in the next block
|
||||||
|
info) [ "${_LOGLEVEL}" = "info" ] && printf -- "%s - %s" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
warn) [ "${_LOGLEVEL}" = "warn" ] && printf -- "%s - %s" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
error) [ "${_LOGLEVEL}" = "error" ] && printf -- "%s - %s" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
case "${CIS[LOGLEVEL]:-warn}" in
|
||||||
|
debug) [ "${_LOGLEVEL}" = "debug" ] && printf -- "%s - %s" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;& # Forces execution to continue in the next block
|
||||||
|
info) [ "${_LOGLEVEL}" = "info" ] && printf -- "%s - %s" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
warn) [ "${_LOGLEVEL}" = "warn" ] && printf -- "%s - %s" "[${_LOGLEVEL^^} ] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;&
|
||||||
|
error) [ "${_LOGLEVEL}" = "error" ] && printf -- "%s - %s" "[${_LOGLEVEL^^}] $(date +%H:%M:%S)" "${_MESSAGE}" >&2 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function base.printEnvironment() {
|
function base.printEnvironment() {
|
||||||
@@ -487,5 +520,5 @@ else
|
|||||||
&& base.checkScriptforCorrectAssignments \
|
&& base.checkScriptforCorrectAssignments \
|
||||||
|| base.abort "The necessary preparations have failed."
|
|| base.abort "The necessary preparations have failed."
|
||||||
|
|
||||||
base.log debug "Module '${BASH_SOURCE[0]}' loaded by script: ${0}"
|
base.log line debug "Module '${BASH_SOURCE[0]}' loaded by script: ${0}"
|
||||||
fi
|
fi
|
||||||
Reference in new issue
Block a user