0

I want to print some statements from kernel but not all.

So can I use printk selectively?

By selectively I mean I want to enable printk only at particular places and disable it everywhere else.

Right now I can only enable (in the kernel configuration enabling printk) or disable it globally; I have no choice at all.

Mat
  • 52,586
user2799508
  • 1,712
  • You might be able to modify the printk source to check for the first character to be special (ie. not in the normal messages) and if that characters is the first of the message, strip it and unconditionally put the stuff in the buffer to print. – Anthon May 19 '14 at 22:07

1 Answers1

5

(Edit: Additional info is here too: Description of kernel.printk values)

You can use the sysctl command/system to alter the kernel printk settings as desired.

sysctl -w kernel.printk="4 4 1 7"

would set the printk settings to 'default' values, while

sysctl -w kernel.printk="3 4 1 3"

would prevent low level messages from being output to the console.

sysctl kernel.printk

will show you your current settings.

From the linux documentation (kernel docs):

printk:

The four values in printk denote: console_loglevel, default_message_loglevel, minimum_console_loglevel and default_console_loglevel respectively.

These values influence printk() behavior when printing or logging error messages. See 'man 2 syslog' for more info on the different loglevels.

  • console_loglevel: messages with a higher priority than this will be printed to the console
  • default_message_loglevel: messages without an explicit priority will be printed with this priority
  • minimum_console_loglevel: minimum (highest) value to which console_loglevel can be set
  • default_console_loglevel: default value for console_loglevel

You will usually need to be root, or have sudo privileges to write (-w) new values using sysctl. You could also echo values into /proc/sys/kernel/printk, need to be root(ish) for that too.


Edit2:

My Monkey Debugging Assistant just reminded me that there is another way to obtain the kernel printk messages which could be useful.

It is possible to request that syslog/rsyslog/whateverlog daemon send the kernel output to a particular file, pipe or socket, to be read by a program of your choosing, parsed, saved or discarded as appropriate.

My rsyslog.conf shows that all kern.* level messages are sent to /var/log/kern.log, you could easily listen to that file and obtain any messages you desired.

It's also possible to define your own facility type and have rsyslog send those to someplace special. Even selectively sending kern.warn or kern.crit messages to somewhere if desired.

The config setups for rsyslog and syslog are different, as always, man pages are your friend.

lornix
  • 3,482