]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-logger.sh
dracut-logger.sh: systemd-cat only understands prio 0-7
[thirdparty/dracut.git] / dracut-logger.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 et filetype=sh
4 #
5 # logging faciality module for dracut both at build- and boot-time
6 #
7 # Copyright 2010 Amadeusz Żołnowski <aidecoe@aidecoe.name>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22
23 __DRACUT_LOGGER__=1
24
25
26 ## @brief Logging facility module for dracut both at build- and boot-time.
27 #
28 # @section intro Introduction
29 #
30 # The logger takes a bit from Log4j philosophy. There are defined 6 logging
31 # levels:
32 # - TRACE (6)
33 # The TRACE Level designates finer-grained informational events than the
34 # DEBUG.
35 # - DEBUG (5)
36 # The DEBUG Level designates fine-grained informational events that are most
37 # useful to debug an application.
38 # - INFO (4)
39 # The INFO level designates informational messages that highlight the
40 # progress of the application at coarse-grained level.
41 # - WARN (3)
42 # The WARN level designates potentially harmful situations.
43 # - ERROR (2)
44 # The ERROR level designates error events that might still allow the
45 # application to continue running.
46 # - FATAL (1)
47 # The FATAL level designates very severe error events that will presumably
48 # lead the application to abort.
49 # Descriptions are borrowed from Log4j documentation:
50 # http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
51 #
52 # @section usage Usage
53 #
54 # First of all you have to start with dlog_init() function which initializes
55 # required variables. Don't call any other logging function before that one!
56 # If you're ready with this, you can use following functions which corresponds
57 # clearly to levels listed in @ref intro Introduction. Here they are:
58 # - dtrace()
59 # - ddebug()
60 # - dinfo()
61 # - dwarn()
62 # - derror()
63 # - dfatal()
64 # They take all arguments given as a single message to be logged. See dlog()
65 # function for details how it works. Note that you shouldn't use dlog() by
66 # yourself. It's wrapped with above functions.
67 #
68 # @see dlog_init() dlog()
69 #
70 # @section conf Configuration
71 #
72 # Logging is controlled by following global variables:
73 # - @var stdloglvl - logging level to standard error (console output)
74 # - @var sysloglvl - logging level to syslog (by logger command)
75 # - @var fileloglvl - logging level to file
76 # - @var kmsgloglvl - logging level to /dev/kmsg (only for boot-time)
77 # - @var logfile - log file which is used when @var fileloglvl is higher
78 # than 0
79 # and two global variables: @var maxloglvl and @var syslogfacility which <b>must
80 # not</b> be overwritten. Both are set by dlog_init(). @var maxloglvl holds
81 # maximum logging level of those three and indicates that dlog_init() was run.
82 # @var syslogfacility is set either to 'user' (when building initramfs) or
83 # 'daemon' (when booting).
84 #
85 # Logging level set by the variable means that messages from this logging level
86 # and above (FATAL is the highest) will be shown. Logging levels may be set
87 # independently for each destination (stderr, syslog, file, kmsg).
88 #
89 # @see dlog_init()
90
91
92 ## @brief Initializes dracut Logger.
93 #
94 # @retval 1 if something has gone wrong
95 # @retval 0 on success.
96 #
97 # @note This function need to be called before any other from this file.
98 #
99 # If any of the variables is not set, this function set it to default:
100 # - @var stdloglvl = 4 (info)
101 # - @var sysloglvl = 0 (no logging)
102 # - @var fileloglvl is set to 4 when @var logfile is set too, otherwise it's
103 # - @var kmsgloglvl = 0 (no logging)
104 # set to 0
105 #
106 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
107 # See file doc comment for details.
108 dlog_init() {
109 local __oldumask
110 local ret=0; local errmsg
111 [ -z "$stdloglvl" ] && stdloglvl=4
112 [ -z "$sysloglvl" ] && sysloglvl=0
113 [ -z "$kmsgloglvl" ] && kmsgloglvl=0
114 # Skip initialization if it's already done.
115 [ -n "$maxloglvl" ] && return 0
116
117 if [ -z "$fileloglvl" ]; then
118 [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
119 elif (( $fileloglvl > 0 )); then
120 if [[ $logfile ]]; then
121 __oldumask=$(umask)
122 umask 0377
123 ! [ -e "$logfile" ] && >"$logfile"
124 umask $__oldumask
125 if [ -w "$logfile" -a -f "$logfile" ]; then
126 # Mark new run in the log file
127 echo >>"$logfile"
128 if command -v date >/dev/null; then
129 echo "=== $(date) ===" >>"$logfile"
130 else
131 echo "===============================================" >>"$logfile"
132 fi
133 echo >>"$logfile"
134 else
135 # We cannot log to file, so turn this facility off.
136 fileloglvl=0
137 ret=1
138 errmsg="'$logfile' is not a writable file"
139 fi
140 fi
141 fi
142
143 if (( $UID != 0 )); then
144 kmsgloglvl=0
145 sysloglvl=0
146 fi
147
148 if (( $sysloglvl > 0 )); then
149 if [[ -d /run/systemd/journal ]] \
150 && type -P systemd-cat &>/dev/null \
151 && systemctl --quiet is-active systemd-journald.socket &>/dev/null \
152 && { echo "dracut-$DRACUT_VERSION" | systemd-cat -t 'dracut' &>/dev/null; } ; then
153 readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
154 readonly _systemdcatfile="$_dlogdir/systemd-cat"
155 mkfifo "$_systemdcatfile"
156 readonly _dlogfd=15
157 systemd-cat -t 'dracut' --level-prefix=true <"$_systemdcatfile" &
158 exec 15>"$_systemdcatfile"
159 elif ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null; then
160 # We cannot log to syslog, so turn this facility off.
161 kmsgloglvl=$sysloglvl
162 sysloglvl=0
163 ret=1
164 errmsg="No '/dev/log' or 'logger' included for syslog logging"
165 fi
166 fi
167
168 if (($sysloglvl > 0)) || (($kmsgloglvl > 0 )); then
169 if [ -n "$dracutbasedir" ]; then
170 readonly syslogfacility=user
171 else
172 readonly syslogfacility=daemon
173 fi
174 export syslogfacility
175 fi
176
177 local lvl; local maxloglvl_l=0
178 for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
179 (( $lvl > $maxloglvl_l )) && maxloglvl_l=$lvl
180 done
181 readonly maxloglvl=$maxloglvl_l
182 export maxloglvl
183
184
185 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
186 unset dtrace
187 dtrace() { :; };
188 fi
189
190 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
191 unset ddebug
192 ddebug() { :; };
193 fi
194
195 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
196 unset dinfo
197 dinfo() { :; };
198 fi
199
200 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
201 unset dwarn
202 dwarn() { :; };
203 unset dwarning
204 dwarning() { :; };
205 fi
206
207 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
208 unset derror
209 derror() { :; };
210 fi
211
212 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
213 unset dfatal
214 dfatal() { :; };
215 fi
216
217 [ -n "$errmsg" ] && derror "$errmsg"
218
219 return $ret
220 }
221
222 ## @brief Converts numeric logging level to the first letter of level name.
223 #
224 # @param lvl Numeric logging level in range from 1 to 6.
225 # @retval 1 if @a lvl is out of range.
226 # @retval 0 if @a lvl is correct.
227 # @result Echoes first letter of level name.
228 _lvl2char() {
229 case "$1" in
230 1) echo F;;
231 2) echo E;;
232 3) echo W;;
233 4) echo I;;
234 5) echo D;;
235 6) echo T;;
236 *) return 1;;
237 esac
238 }
239
240 ## @brief Converts numeric level to logger priority defined by POSIX.2.
241 #
242 # @param lvl Numeric logging level in range from 1 to 6.
243 # @retval 1 if @a lvl is out of range.
244 # @retval 0 if @a lvl is correct.
245 # @result Echoes logger priority.
246 _lvl2syspri() {
247 printf $syslogfacility.
248 case "$1" in
249 1) echo crit;;
250 2) echo error;;
251 3) echo warning;;
252 4) echo info;;
253 5) echo debug;;
254 6) echo debug;;
255 *) return 1;;
256 esac
257 }
258
259 ## @brief Converts dracut-logger numeric level to syslog log level
260 #
261 # @param lvl Numeric logging level in range from 1 to 6.
262 # @retval 1 if @a lvl is out of range.
263 # @retval 0 if @a lvl is correct.
264 # @result Echoes kernel console numeric log level
265 #
266 # Conversion is done as follows:
267 #
268 # <tt>
269 # FATAL(1) -> LOG_EMERG (0)
270 # none -> LOG_ALERT (1)
271 # none -> LOG_CRIT (2)
272 # ERROR(2) -> LOG_ERR (3)
273 # WARN(3) -> LOG_WARNING (4)
274 # none -> LOG_NOTICE (5)
275 # INFO(4) -> LOG_INFO (6)
276 # DEBUG(5) -> LOG_DEBUG (7)
277 # TRACE(6) /
278 # </tt>
279 #
280 # @see /usr/include/sys/syslog.h
281 _dlvl2syslvl() {
282 local lvl
283
284 case "$1" in
285 1) lvl=0;;
286 2) lvl=3;;
287 3) lvl=4;;
288 4) lvl=6;;
289 5) lvl=7;;
290 6) lvl=7;;
291 *) return 1;;
292 esac
293
294 [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
295 }
296
297 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
298 # given message with given level (priority).
299 #
300 # @param lvl Numeric logging level.
301 # @param msg Message.
302 # @retval 0 It's always returned, even if logging failed.
303 #
304 # @note This function is not supposed to be called manually. Please use
305 # dtrace(), ddebug(), or others instead which wrap this one.
306 #
307 # This is core logging function which logs given message to standard error, file
308 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
309 # The format is following:
310 #
311 # <tt>X: some message</tt>
312 #
313 # where @c X is the first letter of logging level. See module description for
314 # details on that.
315 #
316 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
317 # following:
318 # - @c FATAL to @c crit
319 # - @c ERROR to @c error
320 # - @c WARN to @c warning
321 # - @c INFO to @c info
322 # - @c DEBUG and @c TRACE both to @c debug
323 _do_dlog() {
324 local lvl="$1"; shift
325 local lvlc=$(_lvl2char "$lvl") || return 0
326 local msg="$*"
327 local lmsg="$lvlc: $*"
328
329 (( $lvl <= $stdloglvl )) && echo "$msg" >&2
330
331 if (( $lvl <= $sysloglvl )); then
332 if [[ "$_dlogfd" ]]; then
333 printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
334 else
335 logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
336 fi
337 fi
338
339 if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
340 echo "$lmsg" >>"$logfile"
341 fi
342
343 (( $lvl <= $kmsgloglvl )) && \
344 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
345 }
346
347 ## @brief Internal helper function for _do_dlog()
348 #
349 # @param lvl Numeric logging level.
350 # @param msg Message.
351 # @retval 0 It's always returned, even if logging failed.
352 #
353 # @note This function is not supposed to be called manually. Please use
354 # dtrace(), ddebug(), or others instead which wrap this one.
355 #
356 # This function calls _do_dlog() either with parameter msg, or if
357 # none is given, it will read standard input and will use every line as
358 # a message.
359 #
360 # This enables:
361 # dwarn "This is a warning"
362 # echo "This is a warning" | dwarn
363 dlog() {
364 [ -z "$maxloglvl" ] && return 0
365 (( $1 <= $maxloglvl )) || return 0
366
367 if (( $# > 1 )); then
368 _do_dlog "$@"
369 else
370 while read line; do
371 _do_dlog "$1" "$line"
372 done
373 fi
374 }
375
376 ## @brief Logs message at TRACE level (6)
377 #
378 # @param msg Message.
379 # @retval 0 It's always returned, even if logging failed.
380 dtrace() {
381 set +x
382 dlog 6 "$@"
383 [ -n "$debug" ] && set -x || :
384 }
385
386 ## @brief Logs message at DEBUG level (5)
387 #
388 # @param msg Message.
389 # @retval 0 It's always returned, even if logging failed.
390 ddebug() {
391 set +x
392 dlog 5 "$@"
393 [ -n "$debug" ] && set -x || :
394 }
395
396 ## @brief Logs message at INFO level (4)
397 #
398 # @param msg Message.
399 # @retval 0 It's always returned, even if logging failed.
400 dinfo() {
401 set +x
402 dlog 4 "$@"
403 [ -n "$debug" ] && set -x || :
404 }
405
406 ## @brief Logs message at WARN level (3)
407 #
408 # @param msg Message.
409 # @retval 0 It's always returned, even if logging failed.
410 dwarn() {
411 set +x
412 dlog 3 "$@"
413 [ -n "$debug" ] && set -x || :
414 }
415
416 ## @brief It's an alias to dwarn() function.
417 #
418 # @param msg Message.
419 # @retval 0 It's always returned, even if logging failed.
420 dwarning() {
421 set +x
422 dwarn "$@"
423 [ -n "$debug" ] && set -x || :
424 }
425
426 ## @brief Logs message at ERROR level (2)
427 #
428 # @param msg Message.
429 # @retval 0 It's always returned, even if logging failed.
430 derror() {
431 set +x
432 dlog 2 "$@"
433 [ -n "$debug" ] && set -x || :
434 }
435
436 ## @brief Logs message at FATAL level (1)
437 #
438 # @param msg Message.
439 # @retval 0 It's always returned, even if logging failed.
440 dfatal() {
441 set +x
442 dlog 1 "$@"
443 [ -n "$debug" ] && set -x || :
444 }