]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-logger.sh
fix(btrfs): do not require module via cmdline when --no-kernel
[thirdparty/dracut.git] / dracut-logger.sh
index 2a6019c6cdd6ac4167410c564518bc3774f37dca..1a57e5e03fbd7bf4fdbad9d004a1521c9afe1569 100755 (executable)
@@ -17,9 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-
-__DRACUT_LOGGER__=1
-
+export __DRACUT_LOGGER__=1
 
 ## @brief Logging facility module for dracut both at build- and boot-time.
 #
@@ -86,7 +84,6 @@ __DRACUT_LOGGER__=1
 #
 # @see dlog_init()
 
-
 ## @brief Initializes dracut Logger.
 #
 # @retval 1 if something has gone wrong
@@ -105,7 +102,8 @@ __DRACUT_LOGGER__=1
 # See file doc comment for details.
 dlog_init() {
     local __oldumask
-    local ret=0; local errmsg
+    local ret=0
+    local errmsg
     [ -z "$stdloglvl" ] && stdloglvl=4
     [ -z "$sysloglvl" ] && sysloglvl=0
     [ -z "$kmsgloglvl" ] && kmsgloglvl=0
@@ -114,23 +112,23 @@ dlog_init() {
 
     if [ -z "$fileloglvl" ]; then
         [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
-    elif (( $fileloglvl > 0 )); then
+    elif ((fileloglvl > 0)); then
         if [[ $logfile ]]; then
             __oldumask=$(umask)
             umask 0377
-            ! [ -e "$logfile" ] && >"$logfile"
-            umask $__oldumask
-            if [ -w "$logfile" -a -f "$logfile" ]; then
-            # Mark new run in the log file
-                echo >>"$logfile"
-                if command -v date >/dev/null; then
-                    echo "=== $(date) ===" >>"$logfile"
+            ! [ -e "$logfile" ] && : > "$logfile"
+            umask "$__oldumask"
+            if [[ -w $logfile ]] && [[ -f $logfile ]]; then
+                # Mark new run in the log file
+                echo >> "$logfile"
+                if command -v date > /dev/null; then
+                    echo "=== $(date) ===" >> "$logfile"
                 else
-                    echo "===============================================" >>"$logfile"
+                    echo "===============================================" >> "$logfile"
                 fi
-                echo >>"$logfile"
+                echo >> "$logfile"
             else
-            # We cannot log to file, so turn this facility off.
+                # We cannot log to file, so turn this facility off.
                 fileloglvl=0
                 ret=1
                 errmsg="'$logfile' is not a writable file"
@@ -138,23 +136,22 @@ dlog_init() {
         fi
     fi
 
-    if (( $UID  != 0 )); then
+    if ((UID != 0)); then
         kmsgloglvl=0
         sysloglvl=0
     fi
 
-    if (( $sysloglvl > 0 )); then
+    if ((sysloglvl > 0)); then
         if [[ -d /run/systemd/journal ]] \
-            && type -P systemd-cat &>/dev/null \
-            && systemctl --quiet is-active systemd-journald.socket &>/dev/null \
-            && { echo "dracut-$DRACUT_VERSION" | systemd-cat -t 'dracut' &>/dev/null; } ; then
-            readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
-            readonly _systemdcatfile="$_dlogdir/systemd-cat"
+            && type -P systemd-cat &> /dev/null \
+            && systemctl --quiet is-active systemd-journald.socket &> /dev/null \
+            && { echo "dracut-$DRACUT_VERSION" | systemd-cat -t 'dracut' &> /dev/null; }; then
+            readonly _systemdcatfile="$DRACUT_TMPDIR/systemd-cat"
             mkfifo "$_systemdcatfile"
             readonly _dlogfd=15
-            systemd-cat -t 'dracut' --level-prefix=true <"$_systemdcatfile" &
-            exec 15>"$_systemdcatfile"
-        elif ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null; then
+            systemd-cat -t 'dracut' --level-prefix=true < "$_systemdcatfile" &
+            exec 15> "$_systemdcatfile"
+        elif ! ([[ -S /dev/log ]] && [[ -w /dev/log ]] && command -v logger > /dev/null); then
             # We cannot log to syslog, so turn this facility off.
             kmsgloglvl=$sysloglvl
             sysloglvl=0
@@ -163,7 +160,7 @@ dlog_init() {
         fi
     fi
 
-    if (($sysloglvl > 0)) || (($kmsgloglvl > 0 )); then
+    if ((sysloglvl > 0)) || ((kmsgloglvl > 0)); then
         if [ -n "$dracutbasedir" ]; then
             readonly syslogfacility=user
         else
@@ -172,44 +169,44 @@ dlog_init() {
         export syslogfacility
     fi
 
-    local lvl; local maxloglvl_l=0
+    local lvl
+    local maxloglvl_l=0
     for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
-        (( $lvl > $maxloglvl_l )) && maxloglvl_l=$lvl
+        ((lvl > maxloglvl_l)) && maxloglvl_l=$lvl
     done
     readonly maxloglvl=$maxloglvl_l
     export maxloglvl
 
-
-    if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
+    if ((stdloglvl < 6)) && ((kmsgloglvl < 6)) && ((fileloglvl < 6)) && ((sysloglvl < 6)); then
         unset dtrace
-        dtrace() { :; };
+        dtrace() { :; }
     fi
 
-    if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
+    if ((stdloglvl < 5)) && ((kmsgloglvl < 5)) && ((fileloglvl < 5)) && ((sysloglvl < 5)); then
         unset ddebug
-        ddebug() { :; };
+        ddebug() { :; }
     fi
 
-    if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
+    if ((stdloglvl < 4)) && ((kmsgloglvl < 4)) && ((fileloglvl < 4)) && ((sysloglvl < 4)); then
         unset dinfo
-        dinfo() { :; };
+        dinfo() { :; }
     fi
 
-    if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
+    if ((stdloglvl < 3)) && ((kmsgloglvl < 3)) && ((fileloglvl < 3)) && ((sysloglvl < 3)); then
         unset dwarn
-        dwarn() { :; };
+        dwarn() { :; }
         unset dwarning
-        dwarning() { :; };
+        dwarning() { :; }
     fi
 
-    if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
+    if ((stdloglvl < 2)) && ((kmsgloglvl < 2)) && ((fileloglvl < 2)) && ((sysloglvl < 2)); then
         unset derror
-        derror() { :; };
+        derror() { :; }
     fi
 
-    if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
+    if ((stdloglvl < 1)) && ((kmsgloglvl < 1)) && ((fileloglvl < 1)) && ((sysloglvl < 1)); then
         unset dfatal
-        dfatal() { :; };
+        dfatal() { :; }
     fi
 
     [ -n "$errmsg" ] && derror "$errmsg"
@@ -225,13 +222,13 @@ dlog_init() {
 # @result Echoes first letter of level name.
 _lvl2char() {
     case "$1" in
-        1) echo F;;
-        2) echo E;;
-        3) echo W;;
-        4) echo I;;
-        5) echo D;;
-        6) echo T;;
-        *) return 1;;
+        1) echo F ;;
+        2) echo E ;;
+        3) echo W ;;
+        4) echo I ;;
+        5) echo D ;;
+        6) echo T ;;
+        *) return 1 ;;
     esac
 }
 
@@ -242,15 +239,15 @@ _lvl2char() {
 # @retval 0 if @a lvl is correct.
 # @result Echoes logger priority.
 _lvl2syspri() {
-    printf $syslogfacility.
+    printf -- "%s" "$syslogfacility."
     case "$1" in
-        1) echo crit;;
-        2) echo error;;
-        3) echo warning;;
-        4) echo info;;
-        5) echo debug;;
-        6) echo debug;;
-        *) return 1;;
+        1) echo crit ;;
+        2) echo error ;;
+        3) echo warning ;;
+        4) echo info ;;
+        5) echo debug ;;
+        6) echo debug ;;
+        *) return 1 ;;
     esac
 }
 
@@ -280,16 +277,16 @@ _dlvl2syslvl() {
     local lvl
 
     case "$1" in
-        1) lvl=2;;
-        2) lvl=3;;
-        3) lvl=4;;
-        4) lvl=6;;
-        5) lvl=7;;
-        6) lvl=7;;
-        *) return 1;;
+        1) lvl=2 ;;
+        2) lvl=3 ;;
+        3) lvl=4 ;;
+        4) lvl=6 ;;
+        5) lvl=7 ;;
+        6) lvl=7 ;;
+        *) return 1 ;;
     esac
 
-    [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
+    [ "$syslogfacility" = user ] && echo $((8 + lvl)) || echo $((24 + lvl))
 }
 
 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
