Difference between printk and pr_info

18,869

Solution 1

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.

Solution 2

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:

Share:
18,869
Jarvis
Author by

Jarvis

Codes by day, sleeps by night (way mainstream, I know) :P Hi, I am Saurabh, currently working as a Software Developer at Morgan Stanley, Tokyo, Japan. Thanks for stopping by!

Updated on June 03, 2022

Comments

  • Jarvis
    Jarvis almost 2 years

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