printk 和 pr_info 的区别

问题描述 投票:0回答:2

printk
pr_info
函数之间的确切区别是什么?在什么条件下,我应该选择其中一个而不是另一个?

c linux-kernel kernel-module printk
2个回答
34
投票

内核的 printk.h 有:

#define pr_info(fmt,arg...) \
    printk(KERN_INFO fmt,##arg)

就像名字一样,

pr_info()
printk()~ with the 
KERN_INFO`优先级。


11
投票

当具体查看

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')
    ...
}
...

另请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.