@@ -319,27 +316,29 @@ _dlvl2syslvl() {
 #   - @c INFO to @c info
 #   - @c DEBUG and @c TRACE both to @c debug
 _do_dlog() {
-    local lvl="$1"; shift
-    local lvlc=$(_lvl2char "$lvl") || return 0
+    local lvlc
+    local lvl="$1"
+    shift
+    lvlc=$(_lvl2char "$lvl") || return 0
     local msg="$*"
     local lmsg="$lvlc: $*"
 
-    (( $lvl <= $stdloglvl )) && echo "$msg" >&2
+    ((lvl <= stdloglvl)) && printf -- 'dracut[%s]: %s\n' "$lvlc" "$msg" >&2
 
-    if (( $lvl <= $sysloglvl )); then
+    if ((lvl <= sysloglvl)); then
         if [[ "$_dlogfd" ]]; then
-            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
+            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl "$lvl") & 7))" "$msg" >&$_dlogfd
         else
-            logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
+            logger -t "dracut[$$]" -p "$(_lvl2syspri "$lvl")" -- "$msg"
         fi
     fi
 
-    if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
-        echo "$lmsg" >>"$logfile"
+    if ((lvl <= fileloglvl)) && [[ -w $logfile ]] && [[ -f $logfile ]]; then
+        echo "$lmsg" >> "$logfile"
     fi
 
-    (( $lvl <= $kmsgloglvl )) && \
-        echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
+    ((lvl <= kmsgloglvl)) \
+        && echo "<$(_dlvl2syslvl "$lvl")>dracut[$$] $msg" > /dev/kmsg
 }
 
 ## @brief Internal helper function for _do_dlog()
@@ -360,12 +359,12 @@ _do_dlog() {
 # echo "This is a warning" | dwarn
 dlog() {
     [ -z "$maxloglvl" ] && return 0
-    (( $1 <= $maxloglvl )) || return 0
+    (($1 <= maxloglvl)) || return 0
 
-    if (( $# > 1 )); then
+    if (($# > 1)); then
         _do_dlog "$@"
     else
-        while read line || [ -n "$line" ]; do
+        while read -r line || [ -n "$line" ]; do
             _do_dlog "$1" "$line"
         done
     fi
@@ -378,7 +377,9 @@ dlog() {
 dtrace() {
     set +x
     dlog 6 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief Logs message at DEBUG level (5)
@@ -388,7 +389,9 @@ dtrace() {
 ddebug() {
     set +x
     dlog 5 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief Logs message at INFO level (4)
@@ -398,7 +401,9 @@ ddebug() {
 dinfo() {
     set +x
     dlog 4 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief Logs message at WARN level (3)
@@ -408,7 +413,9 @@ dinfo() {
 dwarn() {
     set +x
     dlog 3 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief It's an alias to dwarn() function.
@@ -418,7 +425,9 @@ dwarn() {
 dwarning() {
     set +x
     dwarn "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief Logs message at ERROR level (2)
@@ -428,7 +437,9 @@ dwarning() {
 derror() {
     set +x
     dlog 2 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }
 
 ## @brief Logs message at FATAL level (1)
@@ -438,5 +449,7 @@ derror() {
 dfatal() {
     set +x
     dlog 1 "$@"
-    [ -n "$debug" ] && set -x || :
+    if [ -n "$debug" ]; then
+        set -x
+    fi
 }