【问题标题】:Difference between printk and pr_infoprintk 和 pr_info 的区别
【发布时间】:2017-07-03 17:57:31
【问题描述】:

printkpr_info 函数之间的确切区别是什么?在什么情况下,我应该选择一个而不是另一个?

【问题讨论】:

  • @CL。是的,我的错。
  • 除了调试一个之外,所有这些都是等效的。

标签: c linux-kernel kernel-module printk


【解决方案1】:

kernel's printk.h 有:

#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) 和所有派生词。
【解决方案2】:

当专门查看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')
    ...
}
...

另见:

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 2011-07-04
    • 2012-04-23
    • 2013-08-07
    • 2011-10-20
    • 2020-01-23
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多