]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-logger.sh
Fix 98dracut-systemd/dracut-emergency.sh
[thirdparty/dracut.git] / dracut-logger.sh
index 74a8a1bc564cf3973c012b8532d738d408ba3a0a..e72716ff3d2c247df10de1b440b06764101ff588 100755 (executable)
@@ -1,6 +1,4 @@
 #!/bin/bash
-# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
-# ex: ts=8 sw=4 et filetype=sh
 #
 # logging faciality module for dracut both at build- and boot-time
 #
@@ -140,16 +138,24 @@ dlog_init() {
         fi
     fi
 
+    if (( $UID  != 0 )); then
+        kmsgloglvl=0
+        sysloglvl=0
+    fi
+
     if (( $sysloglvl > 0 )); then
-        if type -P systemd-cat &>/dev/null && (( $UID  == 0 )) ; then
-            readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
-            readonly _systemdcatfile="$_dlogdir/systemd-cat"
+        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' <"$_systemdcatfile" &
+            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"
@@ -173,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
@@ -257,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)
@@ -273,7 +279,7 @@ _dlvl2syslvl() {
     local lvl
 
     case "$1" in
-        1) lvl=0;;
+        1) lvl=2;;
         2) lvl=3;;
         3) lvl=4;;
         4) lvl=6;;
@@ -317,13 +323,13 @@ _do_dlog() {
     local msg="$*"
     local lmsg="$lvlc: $*"
 
-    (( $lvl <= $stdloglvl )) && echo "$msg" >&2
+    (( $lvl <= $stdloglvl )) && printf -- 'dracut: %s\n' "$msg" >&2
 
     if (( $lvl <= $sysloglvl )); then
         if [[ "$_dlogfd" ]]; then
-            echo "<$(_dlvl2syslvl $lvl)>$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
 
@@ -358,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