CodeDump : printk

CodeDump :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

Printk and logging


up

When using printk a loglevel gets defined. printk(KERN_WARNING "Help\n");. Valid loglevels are:
All loglevels are, after macro expansion, an integer ranging from 0 to 7 (0 = highest priority). When no loglevel has been specified it defaults to
DEFAULT_MESSAGE_LOGLEVEL. This is specified in kernel/printk.c:

/* printk's without a loglevel use this.. */
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
 


Depending on the priority messages get logged to the console or discarded. In the following case KERN_DEBUG messages will be discarded (kernel/printk.c):

/* We show everything that is MORE important than this.. */
#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
 


How messages are logged depends on the running loggers (man klogd and man syslogd) and their configurations. Messages can be intercepted by cat'ing /proc/kmsg. The messages are proceeded by the used loglevel.

psychotic:/home/helios# cat /proc/kmsg
<4>    ACPI-0352: *** Error: Looking up [Z005] in namespace, AE_NOT_FOUND
<4>search_node c14d0440 start_node c14d0440 return_node 00000000
<4>    ACPI-1138: *** Error: Method execution failed [\_SB_.BAT1._BST] (Node c14d0340), AE_NOT_FOUND

(Luckily I haven't fixed the buggy ACPI subsystem on my laptop so I can trigger kernel messages on the fly ;-) ).

Changing the console_loglevel can be done by using the -c parameter on klogd, or by syslog() from code. See man 2 syslog for more information. A third way is by accessing the proc filesystem.

psychotic:/usr/src/linux-2.6.11.11/kernel# cat /proc/sys/kernel/printk
7       4       1       7

The digits are:
So the easiest way to change the loglevel is simply:

psychotic:/home/helios/vuilbak/examples/misc-progs# echo 8 > /proc/sys/kernel/printk


The kernel provides a function int printk_ratelimit(void); which can be used to avoid flooding logs with messages. If this function returns zero nothing should be printed. The behaviour of this function can be controlled by modifying the values in /proc/sys/kernel/printk_ratelimit and /proc/sys/kernel/printk_ratelimit_burst which are the number of seconds to wait before re-enabling messages and the number of messages accepted before ratelimiting.
helios@psychotic:~$ cat /proc/sys/kernel/printk_ratelimit
5
helios@psychotic:~$ cat /proc/sys/kernel/printk_ratelimit_burst 
10


Disadvantages of printk:
up
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0604 seconds