]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-logger.sh
Use systemd-cat for logging on systemd systems, if logfile is empty
[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 if type -P systemd-cat &>/dev/null && (( $UID == 0 )) ; then
142 readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
143 readonly _systemdcatfile="$_dlogdir/systemd-cat"
144 mkfifo "$_systemdcatfile"
145 readonly _dlogfd=15
146 systemd-cat -t 'dracut' <"$_systemdcatfile" &
147 exec 15>"$_systemdcatfile"
148 fi
149 fi
150
151 if (( $sysloglvl > 0 )); then
152 if ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
153 then
154 # We cannot log to syslog, so turn this facility off.
155 sysloglvl=0
156 ret=1
157 errmsg="No '/dev/log' or 'logger' included for syslog logging"
158 fi
159 fi
160
161 if (($sysloglvl > 0)) || (($kmsgloglvl > 0 )); then
162 if [ -n "$dracutbasedir" ]; then
163 readonly syslogfacility=user
164 else
165 readonly syslogfacility=daemon
166 fi
167 export syslogfacility
168 fi
169
170 local lvl; local maxloglvl_l=0
171 for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
172 (( $lvl > $maxloglvl_l )) && maxloglvl_l=$lvl
173 done
174 readonly maxloglvl=$maxloglvl_l
175 export maxloglvl
176
177
178 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)); then
179 unset dtrace
180 dtrace() { :; };
181 fi
182
183 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)); then
184 unset ddebug
185 ddebug() { :; };
186 fi
187
188 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)); then
189 unset dinfo
190 dinfo() { :; };
191 fi
192
193 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)); then
194 unset dwarn
195 dwarn() { :; };
196 unset dwarning
197 dwarning() { :; };
198 fi
199
200 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)); then
201 unset derror
202 derror() { :; };
203 fi
204
205 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)); then
206 unset dfatal
207 dfatal() { :; };
208 fi
209
210 [ -n "$errmsg" ] && derror "$errmsg"
211
212 return $ret
213 }
214
215 ## @brief Converts numeric logging level to the first letter of level name.
216 #
217 # @param lvl Numeric logging level in range from 1 to 6.
218 # @retval 1 if @a lvl is out of range.
219 # @retval 0 if @a lvl is correct.
220 # @result Echoes first letter of level name.
221 _lvl2char() {
222 case "$1" in
223 1) echo F;;
224 2) echo E;;
225 3) echo W;;
226 4) echo I;;
227 5) echo D;;
228 6) echo T;;
229 *) return 1;;
230 esac
231 }
232
233 ## @brief Converts numeric level to logger priority defined by POSIX.2.
234 #
235 # @param lvl Numeric logging level in range from 1 to 6.
236 # @retval 1 if @a lvl is out of range.
237 # @retval 0 if @a lvl is correct.
238 # @result Echoes logger priority.
239 _lvl2syspri() {
240 printf $syslogfacility.
241 case "$1" in
242 1) echo crit;;
243 2) echo error;;
244 3) echo warning;;
245 4) echo info;;
246 5) echo debug;;
247 6) echo debug;;
248 *) return 1;;
249 esac
250 }
251
252 ## @brief Converts dracut-logger numeric level to syslog log level
253 #
254 # @param lvl Numeric logging level in range from 1 to 6.
255 # @retval 1 if @a lvl is out of range.
256 # @retval 0 if @a lvl is correct.
257 # @result Echoes kernel console numeric log level
258 #
259 # Conversion is done as follows:
260 #
261 # <tt>
262 # FATAL(1) -> LOG_EMERG (0)
263 # none -> LOG_ALERT (1)
264 # none -> LOG_CRIT (2)
265 # ERROR(2) -> LOG_ERR (3)
266 # WARN(3) -> LOG_WARNING (4)
267 # none -> LOG_NOTICE (5)
268 # INFO(4) -> LOG_INFO (6)
269 # DEBUG(5) -> LOG_DEBUG (7)
270 # TRACE(6) /
271 # </tt>
272 #
273 # @see /usr/include/sys/syslog.h
274 _dlvl2syslvl() {
275 local lvl
276
277 case "$1" in
278 1) lvl=0;;
279 2) lvl=3;;
280 3) lvl=4;;
281 4) lvl=6;;
282 5) lvl=7;;
283 6) lvl=7;;
284 *) return 1;;
285 esac
286
287 [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
288 }
289
290 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
291 # given message with given level (priority).
292 #
293 # @param lvl Numeric logging level.
294 # @param msg Message.
295 # @retval 0 It's always returned, even if logging failed.
296 #
297 # @note This function is not supposed to be called manually. Please use
298 # dtrace(), ddebug(), or others instead which wrap this one.
299 #
300 # This is core logging function which logs given message to standard error, file
301 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
302 # The format is following:
303 #
304 # <tt>X: some message</tt>
305 #
306 # where @c X is the first letter of logging level. See module description for
307 # details on that.
308 #
309 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
310 # following:
311 # - @c FATAL to @c crit
312 # - @c ERROR to @c error
313 # - @c WARN to @c warning
314 # - @c INFO to @c info
315 # - @c DEBUG and @c TRACE both to @c debug
316 _do_dlog() {
317 local lvl="$1"; shift
318 local lvlc=$(_lvl2char "$lvl") || return 0
319 local msg="$*"
320 local lmsg="$lvlc: $*"
321
322 (( $lvl <= $stdloglvl )) && echo "$msg" >&2
323 if (( $lvl <= $sysloglvl )); then
324 logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) "$msg"
325 fi
326
327 if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
328 echo "$lmsg" >>"$logfile"
329 fi
330
331 if (( $lvl <= $fileloglvl )) && [[ "$_dlogfd" ]]; then
332 echo "<$(_dlvl2syslvl $lvl)>$msg" >&$_dlogfd
333 fi
334
335 (( $lvl <= $kmsgloglvl )) && \
336 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
337 }
338
339 ## @brief Internal helper function for _do_dlog()
340 #
341 # @param lvl Numeric logging level.
342 # @param msg Message.
343 # @retval 0 It's always returned, even if logging failed.
344 #
345 # @note This function is not supposed to be called manually. Please use
346 # dtrace(), ddebug(), or others instead which wrap this one.
347 #
348 # This function calls _do_dlog() either with parameter msg, or if
349 # none is given, it will read standard input and will use every line as
350 # a message.
351 #
352 # This enables:
353 # dwarn "This is a warning"
354 # echo "This is a warning" | dwarn
355 dlog() {
356 [ -z "$maxloglvl" ] && return 0
357 (( $1 <= $maxloglvl )) || return 0
358
359 if (( $# > 1 )); then
360 _do_dlog "$@"
361 else
362 while read line; do
363 _do_dlog "$1" "$line"
364 done
365 fi
366 }
367
368 ## @brief Logs message at TRACE level (6)
369 #
370 # @param msg Message.
371 # @retval 0 It's always returned, even if logging failed.
372 dtrace() {
373 set +x
374 dlog 6 "$@"
375 [ -n "$debug" ] && set -x || :
376 }
377
378 ## @brief Logs message at DEBUG level (5)
379 #
380 # @param msg Message.
381 # @retval 0 It's always returned, even if logging failed.
382 ddebug() {
383 set +x
384 dlog 5 "$@"
385 [ -n "$debug" ] && set -x || :
386 }
387
388 ## @brief Logs message at INFO level (4)
389 #
390 # @param msg Message.
391 # @retval 0 It's always returned, even if logging failed.
392 dinfo() {
393 set +x
394 dlog 4 "$@"
395 [ -n "$debug" ] && set -x || :
396 }
397
398 ## @brief Logs message at WARN level (3)
399 #
400 # @param msg Message.
401 # @retval 0 It's always returned, even if logging failed.
402 dwarn() {
403 set +x
404 dlog 3 "$@"
405 [ -n "$debug" ] && set -x || :
406 }
407
408 ## @brief It's an alias to dwarn() function.
409 #
410 # @param msg Message.
411 # @retval 0 It's always returned, even if logging failed.
412 dwarning() {
413 set +x
414 dwarn "$@"
415 [ -n "$debug" ] && set -x || :
416 }
417
418 ## @brief Logs message at ERROR level (2)
419 #
420 # @param msg Message.
421 # @retval 0 It's always returned, even if logging failed.
422 derror() {
423 set +x
424 dlog 2 "$@"
425 [ -n "$debug" ] && set -x || :
426 }
427
428 ## @brief Logs message at FATAL level (1)
429 #
430 # @param msg Message.
431 # @retval 0 It's always returned, even if logging failed.
432 dfatal() {
433 set +x
434 dlog 1 "$@"
435 [ -n "$debug" ] && set -x || :
436 }