Chinaunix首页 | 论坛 | 博客
  • 博客访问: 250512
  • 博文数量: 65
  • 博客积分: 2599
  • 博客等级: 少校
  • 技术积分: 710
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-04 10:49
文章分类

全部博文(65)

文章存档

2015年(4)

2013年(2)

2012年(4)

2011年(51)

2010年(4)

分类: LINUX

2011-03-18 16:23:23

1. printk 输出消息优先级别
#define KERN_EMERG    "<0>"    /* system is unusable */
#define KERN_ALERT    "<1>"    /* action must be taken immediately */
#define KERN_CRIT     "<2>"    /* critical conditions */
#define KERN_ERR      "<3>"    /* error conditions */
#define KERN_WARNING  "<4>"    /* warning conditions */
#define KERN_NOTICE   "<5>"    /* normal but significant */
#define KERN_INFO     "<6>"    /* informational */
#define KERN_DEBUG    "<7>"    /* debug-level messages */

2. Kernel默认消息级别
 /* printk's without a loglevel use this.. */
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */

3. loglevel
根据日志的级别,kernel可能直接将消息打印到当前控制台上,这取决于printk的日志级别和
console_loglevel的值。
kernel定义了一个console_printk数组:
int console_printk[4] = {//printk.c
DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
};

#define console_loglevel (console_printk[0])
#define default_message_loglevel (console_printk[1])
#define minimum_console_loglevel (console_printk[2])
#define default_console_loglevel (console_printk[3])

#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */

这样看来,只要是大于KERN_DEBUG的优先级都会打印到console上。
但是,为什么我们自己调用的printk并没有直接输出到当前的用户console上呢?
这是因为klogd和syslogd的存在,这两个进程,或者只有其中之一,会将printk的全部输出保存在/var.log/messages之中。所以用户的console不会被内核printk扰乱。
    那么由此可见,我们的系统中如果存在klogd或者syslogd的话,输出的消息就不是驱动程序开发者可以直接控制的了。而,大部分开发中使用的发行版本都存在至少一个,所以,个人认为只要知道printk的优先级别,并且知道console_loglevel ,在使用printk的时候,让优先级别小于这个值即可。
    在2.6.37.2中,这个console_loglevel 是7,那么也就是说,只要优先级高于KERN_DEBUG的都可以保证一定会输出,不过这里有一个弄不明白的地方:
问题:
@localhost linux-2.6.37.2]$ cat /proc/sys/kernel/printk
3 4 1 7
3:console_loglevel   
4:default_message_loglevel
1:minimum_console_level
7: default_console_loglevel

/proc/sys/kernel/printk:
              The  four  values  in  this  file  are  console_loglevel,  default_message_loglevel,  minimum_console_level,  and  default_console_loglevel.   These values influence printk() behavior when printing or logging error messages.  See syslog(2) for more info on the different loglevels.  Messages with a higher priority than console_loglevel will be printed to  the  console.   Messages without  an  explicit  priority  will  be printed with priority default_message_level.  minimum_console_loglevel is the minimum  (highest) value to which console_loglevel can be set.  default_console_loglevel is the default value for console_loglevel.
根据解释,当前的console_loglevel是3,那么只有
#define KERN_EMERG    "<0>"    /* system is unusable */
#define KERN_ALERT    "<1>"    /* action must be taken immediately */
#define KERN_CRIT     "<2>"    /* critical conditions */
才会被输出到console,再加上klogd,有点不明白了!
[root@localhost test]# cat /var/log/messages
Mar 18 17:33:23 localhost kernel: This is kernel alert
Mar 18 17:33:23 localhost kernel: This is kernel crit
Mar 18 17:33:23 localhost kernel: This is kernel err
Mar 18 17:33:23 localhost kernel: This is kernel warning
Mar 18 17:33:23 localhost kernel: This is kernel notice
Mar 18 17:33:23 localhost kernel: This is kernel info

[root@localhost test]# dmesg
This is kernel alert
This is kernel crit
This is kernel err
This is kernel warning
This is kernel notice
This is kernel info
This is kernel debug
这都输出了!
希望精通这方面只是的朋友,解释以下原因。谢谢


阅读(1020) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~