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/>.
20 export __DRACUT_LOGGER__
=1
22 ## @brief Logging facility module for dracut both at build- and boot-time.
24 # @section intro Introduction
26 # The logger takes a bit from Log4j philosophy. There are defined 6 logging
29 # The TRACE Level designates finer-grained informational events than the
32 # The DEBUG Level designates fine-grained informational events that are most
33 # useful to debug an application.
35 # The INFO level designates informational messages that highlight the
36 # progress of the application at coarse-grained level.
38 # The WARN level designates potentially harmful situations.
40 # The ERROR level designates error events that might still allow the
41 # application to continue running.
43 # The FATAL level designates very severe error events that will presumably
44 # lead the application to abort.
45 # Descriptions are borrowed from Log4j documentation:
46 # http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
48 # @section usage Usage
50 # First of all you have to start with dlog_init() function which initializes
51 # required variables. Don't call any other logging function before that one!
52 # If you're ready with this, you can use following functions which corresponds
53 # clearly to levels listed in @ref intro Introduction. Here they are:
60 # They take all arguments given as a single message to be logged. See dlog()
61 # function for details how it works. Note that you shouldn't use dlog() by
62 # yourself. It's wrapped with above functions.
64 # @see dlog_init() dlog()
66 # @section conf Configuration
68 # Logging is controlled by following global variables:
69 # - @var stdloglvl - logging level to standard error (console output)
70 # - @var sysloglvl - logging level to syslog (by logger command)
71 # - @var fileloglvl - logging level to file
72 # - @var kmsgloglvl - logging level to /dev/kmsg (only for boot-time)
73 # - @var logfile - log file which is used when @var fileloglvl is higher
75 # and two global variables: @var maxloglvl and @var syslogfacility which <b>must
76 # not</b> be overwritten. Both are set by dlog_init(). @var maxloglvl holds
77 # maximum logging level of those three and indicates that dlog_init() was run.
78 # @var syslogfacility is set either to 'user' (when building initramfs) or
79 # 'daemon' (when booting).
81 # Logging level set by the variable means that messages from this logging level
82 # and above (FATAL is the highest) will be shown. Logging levels may be set
83 # independently for each destination (stderr, syslog, file, kmsg).
87 ## @brief Initializes dracut Logger.
89 # @retval 1 if something has gone wrong
90 # @retval 0 on success.
92 # @note This function need to be called before any other from this file.
94 # If any of the variables is not set, this function set it to default:
95 # - @var stdloglvl = 4 (info)
96 # - @var sysloglvl = 0 (no logging)
97 # - @var fileloglvl is set to 4 when @var logfile is set too, otherwise it's
98 # - @var kmsgloglvl = 0 (no logging)
101 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
102 # See file doc comment for details.
107 [ -z "$stdloglvl" ] && stdloglvl
=4
108 [ -z "$sysloglvl" ] && sysloglvl
=0
109 [ -z "$kmsgloglvl" ] && kmsgloglvl
=0
110 # Skip initialization if it's already done.
111 [ -n "$maxloglvl" ] && return 0
113 if [ -z "$fileloglvl" ]; then
114 [ -w "$logfile" ] && fileloglvl
=4 || fileloglvl
=0
115 elif ((fileloglvl
> 0)); then
116 if [[ $logfile ]]; then
119 ! [ -e "$logfile" ] && : > "$logfile"
121 if [[ -w $logfile ]] && [[ -f $logfile ]]; then
122 # Mark new run in the log file
124 if command -v date > /dev
/null
; then
125 echo "=== $(date) ===" >> "$logfile"
127 echo "===============================================" >> "$logfile"
131 # We cannot log to file, so turn this facility off.
134 errmsg
="'$logfile' is not a writable file"
139 if ((UID
!= 0)); then
144 if ((sysloglvl
> 0)); then
145 if [[ -d /run
/systemd
/journal
]] \
146 && type -P systemd-cat
&> /dev
/null \
147 && systemctl
--quiet is-active systemd-journald.socket
&> /dev
/null \
148 && { echo "dracut-$DRACUT_VERSION" | systemd-cat
-t 'dracut' &> /dev
/null
; }; then
149 readonly _systemdcatfile
="$DRACUT_TMPDIR/systemd-cat"
150 mkfifo "$_systemdcatfile"
152 systemd-cat
-t 'dracut' --level-prefix=true
< "$_systemdcatfile" &
153 exec 15> "$_systemdcatfile"
154 elif ! ([[ -S /dev
/log
]] && [[ -w /dev
/log
]] && command -v logger
> /dev
/null
); then
155 # We cannot log to syslog, so turn this facility off.
156 kmsgloglvl
=$sysloglvl
159 errmsg
="No '/dev/log' or 'logger' included for syslog logging"
163 if ((sysloglvl
> 0)) ||
((kmsgloglvl
> 0)); then
164 if [ -n "$dracutbasedir" ]; then
165 readonly syslogfacility
=user
167 readonly syslogfacility
=daemon
169 export syslogfacility
174 for lvl
in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
175 ((lvl
> maxloglvl_l
)) && maxloglvl_l
=$lvl
177 readonly maxloglvl
=$maxloglvl_l
180 if ((stdloglvl
< 6)) && ((kmsgloglvl
< 6)) && ((fileloglvl
< 6)) && ((sysloglvl
< 6)); then
185 if ((stdloglvl
< 5)) && ((kmsgloglvl
< 5)) && ((fileloglvl
< 5)) && ((sysloglvl
< 5)); then
190 if ((stdloglvl
< 4)) && ((kmsgloglvl
< 4)) && ((fileloglvl
< 4)) && ((sysloglvl
< 4)); then
195 if ((stdloglvl
< 3)) && ((kmsgloglvl
< 3)) && ((fileloglvl
< 3)) && ((sysloglvl
< 3)); then
202 if ((stdloglvl
< 2)) && ((kmsgloglvl
< 2)) && ((fileloglvl
< 2)) && ((sysloglvl
< 2)); then
207 if ((stdloglvl
< 1)) && ((kmsgloglvl
< 1)) && ((fileloglvl
< 1)) && ((sysloglvl
< 1)); then
212 [ -n "$errmsg" ] && derror
"$errmsg"
217 ## @brief Converts numeric logging level to the first letter of level name.
219 # @param lvl Numeric logging level in range from 1 to 6.
220 # @retval 1 if @a lvl is out of range.
221 # @retval 0 if @a lvl is correct.
222 # @result Echoes first letter of level name.
235 ## @brief Converts numeric level to logger priority defined by POSIX.2.
237 # @param lvl Numeric logging level in range from 1 to 6.
238 # @retval 1 if @a lvl is out of range.
239 # @retval 0 if @a lvl is correct.
240 # @result Echoes logger priority.
242 printf -- "%s" "$syslogfacility."
254 ## @brief Converts dracut-logger numeric level to syslog log level
256 # @param lvl Numeric logging level in range from 1 to 6.
257 # @retval 1 if @a lvl is out of range.
258 # @retval 0 if @a lvl is correct.
259 # @result Echoes kernel console numeric log level
261 # Conversion is done as follows:
264 # none -> LOG_EMERG (0)
265 # none -> LOG_ALERT (1)
266 # FATAL(1) -> LOG_CRIT (2)
267 # ERROR(2) -> LOG_ERR (3)
268 # WARN(3) -> LOG_WARNING (4)
269 # none -> LOG_NOTICE (5)
270 # INFO(4) -> LOG_INFO (6)
271 # DEBUG(5) -> LOG_DEBUG (7)
275 # @see /usr/include/sys/syslog.h
289 [ "$syslogfacility" = user
] && echo $
((8 + lvl
)) ||
echo $
((24 + lvl
))
292 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
293 # given message with given level (priority).
295 # @param lvl Numeric logging level.
296 # @param msg Message.
297 # @retval 0 It's always returned, even if logging failed.
299 # @note This function is not supposed to be called manually. Please use
300 # dtrace(), ddebug(), or others instead which wrap this one.
302 # This is core logging function which logs given message to standard error, file
303 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
304 # The format is following:
306 # <tt>X: some message</tt>
308 # where @c X is the first letter of logging level. See module description for
311 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
313 # - @c FATAL to @c crit
314 # - @c ERROR to @c error
315 # - @c WARN to @c warning
316 # - @c INFO to @c info
317 # - @c DEBUG and @c TRACE both to @c debug
322 lvlc
=$
(_lvl2char
"$lvl") ||
return 0
324 local lmsg
="$lvlc: $*"
326 ((lvl
<= stdloglvl
)) && printf -- 'dracut[%s]: %s\n' "$lvlc" "$msg" >&2
328 if ((lvl
<= sysloglvl
)); then
329 if [[ "$_dlogfd" ]]; then
330 printf -- "<%s>%s\n" "$(($(_dlvl2syslvl "$lvl") & 7))" "$msg" >&$_dlogfd
332 logger
-t "dracut[$$]" -p "$(_lvl2syspri "$lvl")" -- "$msg"
336 if ((lvl
<= fileloglvl
)) && [[ -w $logfile ]] && [[ -f $logfile ]]; then
337 echo "$lmsg" >> "$logfile"
340 ((lvl
<= kmsgloglvl
)) \
341 && echo "<$(_dlvl2syslvl "$lvl")>dracut[$$] $msg" > /dev
/kmsg
344 ## @brief Internal helper function for _do_dlog()
346 # @param lvl Numeric logging level.
347 # @param msg Message.
348 # @retval 0 It's always returned, even if logging failed.
350 # @note This function is not supposed to be called manually. Please use
351 # dtrace(), ddebug(), or others instead which wrap this one.
353 # This function calls _do_dlog() either with parameter msg, or if
354 # none is given, it will read standard input and will use every line as
358 # dwarn "This is a warning"
359 # echo "This is a warning" | dwarn
361 [ -z "$maxloglvl" ] && return 0
362 (($1 <= maxloglvl
)) ||
return 0
367 while read -r line ||
[ -n "$line" ]; do
368 _do_dlog
"$1" "$line"
373 ## @brief Logs message at TRACE level (6)
375 # @param msg Message.
376 # @retval 0 It's always returned, even if logging failed.
380 if [ -n "$debug" ]; then
385 ## @brief Logs message at DEBUG level (5)
387 # @param msg Message.
388 # @retval 0 It's always returned, even if logging failed.
392 if [ -n "$debug" ]; then
397 ## @brief Logs message at INFO level (4)
399 # @param msg Message.
400 # @retval 0 It's always returned, even if logging failed.
404 if [ -n "$debug" ]; then
409 ## @brief Logs message at WARN level (3)
411 # @param msg Message.
412 # @retval 0 It's always returned, even if logging failed.
416 if [ -n "$debug" ]; then
421 ## @brief It's an alias to dwarn() function.
423 # @param msg Message.
424 # @retval 0 It's always returned, even if logging failed.
428 if [ -n "$debug" ]; then
433 ## @brief Logs message at ERROR level (2)
435 # @param msg Message.
436 # @retval 0 It's always returned, even if logging failed.
440 if [ -n "$debug" ]; then
445 ## @brief Logs message at FATAL level (1)
447 # @param msg Message.
448 # @retval 0 It's always returned, even if logging failed.
452 if [ -n "$debug" ]; then