Difference between printk and pr_info
Asked Answered
C

2

35

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

Cavill answered 15/2, 2017 at 7:34 Comment(2)
@CL. Yes, my bad.Claudieclaudina
All those are equivalent except debug one.Smear
S
35

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.

Solanaceous answered 15/2, 2017 at 7:49 Comment(2)
Actually #define pr_info(fmt, ...) eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)Claudieclaudina
Exception is pr_debug() vs. printk(KERN_DEBUG) and all derivatives.Smear
M
11

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:

Mendacity answered 8/3, 2018 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.