What's the exact difference between printk
and pr_info
functions ? And under what conditions, should I choose one over the another ?
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.
#define pr_info(fmt, ...) eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
–
Claudieclaudina pr_debug()
vs. printk(KERN_DEBUG)
and all derivatives. –
Smear 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:
© 2022 - 2024 — McMap. All rights reserved.