printk
时间:2010-08-10 来源:pathscale
When using printk a loglevel gets defined. printk(KERN_WARNING "Help\n");. Valid loglevels are:
DEFAULT_MESSAGE_LOGLEVEL. This is specified in kernel/printk.c:
/* printk's without a loglevel use this.. */
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
Depending on the priority messages get logged to the console or discarded. In the following case KERN_DEBUG messages will be discarded (kernel/printk.c):
/* We show everything that is MORE important than this.. */
#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
How messages are logged depends on the running loggers (man klogd and man syslogd) and their configurations. Messages can be intercepted by cat'ing /proc/kmsg. The messages are proceeded by the used loglevel.
psychotic:/home/helios# cat /proc/kmsg
<4> ACPI-0352: *** Error: Looking up [Z005] in namespace, AE_NOT_FOUND
<4>search_node c14d0440 start_node c14d0440 return_node 00000000
<4> ACPI-1138: *** Error: Method execution failed [\_SB_.BAT1._BST] (Node c14d0340), AE_NOT_FOUND
(Luckily I haven't fixed the buggy ACPI subsystem on my laptop so I can trigger kernel messages on the fly ;-) ).
Changing the console_loglevel can be done by using the -c parameter on klogd, or by syslog() from code. See man 2 syslog for more information. A third way is by accessing the proc filesystem.
The digits are:
The kernel provides a function int printk_ratelimit(void); which can be used to avoid flooding logs with messages. If this function returns zero nothing should be printed. The behaviour of this function can be controlled by modifying the values in /proc/sys/kernel/printk_ratelimit and /proc/sys/kernel/printk_ratelimit_burst which are the number of seconds to wait before re-enabling messages and the number of messages accepted before ratelimiting.
Disadvantages of printk:
- KERN_EMERG
- KERN_ALERT
- KERN_CRIT
- KERN_ERR
- KERN_WARNING
- KERN_NOTICE
- KERN_INFO
- KERN_DEBUG
DEFAULT_MESSAGE_LOGLEVEL. This is specified in kernel/printk.c:
/* printk's without a loglevel use this.. */
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
Depending on the priority messages get logged to the console or discarded. In the following case KERN_DEBUG messages will be discarded (kernel/printk.c):
/* We show everything that is MORE important than this.. */
#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
How messages are logged depends on the running loggers (man klogd and man syslogd) and their configurations. Messages can be intercepted by cat'ing /proc/kmsg. The messages are proceeded by the used loglevel.
psychotic:/home/helios# cat /proc/kmsg
<4> ACPI-0352: *** Error: Looking up [Z005] in namespace, AE_NOT_FOUND
<4>search_node c14d0440 start_node c14d0440 return_node 00000000
<4> ACPI-1138: *** Error: Method execution failed [\_SB_.BAT1._BST] (Node c14d0340), AE_NOT_FOUND
(Luckily I haven't fixed the buggy ACPI subsystem on my laptop so I can trigger kernel messages on the fly ;-) ).
Changing the console_loglevel can be done by using the -c parameter on klogd, or by syslog() from code. See man 2 syslog for more information. A third way is by accessing the proc filesystem.
psychotic:/usr/src/linux-2.6.11.11/kernel# cat /proc/sys/kernel/printk
7 4 1 7
The digits are:
- Current loglevel
- Default level for messages without a warning (DEFAULT_MESSAGE_LOGLEVEL)
- Minimum allowed (MINIMUM_CONSOLE_LOGLEVEL)
- Boottime default loglevel.
psychotic:/home/helios/vuilbak/examples/misc-progs# echo 8 > /proc/sys/kernel/printk
The kernel provides a function int printk_ratelimit(void); which can be used to avoid flooding logs with messages. If this function returns zero nothing should be printed. The behaviour of this function can be controlled by modifying the values in /proc/sys/kernel/printk_ratelimit and /proc/sys/kernel/printk_ratelimit_burst which are the number of seconds to wait before re-enabling messages and the number of messages accepted before ratelimiting.
helios@psychotic:~$ cat /proc/sys/kernel/printk_ratelimit
5
helios@psychotic:~$ cat /proc/sys/kernel/printk_ratelimit_burst
10
Disadvantages of printk:
- It slows everything down (each printk gets logged and causes a harddisk operation, this can however be solved by prefixing the name of the logfile in syslog.conf by a hyphen (-), this causes the files not to sync after each event)
- It fills up your logs
- Possibility to flooding (when not ratelimiting)
- It is not clean to have kernel code spawning calls
相关阅读 更多 +