25

What's the exact difference between printk and pr_info functions ? And under what conditions, should I choose one over the another ?

Jarvis
  • 8,020
  • 3
  • 23
  • 51

2 Answers2

23

The kernel's printk.h has:

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

Just like the name, pr_info is printk with the KERN_INFO priority.

CL.
  • 158,085
  • 15
  • 181
  • 214
barcelona_delpy
  • 441
  • 4
  • 14
10

When looking specifically at pr_info, the definition will in turn use printk(KERN_INFO ... (as mentioned in barcelona_delpy's answer); however, the answer's source snippet appears to exclude the format wrapper pr_fmt(fmt) (as mentioned by LPs comment).


The difference to why you may use pr_info over printk(KERN_INFO ... is the custom formatting you can set. If you wish to prefix your messages in your module with printk, a method is to explicitly add your prefix on each line:

printk(KERN_INFO "mymodule: hello there\n");
// outputs "mymodule: hello there"

or:

printk(KERN_INFO KBUILD_MODNAME " hello there\n");
// outputs "mymodule: hello there"

However, if you use pr_info (and other pr_* functions), you can re-define the format and simply use pr_info without additional work:

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

See also:

jdknight
  • 1,526
  • 27
  • 46