dracut-logger: convertion to kernel console log level for kmsg
[dracut.git] / dracut-logger
blob0978d5851bb7523e2fc9ffdeb816eee5dc3896d4
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 one global internal variable @var _maxloglvl which <b>must not</b> be
80 # overwritten. @_maxloglvl is set by dlog_init() and holds maximum logging level
81 # of those three and indicates that dlog_init() was run.
83 # Logging level set by the variable means that messages from this logging level
84 # and above (FATAL is the highest) will be shown. Logging levels may be set
85 # independently for each destination (stderr, syslog, file, kmsg).
87 # @see dlog_init()
90 ## @brief Initializes Dracut Logger.
92 # @retval 1 if something has gone wrong
93 # @retval 0 on success.
95 # @note This function need to be called before any other from this file.
97 # If any of the variables is not set, this function set it to default:
98 # - @var stdloglvl = 4 (info)
99 # - @var sysloglvl = 0 (no logging)
100 # - @var fileloglvl is set to 4 when @var logfile is set too, otherwise it's
101 # - @var kmsgloglvl = 0 (no logging)
102 # set to 0
104 # @warning Function sets global variable @var _maxloglvl for internal use. Don't
105 # overwrite it.
106 dlog_init() {
107 # Skip initialization if it's already done.
108 [ -n "$_maxloglvl" ] && return 0
110 local ret=0; local errmsg
112 [ -z "$stdloglvl" ] && stdloglvl=4
113 [ -z "$sysloglvl" ] && sysloglvl=0
114 [ -z "$kmsgloglvl" ] && kmsgloglvl=0
116 if [ -z "$fileloglvl" ]; then
117 [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
118 elif [ $fileloglvl -gt 0 ]; then
119 ! [ -e "$logfile" ] && >"$logfile"
120 if [ -w "$logfile" -a -f "$logfile" ]; then
121 # Mark new run in the log file
122 echo >>"$logfile"
123 if command -v date >/dev/null; then
124 echo "=== $(date) ===" >>"$logfile"
125 else
126 echo "===============================================" >>"$logfile"
128 echo >>"$logfile"
129 else
130 # We cannot log to file, so turn this facility off.
131 fileloglvl=0
132 ret=1
133 errmsg="'$logfile' is not a writable file"
137 if [ $sysloglvl -gt 0 ]; then
138 if ! [ -c /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
139 then
140 # We cannot log to syslog, so turn this facility off.
141 sysloglvl=0
142 ret=1
143 errmsg="No '/dev/log' or 'logger' not included for syslog logging"
147 local lvl
148 _maxloglvl=0
149 for lvl in $stdloglvl $sysloglvl $fileloglvl; do
150 [ $lvl -gt $_maxloglvl ] && _maxloglvl=$lvl
151 done
152 export _maxloglvl
154 [ -n "$errmsg" ] && derror "$errmsg"
156 return $ret
159 ## @brief Converts numeric logging level to the first letter of level name.
161 # @param lvl Numeric logging level in range from 1 to 6.
162 # @retval 1 if @a lvl is out of range.
163 # @retval 0 if @a lvl is correct.
164 # @result Echoes first letter of level name.
165 _lvl2char() {
166 case "$1" in
167 1) echo F;;
168 2) echo E;;
169 3) echo W;;
170 4) echo I;;
171 5) echo D;;
172 6) echo T;;
173 *) return 1;;
174 esac
177 ## @brief Converts numeric level to logger priority defined by POSIX.2.
179 # @param lvl Numeric logging level in range from 1 to 6.
180 # @retval 1 if @a lvl is out of range.
181 # @retval 0 if @a lvl is correct.
182 # @result Echoes logger priority.
183 _lvl2syslogpri() {
184 case "$1" in
185 1) echo crit;;
186 2) echo error;;
187 3) echo warning;;
188 4) echo info;;
189 5) echo debug;;
190 6) echo debug;;
191 *) return 1;;
192 esac
195 ## @brief Converts dracut-logger numeric level to kernel console log level
197 # @param lvl Numeric logging level in range from 1 to 6.
198 # @retval 1 if @a lvl is out of range.
199 # @retval 0 if @a lvl is correct.
200 # @result Echoes kernel console numeric log level
202 # Conversion is done as follows:
204 # <tt>
205 # none -> KERN_EMERG (0)
206 # FATAL(1) -> KERN_ALERT (1)
207 # none -> KERN_CRIT (2)
208 # ERROR(2) -> KERN_ERR (3)
209 # WARN(3) -> KERN_WARNING (4)
210 # none -> KERN_NOTICE (5)
211 # INFO(4) -> KERN_INFO (6)
212 # DEBUG(5) -> KERN_DEBUG (7)
213 # TRACE(6) /
214 # </tt>
215 _dlvl2klvl() {
216 case "$1" in
217 1) echo 1;;
218 2) echo 3;;
219 3) echo 4;;
220 4) echo 6;;
221 5) echo 7;;
222 6) echo 7;;
223 *) return 1;;
224 esac
227 ## @brief Prints to stderr and/or writes to file, to syslog and/or /dev/kmsg
228 # given message with given level (priority).
230 # @param lvl Numeric logging level.
231 # @param msg Message.
232 # @retval 0 It's always returned, even if logging failed.
234 # @note This function is not supposed to be called manually. Please use
235 # dtrace(), ddebug(), or others instead which wrap this one.
237 # This is core logging function which logs given message to standard error, file
238 # and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
239 # The format is following:
241 # <tt>X: some message</tt>
243 # where @c X is the first letter of logging level. See module description for
244 # details on that.
246 # Message to syslog is sent with tag @c dracut. Priorities are mapped as
247 # following:
248 # - @c FATAL to @c crit
249 # - @c ERROR to @c error
250 # - @c WARN to @c warning
251 # - @c INFO to @c info
252 # - @c DEBUG and @c TRACE both to @c debug
253 _do_dlog() {
254 [ -z "$_maxloglvl" ] && return 0
255 local lvl="$1"; shift
256 local lvlc=$(_lvl2char "$lvl") || return 0
258 [ $lvl -le $_maxloglvl ] || return 0
260 local msg="$lvlc: $*"
262 [ $lvl -le $stdloglvl ] && echo "$msg" >&2
263 if [ $lvl -le $sysloglvl ]; then
264 logger -t "dracut[$$]" -p $(_lvl2syslogpri $lvl) "$msg"
266 if [ $lvl -le $fileloglvl -a -w "$logfile" -a -f "$logfile" ]; then
267 echo "$msg" >>"$logfile"
269 [ $lvl -le $kmsgloglvl ] && \
270 echo "<$(_dlvl2klvl $lvl)>dracut[$$] $msg" >/dev/kmsg
273 ## @brief Internal helper function for _do_dlog()
275 # @param lvl Numeric logging level.
276 # @param msg Message.
277 # @retval 0 It's always returned, even if logging failed.
279 # @note This function is not supposed to be called manually. Please use
280 # dtrace(), ddebug(), or others instead which wrap this one.
282 # This function calls _do_dlog() either with parameter msg, or if
283 # none is given, it will read standard input and will use every line as
284 # a message.
286 # This enables:
287 # dwarn "This is a warning"
288 # echo "This is a warning" | dwarn
289 dlog() {
290 if [ $# -gt 1 ]; then
291 _do_dlog "$@"
292 else
293 while read line; do
294 _do_dlog "$1" "$line"
295 done
297 return 0
300 ## @brief Logs message at TRACE level (6)
302 # @param msg Message.
303 # @retval 0 It's always returned, even if logging failed.
304 dtrace() {
305 dlog 6 "$@"
308 ## @brief Logs message at DEBUG level (5)
310 # @param msg Message.
311 # @retval 0 It's always returned, even if logging failed.
312 ddebug() {
313 dlog 5 "$@"
316 ## @brief Logs message at INFO level (4)
318 # @param msg Message.
319 # @retval 0 It's always returned, even if logging failed.
320 dinfo() {
321 dlog 4 "$@"
324 ## @brief Logs message at WARN level (3)
326 # @param msg Message.
327 # @retval 0 It's always returned, even if logging failed.
328 dwarn() {
329 dlog 3 "$@"
332 ## @brief It's an alias to dwarn() function.
334 # @param msg Message.
335 # @retval 0 It's always returned, even if logging failed.
336 dwarning() {
337 dwarn "$@"
340 ## @brief Logs message at ERROR level (2)
342 # @param msg Message.
343 # @retval 0 It's always returned, even if logging failed.
344 derror() {
345 dlog 2 "$@"
348 ## @brief Logs message at FATAL level (1)
350 # @param msg Message.
351 # @retval 0 It's always returned, even if logging failed.
352 dfatal() {
353 dlog 1 "$@"