dracut-systemd/dracut-initqueue: only start service if really needed
[dracut.git] / dracut-logger.sh
blob2a6019c6cdd6ac4167410c564518bc3774f37dca
1 #!/bin/bash
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/>.
21 __DRACUT_LOGGER__=1
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
29 # levels:
30 # - TRACE (6)
31 # The TRACE Level designates finer-grained informational events than the
32 # DEBUG.
33 # - DEBUG (5)
34 # The DEBUG Level designates fine-grained informational events that are most
35 # useful to debug an application.
36 # - INFO (4)
37 # The INFO level designates informational messages that highlight the
38 # progress of the application at coarse-grained level.
39 # - WARN (3)
40 # The WARN level designates potentially harmful situations.
41 # - ERROR (2)
42 # The ERROR level designates error events that might still allow the
43 # application to continue running.
44 # - FATAL (1)
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:
56 # - dtrace()
57 # - ddebug()
58 # - dinfo()
59 # - dwarn()
60 # - derror()
61 # - dfatal()
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
76 # than 0
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).
87 # @see dlog_init()
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)
102 # set to 0
104 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
105 # See file doc comment for details.
106 dlog_init() {
107 local __oldumask
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
119 __oldumask=$(umask)
120 umask 0377
121 ! [ -e "$logfile" ] && >"$logfile"
122 umask $__oldumask
123 if [ -w "$logfile" -a -f "$logfile" ]; then
124 # Mark new run in the log file
125 echo >>"$logfile"
126 if command -v date >/dev/null; then
127 echo "=== $(date) ===" >>"$logfile"
128 else
129 echo "===============================================" >>"$logfile"
131 echo >>"$logfile"
132 else
133 # We cannot log to file, so turn this facility off.
134 fileloglvl=0
135 ret=1
136 errmsg="'$logfile' is not a writable file"
141 if (( $UID != 0 )); then
142 kmsgloglvl=0
143 sysloglvl=0
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"
154 readonly _dlogfd=15
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
160 sysloglvl=0
161 ret=1
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
169 else
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
178 done
179 readonly maxloglvl=$maxloglvl_l
180 export maxloglvl
183 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
184 unset dtrace
185 dtrace() { :; };
188 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
189 unset ddebug
190 ddebug() { :; };
193 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
194 unset dinfo
195 dinfo() { :; };
198 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
199 unset dwarn
200 dwarn() { :; };
201 unset dwarning
202 dwarning() { :; };
205 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
206 unset derror
207 derror() { :; };
210 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
211 unset dfatal
212 dfatal() { :; };
215 [ -n "$errmsg" ] && derror "$errmsg"
217 return $ret
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.
226 _lvl2char() {
227 case "$1" in
228 1) echo F;;
229 2) echo E;;
230 3) echo W;;
231 4) echo I;;
232 5) echo D;;
233 6) echo T;;
234 *) return 1;;
235 esac
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.
244 _lvl2syspri() {
245 printf $syslogfacility.
246 case "$1" in
247 1) echo crit;;
248 2) echo error;;
249 3) echo warning;;
250 4) echo info;;
251 5) echo debug;;
252 6) echo debug;;
253 *) return 1;;
254 esac
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:
266 # <tt>
267 # none -> LOG_EMERG (0)
268 # none -> LOG_ALERT (1)
269 # FATAL(1) -> 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)
275 # TRACE(6) /
276 # </tt>
278 # @see /usr/include/sys/syslog.h
279 _dlvl2syslvl() {
280 local lvl
282 case "$1" in
283 1) lvl=2;;
284 2) lvl=3;;
285 3) lvl=4;;
286 4) lvl=6;;
287 5) lvl=7;;
288 6) lvl=7;;
289 *) return 1;;
290 esac
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
312 # details on that.
314 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
315 # following:
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
321 _do_dlog() {
322 local lvl="$1"; shift
323 local lvlc=$(_lvl2char "$lvl") || return 0
324 local msg="$*"
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
332 else
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
356 # a message.
358 # This enables:
359 # dwarn "This is a warning"
360 # echo "This is a warning" | dwarn
361 dlog() {
362 [ -z "$maxloglvl" ] && return 0
363 (( $1 <= $maxloglvl )) || return 0
365 if (( $# > 1 )); then
366 _do_dlog "$@"
367 else
368 while read line || [ -n "$line" ]; do
369 _do_dlog "$1" "$line"
370 done
374 ## @brief Logs message at TRACE level (6)
376 # @param msg Message.
377 # @retval 0 It's always returned, even if logging failed.
378 dtrace() {
379 set +x
380 dlog 6 "$@"
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.
388 ddebug() {
389 set +x
390 dlog 5 "$@"
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.
398 dinfo() {
399 set +x
400 dlog 4 "$@"
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.
408 dwarn() {
409 set +x
410 dlog 3 "$@"
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.
418 dwarning() {
419 set +x
420 dwarn "$@"
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.
428 derror() {
429 set +x
430 dlog 2 "$@"
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.
438 dfatal() {
439 set +x
440 dlog 1 "$@"
441 [ -n "$debug" ] && set -x || :