红联Linux门户
Linux帮助

Linux内核log等级与printk打印消息控制

发布时间:2017-03-07 15:08:12来源:linux网站作者:zifehng
预定义的内核log等级
// include/linux/kern_levels.h
#define KERN_SOH    "\001"      /* ASCII Start Of Header */
#define KERN_SOH_ASCII  '\001'
#define KERN_EMERG  KERN_SOH "0"    /* system is unusable */
#define KERN_ALERT  KERN_SOH "1"    /* action must be taken immediately */
#define KERN_CRIT   KERN_SOH "2"    /* critical conditions */
#define KERN_ERR    KERN_SOH "3"    /* error conditions */
#define KERN_WARNING    KERN_SOH "4"    /* warning conditions */
#define KERN_NOTICE KERN_SOH "5"    /* normal but significant condition */
#define KERN_INFO   KERN_SOH "6"    /* informational */
#define KERN_DEBUG  KERN_SOH "7"    /* debug-level messages */
#define KERN_DEFAULT    KERN_SOH "d"    /* the default kernel loglevel */
 
printk打印消息控制
// linux/include/printk.h
/* printk's without a loglevel use this.. */
#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
/* We show everything that is MORE important than this.. */
#define CONSOLE_LOGLEVEL_DEFAULT 7 
#define CONSOLE_LOGLEVEL_MIN     1 
#define CONSOLE_LOGLEVEL_DEFAULT 7 
--------------------------------------------------------
// kernel/printk.c
int console_printk[4] = {
CONSOLE_LOGLEVEL_DEFAULT,   /* console_loglevel */
MESSAGE_LOGLEVEL_DEFAULT,   /* default_message_loglevel */
CONSOLE_LOGLEVEL_MIN,       /* minimum_console_loglevel */
CONSOLE_LOGLEVEL_DEFAULT,   /* default_console_loglevel */
};
--------------------------------------------------------
// linux/include/printk.h
extern int console_printk[];
#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])
 
1.console_loglevel
只有当printk打印消息的log优先级高于console_loglevel时,才能输出到终端上
2.default_message_loglevel
printk打印消息时默认的log等级
3.minimum_console_loglevel
console_loglevel可以被设置的最小值
4.default_console_loglevel
console_loglevel的缺省值
 
通过procfs控制printk打印消息
1.查看当前printk打印消息的log等级
# cat /proc/sys/kernel/printk
# 7 4 1 7
“7 4 1 7” 分别对应console_loglevel、default_message_loglevel、minimum_c onsole_loglevel、default_console_loglevel,意味着只有优先级高于KERN_DEBUG(7)的打印消息才能输出到终端
2.改变console_loglevel
# echo 8 4 1 7 > /proc/sys/kernel/printk
输入“8 4 1 7”改变console_loglevel值,使得所有的打印消息都能输出到终端
3.procfs系列节点
static struct ctl_table kern_table[] = {
......
#if defined CONFIG_PRINTK
{
.procname   = "printk",
.data       = &console_loglevel,
.maxlen     = 4*sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec,
},
{
.procname   = "printk_ratelimit",
.data       = &printk_ratelimit_state.interval,
.maxlen     = sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec_jiffies,
},
{
.procname   = "printk_ratelimit_burst",
.data       = &printk_ratelimit_state.burst,
.maxlen     = sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec,
},
{
.procname   = "printk_delay",
.data       = &printk_delay_msec,
.maxlen     = sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec_minmax,
.extra1     = &zero,
.extra2     = &ten_thousand,
},
{
.procname   = "printk_devkmsg",
.data       = devkmsg_log_str,
.maxlen     = DEVKMSG_STR_MAX_SIZE,
.mode       = 0644,
.proc_handler   = devkmsg_sysctl_set_loglvl,
},
{
.procname   = "dmesg_restrict",
.data       = &dmesg_restrict,
.maxlen     = sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec_minmax_sysadmin,
.extra1     = &zero,
.extra2     = &one,
},
{
.procname   = "kptr_restrict",
.data       = &kptr_restrict,
.maxlen     = sizeof(int),
.mode       = 0644,
.proc_handler   = proc_dointvec_minmax_sysadmin,
.extra1     = &zero,
.extra2     = &two,
},
#endif
......
{ }
};
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28990.html