1

I have created a set of shortcut Macros for Linux Kernel printk to prefix with the module name and to have a set of aliases for severity.

#define _pp(severity, format, args...) \
  printk(severity "%s: " #format "\n", THIS_MODULE->name, ##args)

#define pp_warn(args...) _pp(KERN_WARNING, args)
#define pp_note(args...) _pp(KERN_NOTICE, args)
#define pp_info(args...) _pp(KERN_INFO, args)

Using those Macros like this:

static int __init pretty_printk_demo_init(void) {

  pp_warn("Warning severity");
  pp_warn("Warning, checking arguments, %s and %i", "DDD", 44);

}

Now the issue is that the final output in the Kernel Ring Buffer get quoted in the following way:

[  470.819436] pp_demo_module: "Warning severity"
[  470.819438] pp_demo_module: "Warning, checking arguments, DDD and 44"

Do you have any idea of how to avoid having this extra quotes?

For the full files please take a look at the Header and the Source File.

Thank you very much for your time and looking forward to a possible solution.

  • 1
    Remove the `#` in front of `#format`? Why did you add it there then? In modules you should do `#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt` on top of you module to add a prefix (or anything else) to output of your module. – KamilCuk May 12 '20 at 09:00
  • @KamilCuk Thanks, yes removing # from format does the trick. Wondering why I put it there in first place, guess I was not so sure how this three severity "%s: " #format "\n" expressions would be concatenated. Wasn't aware of pr_fmt used in the Kernel, so guess that's the way to prefix printk. Please go ahead and write a little answer if you like, I'll accept it :) – Thomas Piekarski May 12 '20 at 09:38

1 Answers1

3

You have an additional # in the macro replacement. Just remove it - format is supposed to be a string already:

#define _pp(severity, format, args...) \
  printk(severity "%s: " format "\n", THIS_MODULE->name, ##args)

For prepending some values in kernel the pr_fmt macro was meant to be used, like here or here or basically examples everywhere.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111