linux kernel log Print: printk and usage of dmesg

Transfer: https://blog.csdn.net/qq_29350001/article/details/52232128

For detecting and controlling kernel ring buffer. Start a program to help users understand the information system

Linux dmesg command to display the boot information, kernel will boot information stored in the ring buffer

Boot information is stored in / var / log directory name for the file dmesg

dmesg [ -c ] [ -n 级别 ] [ -s 缓冲区大小 ]
  • 1

-c 
clear the contents of the ring buffer 
 

-s buffer size 

Define a size "buffer size" of the buffer used to query the kernel ring buffer. The default size is 8196 (this default syslog buffer size is consistent with the size of the kernel 2.0.33 and 2.1.103), if you set a value greater than the default ring buffer, then you can use this option to define a considerable buffer area to see the full content of the ring buffer 
 

-n level 
Set the level of the start level information for recording console. For example, -n 1 refers to this level to the lowest level, in addition to the kernel panic message does not display information to the console. All levels will start recording information to / proc / kmsg, document, therefore, syslogd (8) can also be used to control the output of information. When using the -n option, dmesg will not clear the contents of the kernel ring buffer. When using more than two options, only the last option will produce results.

 

To the linux kernel printk content with the level of control of print at the command line, enter dmesg -n 8 will all levels of information are printed out

the printk () instructions

The kernel through the printk) having a log information output level (log level before the string by the printk () output a plus angle brackets integer controlled, such as printk ( "<6> Hello, world! / N" ) ;. CCP kernel provides eight different log level corresponds to a corresponding macro linux / kernel.h in.

#define KERN_EMERG      "<0>"    /* system is unusable */  
#define KERN_ALERT      "<1>"    /* action must be taken immediately */  
#define KERN_CRIT       "<2>"    /* critical conditions */  
#define KERN_ERR        "<3>"    /* error conditions */  
#define KERN_WARNING    "<4>"    /* warning conditions */  
#define KERN_NOTICE     "<5>"    /* normal but significant */  
#define KERN_INFO       "<6>"    /* informational */  
#define KERN_DEBUG      "<7>"    /* debug-level messages */ 
#define KERN_EMERG      "<0>"    /* system is unusable */
#define KERN_ALERT      "<1>"    /* action must be taken immediately */
#define KERN_CRIT       "<2>"    /* critical conditions */
#define KERN_ERR        "<3>"    /* error conditions */
#define KERN_WARNING    "<4>"    /* warning conditions */
#define KERN_NOTICE     "<5>"    /* normal but significant */
#define KERN_INFO       "<6>"    /* informational */
#define KERN_DEBUG      "<7>"    /* debug-level messages */


Therefore, the printk () can be so used: printk (KERN_INFO "! Hello, world / n") ;.

Log level unspecified printk () is used to DEFAULT_MESSAGE_LOGLEVEL default level, this macro is defined as an integer of 4, i.e. in correspondence KERN_WARNING kernel / printk.c in.

In / proc / sys / kernel / printk displays four values ​​(echo may be modified), respectively, the current console log level, not explicitly specified log level default message log level, the minimum (maximum) to allow the console logging level set the default boot log level. When a message log level () is less than the current printk console log level, printk information (have / n character) is displayed on the console. But no matter what the current console log level value is, by / proc / kmsg (or use dmesg) can always view. Also, if configured and running syslogd or klogd, printk information is not displayed on the console will be appended to the /var/log/messages.log in.

Can be read and modified by the console log level read / proc / sys / kernel / printk file. View this document as follows:

#cat /proc/sys/kernel/printk
6   4  1   7

 

4 respectively correspond to the data shown above console log level, the default message log level, the lowest level and the console log default console log level.

With the following command sets the current log level:

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

 

Furthermore, in order to prevent the printk () is invoked too often transiently, by printk_ratelimit () to control the speed. printk_ratelimit () message is sent to the console by tracking the number of jobs, when the output speed exceeds a critical value, zero is returned. Calculation may be adjusted printk_ratelimit () by modifying the / proc / sys / kernel / {printk_ratelimit, printk_ratelimit_burst} a.

printk_ratelimit () is typically used as follows:
IF (printk_ratelimit ())
    the printk (KERN_INFO "the Hello, World / n-!");

Guess you like

Origin blog.csdn.net/xuhao07/article/details/90072032