Skip to content

Printing

printk

Tip

You probably shouldn't use printk, but the pr_### functions instead.

Source

printk(KERN_INFO "Message: %s\n", arg);

Note

The KERN_### part of the printk call is not a separate argument, it concatenates to the message.

Get current log level

# current, default, minimum, boot
$ cat /proc/sys/kernel/printk
4        4        1        7

Set current log level

Using sysfs:

echo 8 > /proc/sys/kernel/printk

Using dmesg:

dmesg -n 8

pr_### functions

Name String Alias function
KERN_EMERG “0” pr_emerg()
KERN_ALERT “1” pr_alert()
KERN_CRIT “2” pr_crit()
KERN_ERR “3” pr_err()
KERN_WARNING “4” pr_warn()
KERN_NOTICE “5” pr_notice()
KERN_INFO “6” pr_info()
KERN_DEBUG “7” pr_debug() and pr_devel() if DEBUG is defined
KERN_DEFAULT “”
KERN_CONT “c” pr_cont()

pr_fmt

The pr_### functions utilise an internal function called pr_fmt which you can overwrite locally in your own kernel driver.

For example, you can force every message to be prepended with the module and function name, saving a lot of duplication and potential future change:

#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__