]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-logger.sh
network-manager: remove useless use of basename
[thirdparty/dracut.git] / dracut-logger.sh
index dbe26eed5593f9b25c510c84e802670f09aeeb0c..e72716ff3d2c247df10de1b440b06764101ff588 100755 (executable)
@@ -1,6 +1,4 @@
-#!/bin/sh
-# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
-# ex: ts=8 sw=4 et filetype=sh
+#!/bin/bash
 #
 # logging faciality module for dracut both at build- and boot-time
 #
@@ -23,7 +21,7 @@
 __DRACUT_LOGGER__=1
 
 
-## @brief Logging facility module for Dracut both at build- and boot-time.
+## @brief Logging facility module for dracut both at build- and boot-time.
 #
 # @section intro Introduction
 #
@@ -89,7 +87,7 @@ __DRACUT_LOGGER__=1
 # @see dlog_init()
 
 
-## @brief Initializes Dracut Logger.
+## @brief Initializes dracut Logger.
 #
 # @retval 1 if something has gone wrong
 # @retval 0 on success.
@@ -116,39 +114,55 @@ dlog_init() {
 
     if [ -z "$fileloglvl" ]; then
         [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
-    elif (( $fileloglvl >= 0 )); then
-        __oldumask=$(umask)
-        umask 0377
-        ! [ -e "$logfile" ] && >"$logfile"
-        umask $__oldumask
-        if [ -w "$logfile" -a -f "$logfile" ]; 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"
+                echo >>"$logfile"
+                if command -v date >/dev/null; then
+                    echo "=== $(date) ===" >>"$logfile"
+                else
+                    echo "===============================================" >>"$logfile"
+                fi
+                echo >>"$logfile"
             else
-                echo "===============================================" >>"$logfile"
-            fi
-            echo >>"$logfile"
-        else
             # We cannot log to file, so turn this facility off.
-            fileloglvl=0
-            ret=1
-            errmsg="'$logfile' is not a writable file"
+                fileloglvl=0
+                ret=1
+                errmsg="'$logfile' is not a writable file"
+            fi
         fi
     fi
 
-    if (( $sysloglvl >= 0 )); then
-        if ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
-        then
+    if (( $UID  != 0 )); then
+        kmsgloglvl=0
+        sysloglvl=0
+    fi
+
+    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 _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
             # We cannot log to syslog, so turn this facility off.
+            kmsgloglvl=$sysloglvl
             sysloglvl=0
             ret=1
             errmsg="No '/dev/log' or 'logger' included for syslog logging"
         fi
     fi
 
-    if (($sysloglvl >= 0)) || (($kmsgloglvl >= 0 )); then
+    if (($sysloglvl > 0)) || (($kmsgloglvl > 0 )); then
         if [ -n "$dracutbasedir" ]; then
             readonly syslogfacility=user
         else
@@ -165,34 +179,34 @@ dlog_init() {
     export maxloglvl
 
 
-    if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)); then
+    if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
         unset dtrace
         dtrace() { :; };
     fi
 
-    if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)); then
+    if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
         unset ddebug
         ddebug() { :; };
     fi
 
-    if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)); then
+    if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
         unset dinfo
         dinfo() { :; };
     fi
 
-    if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)); then
+    if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
         unset dwarn
         dwarn() { :; };
         unset dwarning
         dwarning() { :; };
     fi
 
-    if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)); then
+    if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
         unset derror
         derror() { :; };
     fi
 
-    if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)); then
+    if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
         unset dfatal
         dfatal() { :; };
     fi
@@ -249,9 +263,9 @@ _lvl2syspri() {
 # Conversion is done as follows:
 #
 # <tt>
-#   FATAL(1) -> LOG_EMERG (0)
+#   none     -> LOG_EMERG (0)
 #   none     -> LOG_ALERT (1)
-#   none     -> LOG_CRIT (2)
+#   FATAL(1) -> LOG_CRIT (2)
 #   ERROR(2) -> LOG_ERR (3)
 #   WARN(3)  -> LOG_WARNING (4)
 #   none     -> LOG_NOTICE (5)
@@ -265,7 +279,7 @@ _dlvl2syslvl() {
     local lvl
 
     case "$1" in
-        1) lvl=0;;
+        1) lvl=2;;
         2) lvl=3;;
         3) lvl=4;;
         4) lvl=6;;
@@ -306,15 +320,23 @@ _dlvl2syslvl() {
 _do_dlog() {
     local lvl="$1"; shift
     local lvlc=$(_lvl2char "$lvl") || return 0
-    local msg="$lvlc: $*"
+    local msg="$*"
+    local lmsg="$lvlc: $*"
+
+    (( $lvl <= $stdloglvl )) && printf -- 'dracut: %s\n' "$msg" >&2
 
-    (( $lvl <= $stdloglvl )) && echo "$msg" >&2
     if (( $lvl <= $sysloglvl )); then
-        logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) "$msg"
+        if [[ "$_dlogfd" ]]; then
+            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
+        else
+            logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
+        fi
     fi
+
     if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
-        echo "$msg" >>"$logfile"
+        echo "$lmsg" >>"$logfile"
     fi
+
     (( $lvl <= $kmsgloglvl )) && \
         echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
 }
@@ -342,7 +364,7 @@ dlog() {
     if (( $# > 1 )); then
         _do_dlog "$@"
     else
-        while read line; do
+        while read line || [ -n "$line" ]; do
             _do_dlog "$1" "$line"
         done
     fi