By Jonathan Corbet
December 22, 2009
One of a kernel developer's best friends is the
printk() function,
which works much like
printf() in user-space programs. There
are some differences, though, including the existence of various levels of
logging. The convention used is a little funny, with the logging level
being a short string prepended to the format string. So a warning might be
printed this way:
printk(KERN_WARNING "Core meltdown imminent\n");
This form is not universally loved, though; some call it verbose, making it
hard to make lines fit within 80 columns, and the severity string is easy
to forget. As an alternative, the 2.6.28-rc5 kernel saw the addition of a
set of pr_*() macros, written by Martin Schwidefsky, which are
designed to make life a little easier. So, for example, the above warning
could be rewritten as:
pr_warning("Software patents detected\n");
These macros sat in relative obscurity for a few development cycles until
Joe Perches decided to switch a number of printk() statements in
the core kernel. That led to an outburst
from Peter Zijlstra and the eventual reverting of the change. Peter says:
I might be strange, but if I want to print something in C I write
print[fk]() and be done with it, there's no reason what so ever to
introduce fancy wankery for this. We try to stick to ANSI-C as
much as possible, we've got kalloc,kfree,strcmp,strnlen and all the
other 'regular' C bits, deviating from that serves no purpose but
seed confusion.
Chances are, there won't be any more such conversions near that part of the
kernel. But the pr_*() macros won't be going away either. Their
real purpose, perhaps, was best expressed
by Arjan van de Ven: "pr_ is really just for 'I am a driver and want
a single line message out in a standardized format'. Nothing wrong with
that."
(
Log in to post comments)