What's the exact difference between printk
and pr_info
functions ? And under what conditions, should I choose one over the another ?
Asked
Active
Viewed 2.6k times
33

Jarvis
- 8,494
- 3
- 27
- 58
-
@CL. Yes, my bad. – LPs Feb 15 '17 at 09:45
-
All those are equivalent except debug one. – 0andriy Feb 15 '17 at 22:37
2 Answers
34
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.
-
Actually `#define pr_info(fmt, ...) eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)` – LPs Feb 15 '17 at 08:24
-
Exception is `pr_debug()` vs. `printk(KERN_DEBUG)` and all derivatives. – 0andriy Feb 15 '17 at 22:38
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:

jdknight
- 1,801
- 32
- 52