]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-logger.sh
fix(zfcp_rules): correct shellcheck regression when parsing ccw args
[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 export __DRACUT_LOGGER__=1
21
22 ## @brief Logging facility module for dracut both at build- and boot-time.
23 #
24 # @section intro Introduction
25 #
26 # The logger takes a bit from Log4j philosophy. There are defined 6 logging
27 # levels:
28 # - TRACE (6)
29 # The TRACE Level designates finer-grained informational events than the
30 # DEBUG.
31 # - DEBUG (5)
32 # The DEBUG Level designates fine-grained informational events that are most
33 # useful to debug an application.
34 # - INFO (4)
35 # The INFO level designates informational messages that highlight the
36 # progress of the application at coarse-grained level.
37 # - WARN (3)
38 # The WARN level designates potentially harmful situations.
39 # - ERROR (2)
40 # The ERROR level designates error events that might still allow the
41 # application to continue running.
42 # - FATAL (1)
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
47 #
48 # @section usage Usage
49 #
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:
54 # - dtrace()
55 # - ddebug()
56 # - dinfo()
57 # - dwarn()
58 # - derror()
59 # - dfatal()
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.
63 #
64 # @see dlog_init() dlog()
65 #
66 # @section conf Configuration
67 #
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
74 # than 0
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).
80 #
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).
84 #
85 # @see dlog_init()
86
87 ## @brief Initializes dracut Logger.
88 #
89 # @retval 1 if something has gone wrong
90 # @retval 0 on success.
91 #
92 # @note This function need to be called before any other from this file.
93 #
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)
99 # set to 0
100 #
101 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
102 # See file doc comment for details.
103 dlog_init() {
104 local __oldumask
105 local ret=0
106 local errmsg
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
112
113 if [ -z "$fileloglvl" ]; then
114 [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
115 elif ((fileloglvl > 0)); then
116 if [[ $logfile ]]; then
117 __oldumask=$(umask)
118 umask 0377
119 ! [ -e "$logfile" ] && : > "$logfile"
120 umask "$__oldumask"
121 if [[ -w $logfile ]] && [[ -f $logfile ]]; then
122 # Mark new run in the log file
123 echo >> "$logfile"
124 if command -v date > /dev/null; then
125 echo "=== $(date) ===" >> "$logfile"
126 else
127 echo "===============================================" >> "$logfile"
128 fi
129 echo >> "$logfile"
130 else
131 # We cannot log to file, so turn this facility off.
132 fileloglvl=0
133 ret=1
134 errmsg="'$logfile' is not a writable file"
135 fi
136 fi
137 fi
138
139 if ((UID != 0)); then
140 kmsgloglvl=0
141 sysloglvl=0
142 fi
143
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"
151 readonly _dlogfd=15
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
157 sysloglvl=0
158 ret=1
159 errmsg="No '/dev/log' or 'logger' included for syslog logging"
160 fi
161 fi
162
163 if ((sysloglvl > 0)) || ((kmsgloglvl > 0)); then
164 if [ -n "$dracutbasedir" ]; then
165 readonly syslogfacility=user
166 else
167 readonly syslogfacility=daemon
168 fi
169 export syslogfacility
170 fi
171
172 local lvl
173 local maxloglvl_l=0
174 for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
175 ((lvl > maxloglvl_l)) && maxloglvl_l=$lvl
176 done
177 readonly maxloglvl=$maxloglvl_l
178 export maxloglvl
179
180 if ((stdloglvl < 6)) && ((kmsgloglvl < 6)) && ((fileloglvl < 6)) && ((sysloglvl < 6)); then
181 unset dtrace
182 dtrace() { :; }
183 fi
184
185 if ((stdloglvl < 5)) && ((kmsgloglvl < 5)) && ((fileloglvl < 5)) && ((sysloglvl < 5)); then
186 unset ddebug
187 ddebug() { :; }
188 fi
189
190 if ((stdloglvl < 4)) && ((kmsgloglvl < 4)) && ((fileloglvl < 4)) && ((sysloglvl < 4)); then
191 unset dinfo
192 dinfo() { :; }
193 fi
194
195 if ((stdloglvl < 3)) && ((kmsgloglvl < 3)) && ((fileloglvl < 3)) && ((sysloglvl < 3)); then
196 unset dwarn
197 dwarn() { :; }
198 unset dwarning
199 dwarning() { :; }
200 fi
201
202 if ((stdloglvl < 2)) && ((kmsgloglvl < 2)) && ((fileloglvl < 2)) && ((sysloglvl < 2)); then
203 unset derror
204 derror() { :; }
205 fi
206
207 if ((stdloglvl < 1)) && ((kmsgloglvl < 1)) && ((fileloglvl < 1)) && ((sysloglvl < 1)); then
208 unset dfatal
209 dfatal() { :; }
210 fi
211
212 [ -n "$errmsg" ] && derror "$errmsg"
213
214 return $ret
215 }
216
217 ## @brief Converts numeric logging level to the first letter of level name.
218 #
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.
223 _lvl2char() {
224 case "$1" in
225 1) echo F ;;
226 2) echo E ;;
227 3) echo W ;;
228 4) echo I ;;
229 5) echo D ;;
230 6) echo T ;;
231 *) return 1 ;;
232 esac
233 }
234
235 ## @brief Converts numeric level to logger priority defined by POSIX.2.
236 #
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.
241 _lvl2syspri() {
242 printf -- "%s" "$syslogfacility."
243 case "$1" in
244 1) echo crit ;;
245 2) echo error ;;
246 3) echo warning ;;
247 4) echo info ;;
248 5) echo debug ;;
249 6) echo debug ;;
250 *) return 1 ;;
251 esac
252 }
253
254 ## @brief Converts dracut-logger numeric level to syslog log level
255 #
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
260 #
261 # Conversion is done as follows:
262 #
263 # <tt>
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)
272 # TRACE(6) /
273 # </tt>
274 #
275 # @see /usr/include/sys/syslog.h
276 _dlvl2syslvl() {
277 local lvl
278
279 case "$1" in
280 1) lvl=2 ;;
281 2) lvl=3 ;;
282 3) lvl=4 ;;
283 4) lvl=6 ;;
284 5) lvl=7 ;;
285 6) lvl=7 ;;
286 *) return 1 ;;
287 esac
288
289 [ "$syslogfacility" = user ] && echo $((8 + lvl)) || echo $((24 + lvl))
290 }
291
292 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
293 # given message with given level (priority).
294 #
295 # @param lvl Numeric logging level.
296 # @param msg Message.
297 # @retval 0 It's always returned, even if logging failed.
298 #
299 # @note This function is not supposed to be called manually. Please use
300 # dtrace(), ddebug(), or others instead which wrap this one.
301 #
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:
305 #
306 # <tt>X: some message</tt>
307 #
308 # where @c X is the first letter of logging level. See module description for
309 # details on that.
310 #
311 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
312 # following:
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
318 _do_dlog() {
319 local lvlc
320 local lvl="$1"
321 shift
322 lvlc=$(_lvl2char "$lvl") || return 0
323 local msg="$*"
324 local lmsg="$lvlc: $*"
325
326 ((lvl <= stdloglvl)) && printf -- 'dracut[%s]: %s\n' "$lvlc" "$msg" >&2
327
328 if ((lvl <= sysloglvl)); then
329 if [[ "$_dlogfd" ]]; then
330 printf -- "<%s>%s\n" "$(($(_dlvl2syslvl "$lvl") & 7))" "$msg" >&$_dlogfd
331 else
332 logger -t "dracut[$$]" -p "$(_lvl2syspri "$lvl")" -- "$msg"
333 fi
334 fi
335
336 if ((lvl <= fileloglvl)) && [[ -w $logfile ]] && [[ -f $logfile ]]; then
337 echo "$lmsg" >> "$logfile"
338 fi
339
340 ((lvl <= kmsgloglvl)) \
341 && echo "<$(_dlvl2syslvl "$lvl")>dracut[$$] $msg" > /dev/kmsg
342 }
343
344 ## @brief Internal helper function for _do_dlog()
345 #
346 # @param lvl Numeric logging level.
347 # @param msg Message.
348 # @retval 0 It's always returned, even if logging failed.
349 #
350 # @note This function is not supposed to be called manually. Please use
351 # dtrace(), ddebug(), or others instead which wrap this one.
352 #
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
355 # a message.
356 #
357 # This enables:
358 # dwarn "This is a warning"
359 # echo "This is a warning" | dwarn
360 dlog() {
361 [ -z "$maxloglvl" ] && return 0
362 (($1 <= maxloglvl)) || return 0
363
364 if (($# > 1)); then
365 _do_dlog "$@"
366 else
367 while read -r line || [ -n "$line" ]; do
368 _do_dlog "$1" "$line"
369 done
370 fi
371 }
372
373 ## @brief Logs message at TRACE level (6)
374 #
375 # @param msg Message.
376 # @retval 0 It's always returned, even if logging failed.
377 dtrace() {
378 set +x
379 dlog 6 "$@"
380 if [ -n "$debug" ]; then
381 set -x
382 fi
383 }
384
385 ## @brief Logs message at DEBUG level (5)
386 #
387 # @param msg Message.
388 # @retval 0 It's always returned, even if logging failed.
389 ddebug() {
390 set +x
391 dlog 5 "$@"
392 if [ -n "$debug" ]; then
393 set -x
394 fi
395 }
396
397 ## @brief Logs message at INFO level (4)
398 #
399 # @param msg Message.
400 # @retval 0 It's always returned, even if logging failed.
401 dinfo() {
402 set +x
403 dlog 4 "$@"
404 if [ -n "$debug" ]; then
405 set -x
406 fi
407 }
408
409 ## @brief Logs message at WARN level (3)
410 #
411 # @param msg Message.
412 # @retval 0 It's always returned, even if logging failed.
413 dwarn() {
414 set +x
415 dlog 3 "$@"
416 if [ -n "$debug" ]; then
417 set -x
418 fi
419 }
420
421 ## @brief It's an alias to dwarn() function.
422 #
423 # @param msg Message.
424 # @retval 0 It's always returned, even if logging failed.
425 dwarning() {
426 set +x
427 dwarn "$@"
428 if [ -n "$debug" ]; then
429 set -x
430 fi
431 }
432
433 ## @brief Logs message at ERROR level (2)
434 #
435 # @param msg Message.
436 # @retval 0 It's always returned, even if logging failed.
437 derror() {
438 set +x
439 dlog 2 "$@"
440 if [ -n "$debug" ]; then
441 set -x
442 fi
443 }
444
445 ## @brief Logs message at FATAL level (1)
446 #
447 # @param msg Message.
448 # @retval 0 It's always returned, even if logging failed.
449 dfatal() {
450 set +x
451 dlog 1 "$@"
452 if [ -n "$debug" ]; then
453 set -x
454 fi
455 }