TEST-50-MULTINIC/client-init: drop to shell, if "rd.shell"
[dracut.git] / dracut-logger
blob3bcce8387fac5dbfdbccd7fd8286ceccee3f2c2d
1 #!/bin/sh
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 et filetype=sh
5 # logging faciality module for dracut both at build- and boot-time
7 # Copyright 2010 Amadeusz Żołnowski <aidecoe@aidecoe.name>
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.
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.
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/>.
23 __DRACUT_LOGGER__=1
26 ## @brief Logging facility module for Dracut both at build- and boot-time.
28 # @section intro Introduction
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
52 # @section usage Usage
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.
68 # @see dlog_init() dlog()
70 # @section conf Configuration
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).
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).
89 # @see dlog_init()
92 ## @brief Initializes Dracut Logger.
94 # @retval 1 if something has gone wrong
95 # @retval 0 on success.
97 # @note This function need to be called before any other from this file.
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
106 # @warning Function sets global variables @var maxloglvl and @syslogfacility.
107 # See file doc comment for details.
108 dlog_init() {
109 # Skip initialization if it's already done.
110 [ -n "$maxloglvl" ] && return 0
112 local ret=0; local errmsg
114 [ -z "$stdloglvl" ] && stdloglvl=4
115 [ -z "$sysloglvl" ] && sysloglvl=0
116 [ -z "$kmsgloglvl" ] && kmsgloglvl=0
118 if [ -z "$fileloglvl" ]; then
119 [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
120 elif [ $fileloglvl -gt 0 ]; then
121 ! [ -e "$logfile" ] && >"$logfile"
122 if [ -w "$logfile" -a -f "$logfile" ]; then
123 # Mark new run in the log file
124 echo >>"$logfile"
125 if command -v date >/dev/null; then
126 echo "=== $(date) ===" >>"$logfile"
127 else
128 echo "===============================================" >>"$logfile"
130 echo >>"$logfile"
131 else
132 # We cannot log to file, so turn this facility off.
133 fileloglvl=0
134 ret=1
135 errmsg="'$logfile' is not a writable file"
139 if [ $sysloglvl -gt 0 ]; then
140 if ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
141 then
142 # We cannot log to syslog, so turn this facility off.
143 sysloglvl=0
144 ret=1
145 errmsg="No '/dev/log' or 'logger' included for syslog logging"
149 if [ $sysloglvl -gt 0 -o $kmsgloglvl -gt 0 ]; then
150 if [ -n "$dracutbasedir" ]; then
151 readonly syslogfacility=user
152 else
153 readonly syslogfacility=daemon
155 export syslogfacility
158 local lvl; local maxloglvl_l=0
159 for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
160 [ $lvl -gt $maxloglvl_l ] && maxloglvl_l=$lvl
161 done
162 readonly maxloglvl=$maxloglvl_l
163 export maxloglvl
165 [ -n "$errmsg" ] && derror "$errmsg"
167 return $ret
170 ## @brief Converts numeric logging level to the first letter of level name.
172 # @param lvl Numeric logging level in range from 1 to 6.
173 # @retval 1 if @a lvl is out of range.
174 # @retval 0 if @a lvl is correct.
175 # @result Echoes first letter of level name.
176 _lvl2char() {
177 case "$1" in
178 1) echo F;;
179 2) echo E;;
180 3) echo W;;
181 4) echo I;;
182 5) echo D;;
183 6) echo T;;
184 *) return 1;;
185 esac
188 ## @brief Converts numeric level to logger priority defined by POSIX.2.
190 # @param lvl Numeric logging level in range from 1 to 6.
191 # @retval 1 if @a lvl is out of range.
192 # @retval 0 if @a lvl is correct.
193 # @result Echoes logger priority.
194 _lvl2syspri() {
195 printf $syslogfacility.
196 case "$1" in
197 1) echo crit;;
198 2) echo error;;
199 3) echo warning;;
200 4) echo info;;
201 5) echo debug;;
202 6) echo debug;;
203 *) return 1;;
204 esac
207 ## @brief Converts dracut-logger numeric level to syslog log level
209 # @param lvl Numeric logging level in range from 1 to 6.
210 # @retval 1 if @a lvl is out of range.
211 # @retval 0 if @a lvl is correct.
212 # @result Echoes kernel console numeric log level
214 # Conversion is done as follows:
216 # <tt>
217 # FATAL(1) -> LOG_EMERG (0)
218 # none -> LOG_ALERT (1)
219 # none -> LOG_CRIT (2)
220 # ERROR(2) -> LOG_ERR (3)
221 # WARN(3) -> LOG_WARNING (4)
222 # none -> LOG_NOTICE (5)
223 # INFO(4) -> LOG_INFO (6)
224 # DEBUG(5) -> LOG_DEBUG (7)
225 # TRACE(6) /
226 # </tt>
228 # @see /usr/include/sys/syslog.h
229 _dlvl2syslvl() {
230 local lvl
232 case "$1" in
233 1) lvl=0;;
234 2) lvl=3;;
235 3) lvl=4;;
236 4) lvl=6;;
237 5) lvl=7;;
238 6) lvl=7;;
239 *) return 1;;
240 esac
242 [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
245 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
246 # given message with given level (priority).
248 # @param lvl Numeric logging level.
249 # @param msg Message.
250 # @retval 0 It's always returned, even if logging failed.
252 # @note This function is not supposed to be called manually. Please use
253 # dtrace(), ddebug(), or others instead which wrap this one.
255 # This is core logging function which logs given message to standard error, file
256 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
257 # The format is following:
259 # <tt>X: some message</tt>
261 # where @c X is the first letter of logging level. See module description for
262 # details on that.
264 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
265 # following:
266 # - @c FATAL to @c crit
267 # - @c ERROR to @c error
268 # - @c WARN to @c warning
269 # - @c INFO to @c info
270 # - @c DEBUG and @c TRACE both to @c debug
271 _do_dlog() {
272 [ -z "$maxloglvl" ] && return 0
273 local lvl="$1"; shift
274 local lvlc=$(_lvl2char "$lvl") || return 0
276 [ $lvl -le $maxloglvl ] || return 0
278 local msg="$lvlc: $*"
280 [ $lvl -le $stdloglvl ] && echo "$msg" >&2
281 if [ $lvl -le $sysloglvl ]; then
282 logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) "$msg"
284 if [ $lvl -le $fileloglvl -a -w "$logfile" -a -f "$logfile" ]; then
285 echo "$msg" >>"$logfile"
287 [ $lvl -le $kmsgloglvl ] && \
288 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
291 ## @brief Internal helper function for _do_dlog()
293 # @param lvl Numeric logging level.
294 # @param msg Message.
295 # @retval 0 It's always returned, even if logging failed.
297 # @note This function is not supposed to be called manually. Please use
298 # dtrace(), ddebug(), or others instead which wrap this one.
300 # This function calls _do_dlog() either with parameter msg, or if
301 # none is given, it will read standard input and will use every line as
302 # a message.
304 # This enables:
305 # dwarn "This is a warning"
306 # echo "This is a warning" | dwarn
307 dlog() {
308 if [ $# -gt 1 ]; then
309 _do_dlog "$@"
310 else
311 while read line; do
312 _do_dlog "$1" "$line"
313 done
315 [ -n "$debug" ] && set -x || :
318 ## @brief Logs message at TRACE level (6)
320 # @param msg Message.
321 # @retval 0 It's always returned, even if logging failed.
322 dtrace() {
323 set +x
324 dlog 6 "$@"
327 ## @brief Logs message at DEBUG level (5)
329 # @param msg Message.
330 # @retval 0 It's always returned, even if logging failed.
331 ddebug() {
332 set +x
333 dlog 5 "$@"
336 ## @brief Logs message at INFO level (4)
338 # @param msg Message.
339 # @retval 0 It's always returned, even if logging failed.
340 dinfo() {
341 set +x
342 dlog 4 "$@"
345 ## @brief Logs message at WARN level (3)
347 # @param msg Message.
348 # @retval 0 It's always returned, even if logging failed.
349 dwarn() {
350 set +x
351 dlog 3 "$@"
354 ## @brief It's an alias to dwarn() function.
356 # @param msg Message.
357 # @retval 0 It's always returned, even if logging failed.
358 dwarning() {
359 set +x
360 dwarn "$@"
363 ## @brief Logs message at ERROR level (2)
365 # @param msg Message.
366 # @retval 0 It's always returned, even if logging failed.
367 derror() {
368 set +x
369 dlog 2 "$@"
372 ## @brief Logs message at FATAL level (1)
374 # @param msg Message.
375 # @retval 0 It's always returned, even if logging failed.
376 dfatal() {
377 set +x
378 dlog 1 "$@"