问题 printk和pr_info之间的区别


两者之间的确切区别是什么 printk 和 pr_info 功能 ?在什么条件下,我应该选择一个而不是另一个?


6227
2018-02-15 07:34


起源

@CL。是的,我的坏。 - LPs
除调试之外,所有这些都是等效的。 - 0andriy


答案:


kernel的printk.h 具有:

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

就像名称一样,pr_info是具有KERN_INFO优先级的printk。


9
2018-02-15 07:49



其实 #define pr_info(fmt, ...) eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__) - LPs
例外是 pr_debug() 与 printk(KERN_DEBUG) 和所有衍生品。 - 0andriy


仔细观察时 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')
    ...
}
...

也可以看看:


1
2018-03-08 04:29