两者之间的确切区别是什么 printk
和 pr_info
功能 ?在什么条件下,我应该选择一个而不是另一个?
两者之间的确切区别是什么 printk
和 pr_info
功能 ?在什么条件下,我应该选择一个而不是另一个?
该 kernel的printk.h 具有:
#define pr_info(fmt,arg...) \
printk(KERN_INFO fmt,##arg)
就像名称一样,pr_info是具有KERN_INFO优先级的printk。
仔细观察时 pr_info
,定义将依次使用 printk(KERN_INFO ...
(如barcelona_delpy所述 回答);但是,答案的源代码段似乎排除了格式包装器 pr_fmt(fmt)
(如LP所述 评论)。
区别 为什么你可以使用 pr_info
过度 printk(KERN_INFO ...
是您可以设置的自定义格式。如果您希望在模块中添加消息前缀 printk
,一种方法是在每一行显式添加你的前缀:
printk(KERN_INFO "mymodule: hello there\n");
// outputs "mymodule: hello there"
要么:
printk(KERN_INFO KBUILD_MODNAME " hello there\n");
// outputs "mymodule: hello there"
但是,如果你使用 pr_info
(和别的 pr_*
功能),您可以重新定义格式并简单地使用 pr_info
没有额外的工作:
... (includes)
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
...
{
...
pr_err("hello there\n");
// outputs "mymodule: hello there" (assuming module is named 'mymodule')
...
}
...
也可以看看: