From 9a663584433bec74059ce7b205ecdcb32efa27f7 Mon Sep 17 00:00:00 2001
From: Stephan Wendel <me@stephanwe.de>
Date: Thu, 16 Feb 2023 17:21:39 +0100
Subject: [PATCH] fix: store log_level in var instead in function

Instead of checking multiple times the log level store in Variable.

Signed-off-by: Stephan Wendel <me@stephanwe.de>
---
 libs/core.sh           |  4 ++--
 libs/hwhandler.sh      | 12 ++++++------
 libs/logging.sh        | 39 ++++++++++++++++++++-------------------
 libs/v4l2_control.sh   |  6 +++---
 libs/versioncontrol.sh | 12 ++++++------
 5 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/libs/core.sh b/libs/core.sh
index 95ae761..cc659a0 100755
--- a/libs/core.sh
+++ b/libs/core.sh
@@ -115,9 +115,9 @@ function initial_check {
     check_dep "ffmpeg"
     check_apps
     versioncontrol
-    # print cfg if ! log_level: quiet
+    # print cfg if ! "${CROWSNEST_LOG_LEVEL}": quiet
     if [ -z "$(check_cfg "${CROWSNEST_CFG}")" ]; then
-        if [ "$(log_level)" != "quiet" ]; then
+        if [[ "${CROWSNEST_LOG_LEVEL}" != "quiet" ]]; then
             print_cfg
         fi
     fi
diff --git a/libs/hwhandler.sh b/libs/hwhandler.sh
index 095d11f..02615c6 100755
--- a/libs/hwhandler.sh
+++ b/libs/hwhandler.sh
@@ -21,13 +21,13 @@ function detect_avail_cams {
     local avail realpath
     avail="$(find /dev/v4l/by-id/ -iname "*index0" 2> /dev/null)"
     count="$(echo "${avail}" | wc -l)"
-    if [ -d "/dev/v4l/by-id/" ] &&
-    [ -n "${avail}" ]; then
+    if [[ -d "/dev/v4l/by-id/" ]] &&
+    [[ -n "${avail}" ]]; then
         log_msg "INFO: Found ${count} available camera(s)"
         echo "${avail}" | while read -r v4l; do
             realpath=$(readlink -e "${v4l}")
             log_msg "${v4l} -> ${realpath}"
-            if [ "$(log_level)" != "quiet" ]; then
+            if [[ "${CROWSNEST_LOG_LEVEL}" != "quiet" ]]; then
                 list_cam_formats "${v4l}"
                 list_cam_v4l2ctrls "${v4l}"
             fi
@@ -41,8 +41,8 @@ function detect_avail_csi {
     local avail count realpath
     avail="$(find /dev/v4l/by-path/ -iname "*csi*index0" 2> /dev/null)"
     count="$(echo "${avail}" | wc -l)"
-    if [ -d "/dev/v4l/by-path/" ] &&
-    [ -n "${avail}" ]; then
+    if [[ -d "/dev/v4l/by-path/" ]] &&
+    [[ -n "${avail}" ]]; then
         log_msg "INFO: Found ${count} available csi device(s)"
         echo "${avail}" | while read -r csi; do
             realpath=$(readlink -e "${csi}")
@@ -77,7 +77,7 @@ function list_cam_v4l2ctrls {
 # Determine connected "raspicam" device
 function detect_raspicam {
     local avail
-    if [ -f /proc/device-tree/model ] &&
+    if [[ -f /proc/device-tree/model ]] &&
     grep -q "Raspberry" /proc/device-tree/model; then
         avail="$(vcgencmd get_camera | awk -F '=' '{ print $3 }' | cut -d',' -f1)"
     else
diff --git a/libs/logging.sh b/libs/logging.sh
index 46f003c..7cbdbf2 100755
--- a/libs/logging.sh
+++ b/libs/logging.sh
@@ -29,6 +29,7 @@ function set_log_path {
 
 function init_logging {
     set_log_path
+    set_log_level
     delete_log
     log_msg "crowsnest - A webcam Service for multiple Cams and Stream Services."
     log_msg "Version: $(self_version)"
@@ -36,16 +37,16 @@ function init_logging {
     print_host
 }
 
-function log_level {
+function set_log_level {
     local loglevel
     loglevel="$(get_param crowsnest log_level 2> /dev/null)"
     # Set default log_level to quiet
-    if [ -z "${loglevel}" ] || [[ "${loglevel}" != @(quiet|verbose|debug) ]];
-    then
-        echo "quiet"
+    if [ -z "${loglevel}" ] || [[ "${loglevel}" != @(quiet|verbose|debug) ]]; then
+        CROWSNEST_LOG_LEVEL="quiet"
     else
-        echo "${loglevel}"
+        CROWSNEST_LOG_LEVEL="${loglevel}"
     fi
+    declare -r CROWSNEST_LOG_LEVEL
 }
 
 function delete_log {
@@ -69,10 +70,10 @@ function log_output {
     local prefix
     prefix="DEBUG: ${1}"
     while read -r line; do
-        if [ "$(log_level)" == "debug" ]; then
+        if [[ "${CROWSNEST_LOG_LEVEL}" = "debug" ]]; then
             log_msg "${prefix}: ${line}"
         fi
-        if [ -n "${line}" ]; then
+        if [[ -n "${line}" ]]; then
             # needed to prettify ustreamers output
             echo "${line//^--/ustreamer}" | logger -t crowsnest
         fi
@@ -94,25 +95,25 @@ function print_cams {
     v4l="$(find /dev/v4l/by-id/ -iname "*index0" 2> /dev/null | wc -l)"
     csi="$(find /dev/v4l/by-path/ -iname "*csi*index0" 2> /dev/null | wc -l)"
     total="$((v4l+$(detect_raspicam)+csi))"
-    if [ "${total}" -eq 0 ]; then
+    if [[ "${total}" -eq 0 ]]; then
         log_msg "ERROR: No usable Devices Found. Stopping $(basename "${0}")."
         exit 1
     else
         log_msg "INFO: Found ${total} total available Device(s)"
     fi
-    if [ "$(detect_raspicam)" -ne 0 ]; then
+    if [[ "$(detect_raspicam)" -ne 0 ]]; then
         raspicam="$(v4l2-ctl --list-devices |  grep -A1 -e 'mmal' | \
         awk 'NR==2 {print $1}')"
         log_msg "Detected 'Raspicam' Device -> ${raspicam}"
-        if [ ! "$(log_level)" = "quiet" ]; then
+        if [[ ! "${CROWSNEST_LOG_LEVEL}" = "quiet" ]]; then
             list_cam_formats "${raspicam}"
             list_cam_v4l2ctrls "${raspicam}"
         fi
     fi
-    if [ -d "/dev/v4l/by-id/" ]; then
+    if [[ -d "/dev/v4l/by-id/" ]]; then
         detect_avail_cams
     fi
-    if [ -d "/dev/v4l/by-path" ]; then
+    if [[ -d "/dev/v4l/by-path" ]]; then
         detect_avail_csi
     fi
 }
@@ -123,28 +124,28 @@ function print_host {
     sbc_model="$(grep "Model" /proc/cpuinfo | cut -d':' -f2)"
     memtotal="$(grep "MemTotal:" /proc/meminfo | awk '{print $2" "$3}')"
     disksize="$(LC_ALL=C df -h / | awk 'NR==2 {print $4" / "$2}')"
-    ## print only if not log_level: quiet
-    if [ "$(log_level)" != "quiet" ]; then
+    ## print only if not "${CROWSNEST_LOG_LEVEL}": quiet
+    if [[ "${CROWSNEST_LOG_LEVEL}" != "quiet" ]]; then
         log_msg "INFO: Host information:"
         ## OS Infos
         ## OS Version
-        if [ -f /etc/os-release ]; then
+        if [[ -f /etc/os-release ]]; then
             log_msg "Host Info: Distribution: $(grep "PRETTY" /etc/os-release | \
             cut -d '=' -f2 | sed 's/^"//;s/"$//')"
         fi
         ## Release Version of MainsailOS (if file present)
-        if [ -f /etc/mainsailos-release ]; then
+        if [[ -f /etc/mainsailos-release ]]; then
             log_msg "Host Info: Release: $(cat /etc/mainsailos-release)"
         fi
         ## Kernel version
         log_msg "Host Info: Kernel: $(uname -s) $(uname -rm)"
         ## Host Machine Infos
         ## Host model
-        if [ -n "${sbc_model}" ]; then
+        if [[ -n "${sbc_model}" ]]; then
             log_msg "Host Info: Model: ${sbc_model}"
         fi
-        if [ -n "${generic_model}" ] &&
-        [ -z "${sbc_model}" ]; then
+        if [[ -n "${generic_model}" ]] &&
+        [[ -z "${sbc_model}" ]]; then
             log_msg "Host Info: Model: ${generic_model}"
         fi
         ## CPU count
diff --git a/libs/v4l2_control.sh b/libs/v4l2_control.sh
index 9bcec71..7e9eef1 100755
--- a/libs/v4l2_control.sh
+++ b/libs/v4l2_control.sh
@@ -29,7 +29,7 @@ function v4l2_control {
             # get v4l2ctl parameters
             v4l2ctl="$(get_param "cam ${cam}" v4l2ctl)"
             # if not empty do
-            if [ -n "${v4l2ctl}" ]; then
+            if [[ -n "${v4l2ctl}" ]]; then
                 # Write configured options to Log
                 log_msg "Device: [cam ${cam}]"
                 log_msg "Options: ${v4l2ctl}"
@@ -48,7 +48,7 @@ function v4l2_control {
                         v4l2-ctl -d "${device}" -c "${param}" 2> /dev/null
                     fi
                 done
-                    if [ "$(log_level)" == "debug" ]; then
+                    if [[ "${CROWSNEST_LOG_LEVEL}" == "debug" ]]; then
                         v4l2-ctl -d "${device}" -L | log_output "v4l2ctl"
                     fi
             else
@@ -123,7 +123,7 @@ function brokenfocus {
                 detected_broken_dev_msg
                 set_focus_absolute "${device}" "${conf_val}"
             fi
-            if [ "$(log_level)" == "debug" ] && [ -n "${cur_val}" ]; then
+            if [[ "${CROWSNEST_LOG_LEVEL}" == "debug" ]] && [[ -n "${cur_val}" ]]; then
                 debug_focus_val_msg "$(get_current_value "${device}")"
             fi
         done
diff --git a/libs/versioncontrol.sh b/libs/versioncontrol.sh
index 9348a95..e48dad4 100644
--- a/libs/versioncontrol.sh
+++ b/libs/versioncontrol.sh
@@ -29,10 +29,10 @@ function versioncontrol {
         pushd "${BASE_CN_PATH}"/bin/ustreamer || exit 1
             avail_ver="$(git describe --tags --always)"
             cur_ver="v$("${PWD}"/ustreamer -v)"
-            if [ "${cur_ver}" == "${avail_ver}" ]; then
+            if [[ "${cur_ver}" == "${avail_ver}" ]]; then
                 vc_log_msg "ustreamer is up to date. (${cur_ver})"
             fi
-            if [ "${cur_ver}" != "${avail_ver}" ]; then
+            if [[ "${cur_ver}" != "${avail_ver}" ]]; then
                 vc_log_msg "ustreamer new version available: ${avail_ver} (${cur_ver})."
             fi
         popd || exit 1
@@ -43,7 +43,7 @@ function versioncontrol {
         pushd "${BASE_CN_PATH}"/bin/rtsp-simple-server || exit 1
             avail_ver="$(cat version)"
             cur_ver="$("${PWD}"/rtsp-simple-server --version)"
-            if [ "${cur_ver}" == "${avail_ver}" ]; then
+            if [[ "${cur_ver}" == "${avail_ver}" ]]; then
                 vc_log_msg "rtsp-simple-server is up to date. (${cur_ver})"
             fi
             if [ "${cur_ver}" != "${avail_ver}" ]; then
@@ -56,17 +56,17 @@ function versioncontrol {
         local cur_ver avail_ver
             avail_ver="$(dpkg-query -W ffmpeg | awk -F':' '{print $2}')"
             cur_ver="$(ffmpeg -version | awk 'NR==1 {print $3}')"
-            if [ "${cur_ver}" == "${avail_ver}" ]; then
+            if [[ "${cur_ver}" == "${avail_ver}" ]]; then
                 vc_log_msg "ffmpeg is up to date. (${cur_ver})"
             fi
-            if [ "${cur_ver}" != "${avail_ver}" ]; then
+            if [[ "${cur_ver}" != "${avail_ver}" ]]; then
                 vc_log_msg "ffmpeg new version available: ${avail_ver} (${cur_ver})."
             fi
     }
 
     ### MAIN
     function main {
-        if [ "$(log_level)" != "quiet" ]; then
+        if [[ "${CROWSNEST_LOG_LEVEL}" != "quiet" ]]; then
             get_ustreamer_version
             get_rtsp_version
             get_ffmpeg_version