ted
ted

Reputation: 4975

Prefixing printk / pr_* calls

I would like to prefix my drivers (debug) output with its name, i.e. [myDriver] Actual message. Since it is tiresome to write printk(level NAMEMACRO "Actual message\n") every time I was thinking of overwriting printk/pr_* to actually include the [myDriver] part. However I can not think of a way to do this. In the best case the solution would not force me to change the printk/pr_* calls in the code (With changed calls this becomes trivial).

Is this possible? (Since I included other headers which in turn include the printk header it will always be defined this rules out not linking to the original as suggested in a different so answer)

Are there any reasons why current drivers do not at this to the text? (Is there another way to filter dmesg by driver?)

I am somewhat aware of dev_dbg but I have not found anything dev specific for warnings in general so I will use printk/pr_err for that.

Upvotes: 2

Views: 2065

Answers (2)

ted
ted

Reputation: 4975

The relevant answer (found in the duplicate) is to #define pr_fmt (code from the duplicate question linked above):

/* At the top of the file, before any includes */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/printk.h>

As an additional note, if I include variables, sometime pr_fmt is not automatically applied for me. Manual use as in printk(pr_fmt("message %p"), (void*)ptr) fixes those occasions, and adheres to the convention of defining pr_fmt

Since I did not find the duplicate question, I will not delete this question for other googlers like me.

Upvotes: 2

askb
askb

Reputation: 6768

Its standard to use pr_{debug,warn,err}() with [drivername] prefixed.

ex:

 pr_debug("kvm: matched tsc offset for %llu\n", data);

Alternatively you can use dev_warn()

ex:

 dev_warn(&adap->dev, "Bus may be unreliable\n");

Is there another way to filter dmesg by driver?

Not unless you want to run dmesg -c to clear the logs, before getting the your driver loaded. Its always recommenced prefixing the driver name in your debug / print messages. As when you receives logs from customers, you don't want to waste time reading through each line manually.

Upvotes: 5

Related Questions