【发布时间】:2017-07-03 17:57:31
【问题描述】:
printk 和 pr_info 函数之间的确切区别是什么?在什么情况下,我应该选择一个而不是另一个?
【问题讨论】:
-
@CL。是的,我的错。
-
除了调试一个之外,所有这些都是等效的。
标签: c linux-kernel kernel-module printk
printk 和 pr_info 函数之间的确切区别是什么?在什么情况下,我应该选择一个而不是另一个?
【问题讨论】:
标签: c linux-kernel kernel-module printk
#define pr_info(fmt,arg...) \
printk(KERN_INFO fmt,##arg)
就像名字一样,pr_info 是具有 KERN_INFO 优先级的 printk。
【讨论】:
#define pr_info(fmt, ...) eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
pr_debug() vs. printk(KERN_DEBUG) 和所有派生词。
当专门查看pr_info时,定义将依次使用printk(KERN_INFO ...(如barcelona_delpy的answer中所述);但是,答案的源 sn-p 似乎排除了格式包装器pr_fmt(fmt)(如 LPs 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')
...
}
...
另见:
【讨论】: