printk
和 pr_info
函数之间的确切区别是什么?在什么条件下,我应该选择其中一个而不是另一个?
内核的 printk.h 有:
#define pr_info(fmt,arg...) \
printk(KERN_INFO fmt,##arg)
就像名字一样,
pr_info()
是printk()~ with the
KERN_INFO`优先级。
当具体查看
pr_info
时,定义将依次使用printk(KERN_INFO ...
(如barcelona_delpy的answer中所述);然而,答案的源代码片段似乎排除了格式包装器pr_fmt(fmt)
(如 LP comment所述)。
与使用 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')
...
}
...
另请参阅: