printk function

One difference is that printk lets you classify them according to the severity of the message, by attaching different recording level or priority. You often use a macro definition to indicate the level recorded on the message. For example, KERN_INFO, before we had a number of print statements the saw earlier, a message record level values ​​may be recorded as a string definition expansions, in connection with the message text together compile time; that is why there is no comma between the priority and the format string below . the reason there are two examples of printk commands, a debug message, an emergency message:

 

printk(KERN_DEBUG "Here I am: %s:%i\n", FILE , LINE ); printk(KERN_CRIT "I'm trashed; giving up on %p\n", ptr);

 

There are eight possible recording string defined in the header <linux / kernel.h> li; We list them in order of decreasing severity:

 

KERN_EMERG

 

For an emergency message, the message is often in front of those crashes.

 

KERN_ALERT

 

 

 

Require immediate action in the case.

 

KERN_CRIT

 

Severe cases, often associated with severe failure of hardware or software.

 

KERN_ERR

 

It used to report an error condition; KERN_ERR device driver is often used to report a hardware failure.

 

KERN_WARNING

 

Warning problematic situations, these cases themselves do not cause serious problems in the system.

 

KERN_NOTICE

 

Normally, it is still worth noting. Relevant circumstances will be reported at this level some security.

 

KERN_INFO

 

Informational messages. At this level, many drivers print information about the hardware they find at startup.

 

KERN_DEBUG

 

As a debug message.

 

Each string (defined in macro expansion) represents an integer in angle brackets. Integer ranging from 0 to 7, a smaller number indicates a larger priority.

 

Do not specify a priority printk statement default is DEFAULT_MESSAGE_LOGLEVEL, specified as an integer in kernel / printk.c in. In the 2.6.10 kernel, DEFAULT_MESSAGE_LOGLEVEL is KERN_WARNING, but in the past known to be changed.

 

Based on the recording level, the kernel may print the message to the current console, a terminal may be a text mode, serial port, or a parallel printer. If the priority is less than console_loglevel integer value, the message is delivered to the console, one row (unless the end of a new line, send nothing otherwise). If klogd and syslogd running on the system, the message is appended to the core / var / log / messages (or another configuration depending on your syslogd treatment), independent of console_loglevel If klogd is not running, you only read / proc / kmsg (with dmsg commands most likely to do) will take the message to the user space when using klogd, you should remember that it will not save the same continuous line; it does retain article one such line, followed by the number of duplicate rows it receives.

 

Console_loglevel variable initialized to DEFAULT_CONSOLE_LOGLEVEL, and can be modified by calling sys_syslog system. It's kind of modification is to specify -c switch when invoking klogd, there are specified in the klogd manpage in. Note To change the current value, you must first kill klogd and then restart it using the -c option. in addition, you can write a program to change the console logging level. you will find this version of a program on the FTP site provided by O 'Reilly of miscprogs / setlevel.c. the new a level not specified integer, 1 and 8 before, and comprising 1 8. If it is set to 1, only the message 0 (KERN_EMERG) reaches the console; if it is set to 8, all messages, including debug messages are displayed.

 

61

 

You can also text file / proc / sys / kernel / printk literacy console record levels this file has four integer values: the minimum recording the current record level for no clear default level logging level of the message, the allowed level , and the default recording start level when writing a single value to the file to change the current record level to this value; therefore, for example, you can make all kernel messages appear in the console by simply typing:

 

# echo 8 > /proc/sys/kernel/printk

 

It should now be clear why hello.c example of using KERN_ALERT signs; they are to ensure that the message appears on the console.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11106341.html