From b1594a8b1c1ed6ce701f2f306c31fd8b6b723c06 Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Tue, 5 Apr 2022 17:10:01 +0200 Subject: [PATCH] log/scripts: introduce proper log level fallback and env getter function (#2506) This PR does two small things: 1. The log level, in case it is unset, will now be "calculated" from `/etc/dms-settings` and not always default to `info`. This way, we can ensure that more often than not, the log level the user chose when starting DMS is used everywhere. 2. I noticed that the way I obtained the log level could be used to obtain any env variable's log level. I therefore added a function to `utils.sh` in case we use it in the future. --- target/scripts/helpers/log.sh | 22 +++++++++++++++------- target/scripts/helpers/utils.sh | 9 +++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/target/scripts/helpers/log.sh b/target/scripts/helpers/log.sh index 190fad5c..4af09c04 100644 --- a/target/scripts/helpers/log.sh +++ b/target/scripts/helpers/log.sh @@ -39,15 +39,23 @@ function _log return 1 fi - local MESSAGE LEVEL_AS_INT + local MESSAGE LEVEL_AS_INT LOG_LEVEL_FALLBACK MESSAGE="${LOG_RESET}[" - case "${LOG_LEVEL:-}" in - ( 'trace' ) LEVEL_AS_INT=5 ;; - ( 'debug' ) LEVEL_AS_INT=4 ;; - ( 'warn' ) LEVEL_AS_INT=2 ;; - ( 'error' ) LEVEL_AS_INT=1 ;; - ( * ) LEVEL_AS_INT=3 ;; + if [[ -e /etc/dms-settings ]] + then + LOG_LEVEL_FALLBACK=$(grep "^LOG_LEVEL=" /etc/dms-settings | cut -d '=' -f 2) + LOG_LEVEL_FALLBACK="${LOG_LEVEL_FALLBACK:1:-1}" + else + LOG_LEVEL_FALLBACK='info' + fi + + case "${LOG_LEVEL:-${LOG_LEVEL_FALLBACK}}" in + ( 'trace' ) LEVEL_AS_INT=5 ;; + ( 'debug' ) LEVEL_AS_INT=4 ;; + ( 'warn' ) LEVEL_AS_INT=2 ;; + ( 'error' ) LEVEL_AS_INT=1 ;; + ( * ) LEVEL_AS_INT=3 ;; esac case "${1}" in diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index f7e51eb8..f217b8d8 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -11,3 +11,12 @@ function _is_comment { grep -q -E "^\s*$|^\s*#" <<< "${1}" } + +# Provide the name of an environment variable to this function +# and it will return its value stored in /etc/dms-settings +function _get_dms_env_value +{ + local VALUE + VALUE=$(grep "^${1}=" /etc/dms-settings | cut -d '=' -f 2) + printf '%s' "${VALUE:1:-1}" +}