3 # logging faciality module for dracut both at build- and boot-time
5 # Copyright 2010 Amadeusz Żołnowski <aidecoe@aidecoe.name>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ## @brief Logging facility module for dracut both at build- and boot-time.
26 # @section intro Introduction
28 # The logger takes a bit from Log4j philosophy. There are defined 6 logging
31 # The TRACE Level designates finer-grained informational events than the
34 # The DEBUG Level designates fine-grained informational events that are most
35 # useful to debug an application.
37 # The INFO level designates informational messages that highlight the
38 # progress of the application at coarse-grained level.
40 # The WARN level designates potentially harmful situations.
42 # The ERROR level designates error events that might still allow the
43 # application to continue running.
45 # The FATAL level designates very severe error events that will presumably
46 # lead the application to abort.
47 # Descriptions are borrowed from Log4j documentation:
48 # http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
50 # @section usage Usage
52 # First of all you have to start with dlog_init() function which initializes
53 # required variables. Don't call any other logging function before that one!
54 # If you're ready with this, you can use following functions which corresponds
55 # clearly to levels listed in @ref intro Introduction. Here they are:
62 # They take all arguments given as a single message to be logged. See dlog()
63 # function for details how it works. Note that you shouldn't use dlog() by
64 # yourself. It's wrapped with above functions.
66 # @see dlog_init() dlog()
68 # @section conf Configuration
70 # Logging is controlled by following global variables:
71 # - @var stdloglvl - logging level to standard error (console output)
72 # - @var sysloglvl - logging level to syslog (by logger command)
73 # - @var fileloglvl - logging level to file
74 # - @var kmsgloglvl - logging level to /dev/kmsg (only for boot-time)
75 # - @var logfile - log file which is used when @var fileloglvl is higher
77 # and two global variables: @var maxloglvl and @var syslogfacility which <b>must
78 # not</b> be overwritten. Both are set by dlog_init(). @var maxloglvl holds
79 # maximum logging level of those three and indicates that dlog_init() was run.
80 # @var syslogfacility is set either to 'user' (when building initramfs) or
81 # 'daemon' (when booting).
83 # Logging level set by the variable means that messages from this logging level
84 # and above (FATAL is the highest) will be shown. Logging levels may be set
85 # independently for each destination (stderr, syslog, file, kmsg).
90 ## @brief Initializes dracut Logger.
92 # @retval 1 if something has gone wrong
93 # @retval 0 on success.
95 # @note This function need to be called before any other from this file.
97 # If any of the variables is not set, this function set it to default:
98 # - @var stdloglvl = 4 (info)
99 # - @var sysloglvl = 0 (no logging)
100 # - @var fileloglvl is set to 4 when @var logfile is set too, otherwise it's
101 # - @var kmsgloglvl = 0 (no logging)
104 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
105 # See file doc comment for details.
108 local ret
=0; local errmsg
109 [ -z "$stdloglvl" ] && stdloglvl
=4
110 [ -z "$sysloglvl" ] && sysloglvl
=0
111 [ -z "$kmsgloglvl" ] && kmsgloglvl
=0
112 # Skip initialization if it's already done.
113 [ -n "$maxloglvl" ] && return 0
115 if [ -z "$fileloglvl" ]; then
116 [ -w "$logfile" ] && fileloglvl
=4 || fileloglvl
=0
117 elif (( $fileloglvl > 0 )); then
118 if [[ $logfile ]]; then
121 ! [ -e "$logfile" ] && >"$logfile"
123 if [ -w "$logfile" -a -f "$logfile" ]; then
124 # Mark new run in the log file
126 if command -v date >/dev
/null
; then
127 echo "=== $(date) ===" >>"$logfile"
129 echo "===============================================" >>"$logfile"
133 # We cannot log to file, so turn this facility off.
136 errmsg
="'$logfile' is not a writable file"
141 if (( $UID != 0 )); then
146 if (( $sysloglvl > 0 )); then
147 if [[ -d /run
/systemd
/journal
]] \
148 && type -P systemd-cat
&>/dev
/null \
149 && systemctl
--quiet is-active systemd-journald.socket
&>/dev
/null \
150 && { echo "dracut-$DRACUT_VERSION" | systemd-cat
-t 'dracut' &>/dev
/null
; } ; then
151 readonly _dlogdir
="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
152 readonly _systemdcatfile
="$_dlogdir/systemd-cat"
153 mkfifo "$_systemdcatfile"
155 systemd-cat
-t 'dracut' --level-prefix=true
<"$_systemdcatfile" &
156 exec 15>"$_systemdcatfile"
157 elif ! [ -S /dev
/log
-a -w /dev
/log
] ||
! command -v logger
>/dev
/null
; then
158 # We cannot log to syslog, so turn this facility off.
159 kmsgloglvl
=$sysloglvl
162 errmsg
="No '/dev/log' or 'logger' included for syslog logging"
166 if (($sysloglvl > 0)) ||
(($kmsgloglvl > 0 )); then
167 if [ -n "$dracutbasedir" ]; then
168 readonly syslogfacility
=user
170 readonly syslogfacility
=daemon
172 export syslogfacility
175 local lvl
; local maxloglvl_l
=0
176 for lvl
in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
177 (( $lvl > $maxloglvl_l )) && maxloglvl_l
=$lvl
179 readonly maxloglvl
=$maxloglvl_l
183 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
188 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
193 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
198 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
205 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
210 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
215 [ -n "$errmsg" ] && derror
"$errmsg"
220 ## @brief Converts numeric logging level to the first letter of level name.
222 # @param lvl Numeric logging level in range from 1 to 6.
223 # @retval 1 if @a lvl is out of range.
224 # @retval 0 if @a lvl is correct.
225 # @result Echoes first letter of level name.
238 ## @brief Converts numeric level to logger priority defined by POSIX.2.
240 # @param lvl Numeric logging level in range from 1 to 6.
241 # @retval 1 if @a lvl is out of range.
242 # @retval 0 if @a lvl is correct.
243 # @result Echoes logger priority.
245 printf $syslogfacility.
257 ## @brief Converts dracut-logger numeric level to syslog log level
259 # @param lvl Numeric logging level in range from 1 to 6.
260 # @retval 1 if @a lvl is out of range.
261 # @retval 0 if @a lvl is correct.
262 # @result Echoes kernel console numeric log level
264 # Conversion is done as follows:
267 # FATAL(1) -> LOG_EMERG (0)
268 # none -> LOG_ALERT (1)
269 # none -> LOG_CRIT (2)
270 # ERROR(2) -> LOG_ERR (3)
271 # WARN(3) -> LOG_WARNING (4)
272 # none -> LOG_NOTICE (5)
273 # INFO(4) -> LOG_INFO (6)
274 # DEBUG(5) -> LOG_DEBUG (7)
278 # @see /usr/include/sys/syslog.h
292 [ "$syslogfacility" = user
] && echo $
((8+$lvl)) ||
echo $
((24+$lvl))
295 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
296 # given message with given level (priority).
298 # @param lvl Numeric logging level.
299 # @param msg Message.
300 # @retval 0 It's always returned, even if logging failed.
302 # @note This function is not supposed to be called manually. Please use
303 # dtrace(), ddebug(), or others instead which wrap this one.
305 # This is core logging function which logs given message to standard error, file
306 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
307 # The format is following:
309 # <tt>X: some message</tt>
311 # where @c X is the first letter of logging level. See module description for
314 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
316 # - @c FATAL to @c crit
317 # - @c ERROR to @c error
318 # - @c WARN to @c warning
319 # - @c INFO to @c info
320 # - @c DEBUG and @c TRACE both to @c debug
322 local lvl
="$1"; shift
323 local lvlc
=$
(_lvl2char
"$lvl") ||
return 0
325 local lmsg
="$lvlc: $*"
327 (( $lvl <= $stdloglvl )) && echo "$msg" >&2
329 if (( $lvl <= $sysloglvl )); then
330 if [[ "$_dlogfd" ]]; then
331 printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
333 logger
-t "dracut[$$]" -p $
(_lvl2syspri
$lvl) -- "$msg"
337 if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
338 echo "$lmsg" >>"$logfile"
341 (( $lvl <= $kmsgloglvl )) && \
342 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev
/kmsg
345 ## @brief Internal helper function for _do_dlog()
347 # @param lvl Numeric logging level.
348 # @param msg Message.
349 # @retval 0 It's always returned, even if logging failed.
351 # @note This function is not supposed to be called manually. Please use
352 # dtrace(), ddebug(), or others instead which wrap this one.
354 # This function calls _do_dlog() either with parameter msg, or if
355 # none is given, it will read standard input and will use every line as
359 # dwarn "This is a warning"
360 # echo "This is a warning" | dwarn
362 [ -z "$maxloglvl" ] && return 0
363 (( $1 <= $maxloglvl )) ||
return 0
365 if (( $# > 1 )); then
369 _do_dlog
"$1" "$line"
374 ## @brief Logs message at TRACE level (6)
376 # @param msg Message.
377 # @retval 0 It's always returned, even if logging failed.
381 [ -n "$debug" ] && set -x ||
:
384 ## @brief Logs message at DEBUG level (5)
386 # @param msg Message.
387 # @retval 0 It's always returned, even if logging failed.
391 [ -n "$debug" ] && set -x ||
:
394 ## @brief Logs message at INFO level (4)
396 # @param msg Message.
397 # @retval 0 It's always returned, even if logging failed.
401 [ -n "$debug" ] && set -x ||
:
404 ## @brief Logs message at WARN level (3)
406 # @param msg Message.
407 # @retval 0 It's always returned, even if logging failed.
411 [ -n "$debug" ] && set -x ||
:
414 ## @brief It's an alias to dwarn() function.
416 # @param msg Message.
417 # @retval 0 It's always returned, even if logging failed.
421 [ -n "$debug" ] && set -x ||
:
424 ## @brief Logs message at ERROR level (2)
426 # @param msg Message.
427 # @retval 0 It's always returned, even if logging failed.
431 [ -n "$debug" ] && set -x ||
:
434 ## @brief Logs message at FATAL level (1)
436 # @param msg Message.
437 # @retval 0 It's always returned, even if logging failed.
441 [ -n "$debug" ] && set -x ||
: