apply "ro" and "rw" options from cmdline to / mount
[dracut.git] / dracut-logger.sh
blobdbe26eed5593f9b25c510c84e802670f09aeeb0c
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 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
117 if [ -z "$fileloglvl" ]; then
118 [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
119 elif (( $fileloglvl >= 0 )); then
120 __oldumask=$(umask)
121 umask 0377
122 ! [ -e "$logfile" ] && >"$logfile"
123 umask $__oldumask
124 if [ -w "$logfile" -a -f "$logfile" ]; then
125 # Mark new run in the log file
126 echo >>"$logfile"
127 if command -v date >/dev/null; then
128 echo "=== $(date) ===" >>"$logfile"
129 else
130 echo "===============================================" >>"$logfile"
132 echo >>"$logfile"
133 else
134 # We cannot log to file, so turn this facility off.
135 fileloglvl=0
136 ret=1
137 errmsg="'$logfile' is not a writable file"
141 if (( $sysloglvl >= 0 )); then
142 if ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
143 then
144 # We cannot log to syslog, so turn this facility off.
145 sysloglvl=0
146 ret=1
147 errmsg="No '/dev/log' or 'logger' included for syslog logging"
151 if (($sysloglvl >= 0)) || (($kmsgloglvl >= 0 )); then
152 if [ -n "$dracutbasedir" ]; then
153 readonly syslogfacility=user
154 else
155 readonly syslogfacility=daemon
157 export syslogfacility
160 local lvl; local maxloglvl_l=0
161 for lvl in $stdloglvl $sysloglvl $fileloglvl $kmsgloglvl; do
162 (( $lvl > $maxloglvl_l )) && maxloglvl_l=$lvl
163 done
164 readonly maxloglvl=$maxloglvl_l
165 export maxloglvl
168 if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)); then
169 unset dtrace
170 dtrace() { :; };
173 if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)); then
174 unset ddebug
175 ddebug() { :; };
178 if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)); then
179 unset dinfo
180 dinfo() { :; };
183 if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)); then
184 unset dwarn
185 dwarn() { :; };
186 unset dwarning
187 dwarning() { :; };
190 if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)); then
191 unset derror
192 derror() { :; };
195 if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)); then
196 unset dfatal
197 dfatal() { :; };
200 [ -n "$errmsg" ] && derror "$errmsg"
202 return $ret
205 ## @brief Converts numeric logging level to the first letter of level name.
207 # @param lvl Numeric logging level in range from 1 to 6.
208 # @retval 1 if @a lvl is out of range.
209 # @retval 0 if @a lvl is correct.
210 # @result Echoes first letter of level name.
211 _lvl2char() {
212 case "$1" in
213 1) echo F;;
214 2) echo E;;
215 3) echo W;;
216 4) echo I;;
217 5) echo D;;
218 6) echo T;;
219 *) return 1;;
220 esac
223 ## @brief Converts numeric level to logger priority defined by POSIX.2.
225 # @param lvl Numeric logging level in range from 1 to 6.
226 # @retval 1 if @a lvl is out of range.
227 # @retval 0 if @a lvl is correct.
228 # @result Echoes logger priority.
229 _lvl2syspri() {
230 printf $syslogfacility.
231 case "$1" in
232 1) echo crit;;
233 2) echo error;;
234 3) echo warning;;
235 4) echo info;;
236 5) echo debug;;
237 6) echo debug;;
238 *) return 1;;
239 esac
242 ## @brief Converts dracut-logger numeric level to syslog log level
244 # @param lvl Numeric logging level in range from 1 to 6.
245 # @retval 1 if @a lvl is out of range.
246 # @retval 0 if @a lvl is correct.
247 # @result Echoes kernel console numeric log level
249 # Conversion is done as follows:
251 # <tt>
252 # FATAL(1) -> LOG_EMERG (0)
253 # none -> LOG_ALERT (1)
254 # none -> LOG_CRIT (2)
255 # ERROR(2) -> LOG_ERR (3)
256 # WARN(3) -> LOG_WARNING (4)
257 # none -> LOG_NOTICE (5)
258 # INFO(4) -> LOG_INFO (6)
259 # DEBUG(5) -> LOG_DEBUG (7)
260 # TRACE(6) /
261 # </tt>
263 # @see /usr/include/sys/syslog.h
264 _dlvl2syslvl() {
265 local lvl
267 case "$1" in
268 1) lvl=0;;
269 2) lvl=3;;
270 3) lvl=4;;
271 4) lvl=6;;
272 5) lvl=7;;
273 6) lvl=7;;
274 *) return 1;;
275 esac
277 [ "$syslogfacility" = user ] && echo $((8+$lvl)) || echo $((24+$lvl))
280 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
281 # given message with given level (priority).
283 # @param lvl Numeric logging level.
284 # @param msg Message.
285 # @retval 0 It's always returned, even if logging failed.
287 # @note This function is not supposed to be called manually. Please use
288 # dtrace(), ddebug(), or others instead which wrap this one.
290 # This is core logging function which logs given message to standard error, file
291 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
292 # The format is following:
294 # <tt>X: some message</tt>
296 # where @c X is the first letter of logging level. See module description for
297 # details on that.
299 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
300 # following:
301 # - @c FATAL to @c crit
302 # - @c ERROR to @c error
303 # - @c WARN to @c warning
304 # - @c INFO to @c info
305 # - @c DEBUG and @c TRACE both to @c debug
306 _do_dlog() {
307 local lvl="$1"; shift
308 local lvlc=$(_lvl2char "$lvl") || return 0
309 local msg="$lvlc: $*"
311 (( $lvl <= $stdloglvl )) && echo "$msg" >&2
312 if (( $lvl <= $sysloglvl )); then
313 logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) "$msg"
315 if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
316 echo "$msg" >>"$logfile"
318 (( $lvl <= $kmsgloglvl )) && \
319 echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
322 ## @brief Internal helper function for _do_dlog()
324 # @param lvl Numeric logging level.
325 # @param msg Message.
326 # @retval 0 It's always returned, even if logging failed.
328 # @note This function is not supposed to be called manually. Please use
329 # dtrace(), ddebug(), or others instead which wrap this one.
331 # This function calls _do_dlog() either with parameter msg, or if
332 # none is given, it will read standard input and will use every line as
333 # a message.
335 # This enables:
336 # dwarn "This is a warning"
337 # echo "This is a warning" | dwarn
338 dlog() {
339 [ -z "$maxloglvl" ] && return 0
340 (( $1 <= $maxloglvl )) || return 0
342 if (( $# > 1 )); then
343 _do_dlog "$@"
344 else
345 while read line; do
346 _do_dlog "$1" "$line"
347 done
351 ## @brief Logs message at TRACE level (6)
353 # @param msg Message.
354 # @retval 0 It's always returned, even if logging failed.
355 dtrace() {
356 set +x
357 dlog 6 "$@"
358 [ -n "$debug" ] && set -x || :
361 ## @brief Logs message at DEBUG level (5)
363 # @param msg Message.
364 # @retval 0 It's always returned, even if logging failed.
365 ddebug() {
366 set +x
367 dlog 5 "$@"
368 [ -n "$debug" ] && set -x || :
371 ## @brief Logs message at INFO level (4)
373 # @param msg Message.
374 # @retval 0 It's always returned, even if logging failed.
375 dinfo() {
376 set +x
377 dlog 4 "$@"
378 [ -n "$debug" ] && set -x || :
381 ## @brief Logs message at WARN level (3)
383 # @param msg Message.
384 # @retval 0 It's always returned, even if logging failed.
385 dwarn() {
386 set +x
387 dlog 3 "$@"
388 [ -n "$debug" ] && set -x || :
391 ## @brief It's an alias to dwarn() function.
393 # @param msg Message.
394 # @retval 0 It's always returned, even if logging failed.
395 dwarning() {
396 set +x
397 dwarn "$@"
398 [ -n "$debug" ] && set -x || :
401 ## @brief Logs message at ERROR level (2)
403 # @param msg Message.
404 # @retval 0 It's always returned, even if logging failed.
405 derror() {
406 set +x
407 dlog 2 "$@"
408 [ -n "$debug" ] && set -x || :
411 ## @brief Logs message at FATAL level (1)
413 # @param msg Message.
414 # @retval 0 It's always returned, even if logging failed.
415 dfatal() {
416 set +x
417 dlog 1 "$@"
418 [ -n "$debug" ] && set -x || :