Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40379
  • 博文数量: 10
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 20:34
文章分类

全部博文(10)

文章存档

2010年(10)

我的朋友

分类: LINUX

2010-08-10 23:53:58

When using printk a loglevel gets defined. printk(KERN_WARNING "Help\n");. Valid loglevels are:
  • KERN_EMERG
  • KERN_ALERT
  • KERN_CRIT
  • KERN_ERR
  • KERN_WARNING
  • KERN_NOTICE
  • KERN_INFO
  • KERN_DEBUG
All loglevels are, after macro expansion, an integer ranging from 0 to 7 (0 = highest priority). When no loglevel has been specified it defaults to
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.
So the easiest way to change the loglevel is simply:

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
阅读(459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~