]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-logger.sh
dracut-logger: prefix stderr output with "dracut: "
[thirdparty/dracut.git] / dracut-logger.sh
1 #!/bin/bash
2 #
3 # logging faciality module for dracut both at build- and boot-time
4 #
5 # Copyright 2010 Amadeusz Żołnowski <aidecoe@aidecoe.name>
6 #
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.
11 #
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.
16 #
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/>.
19
20
21 __DRACUT_LOGGER__=1
22
23
24 ## @brief Logging facility module for dracut both at build- and boot-time.
25 #
26 # @section intro Introduction
27 #
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
49 #
50 # @section usage Usage
51 #
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.
65 #
66 # @see dlog_init() dlog()
67 #
68 # @section conf Configuration
69 #
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).
82 #
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).
86 #
87 # @see dlog_init()
88
89
90 ## @brief Initializes dracut Logger.
91 #
92 # @retval 1 if something has gone wrong
93 # @retval 0 on success.
94 #
95 # @note This function need to be called before any other from this file.
96 #
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
103 #
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
114
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"
130 fi
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"
137 fi
138 fi
139 fi
140
141 if (( $UID != 0 )); then
142 kmsgloglvl=0
143 sysloglvl=0
144 fi
145
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 -p "$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"
163 fi
164 fi
165
166 if (($sysloglvl > 0)) || (($kmsgloglvl > 0 )); then
167 if [ -n "$dracutbasedir" ]; then
168 readonly syslogfacility=user
169 else
170 readonly syslogfacility=daemon
171 fi
172 export syslogfacility
173 fi
174
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
181
182
183 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
184 unset dtrace
185 dtrace() { :; };
186 fi
187
188 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
189 unset ddebug
190 ddebug() { :; };
191 fi
192
193 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
194 unset dinfo
195 dinfo() { :; };
196 fi
197
198 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
199 unset dwarn
200 dwarn() { :; };
201 unset dwarning
202 dwarning() { :; };
203 fi
204
205 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
206 unset derror
207 derror() { :; };
208 fi
209
210 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
211 unset dfatal
212 dfatal() { :; };
213 fi
214
215 [ -n "$errmsg" ] && derror "$errmsg"
216
217 return $ret
218 }
219
220 ## @brief Converts numeric logging level to the first letter of level name.
221 #
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
236 }
237
238 ## @brief Converts numeric level to logger priority defined by POSIX.2.
239 #
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
255 }
256
257 ## @brief Converts dracut-logger numeric level to syslog log level
258 #
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
263 #
264 # Conversion is done as follows:
265 #
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>
277 #
278 # @see /usr/include/sys/syslog.h
279 _dlvl2syslvl() {
280 local lvl
281
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
291
292 [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
293 }
294
295 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
296 # given message with given level (priority).
297 #
298 # @param lvl Numeric logging level.
299 # @param msg Message.
300 # @retval 0 It's always returned, even if logging failed.
301 #
302 # @note This function is not supposed to be called manually. Please use
303 # dtrace(), ddebug(), or others instead which wrap this one.
304 #
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:
308 #
309 # <tt>X: some message</tt>
310 #
311 # where @c X is the first letter of logging level. See module description for
312 # details on that.
313 #
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: $*"
326
327 (( $lvl <= $stdloglvl )) && printf -- 'dracut: %s\n' "$msg" >&2
328
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"
334 fi
335 fi
336
337 if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
338 echo "$lmsg" >>"$logfile"
339 fi
340
341 (( $lvl <= $kmsgloglvl )) && \
342 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
343 }
344
345 ## @brief Internal helper function for _do_dlog()
346 #
347 # @param lvl Numeric logging level.
348 # @param msg Message.
349 # @retval 0 It's always returned, even if logging failed.
350 #
351 # @note This function is not supposed to be called manually. Please use
352 # dtrace(), ddebug(), or others instead which wrap this one.
353 #
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.
357 #
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
364
365 if (( $# > 1 )); then
366 _do_dlog "$@"
367 else
368 while read line || [ -n "$line" ]; do
369 _do_dlog "$1" "$line"
370 done
371 fi
372 }
373
374 ## @brief Logs message at TRACE level (6)
375 #
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 || :
382 }
383
384 ## @brief Logs message at DEBUG level (5)
385 #
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 || :
392 }
393
394 ## @brief Logs message at INFO level (4)
395 #
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 || :
402 }
403
404 ## @brief Logs message at WARN level (3)
405 #
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 || :
412 }
413
414 ## @brief It's an alias to dwarn() function.
415 #
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 || :
422 }
423
424 ## @brief Logs message at ERROR level (2)
425 #
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 || :
432 }
433
434 ## @brief Logs message at FATAL level (1)
435 #
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 || :
442 }