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