The Printk and dmesg functions in Linux print out the printk content with level control of the Linux kernel and print all levels of information in the command line input dmesg-n8. Linux command: dmesg function description: displays the boot information. Syntax: dmesg [-...
In Linux, the Printk and dmesg functions print out the printk content with level control of the Linux kernel. input dmesg-n 8 on the command line to print all levels of information. Linux command: dmesg function description: displays the boot information. Syntax: dmesg [-cn] [-s <缓冲区大小> ] Note: the kernel will store the boot information in the ring buffer. You can use dmesg to view the information if you cannot view it at startup. The boot information is also saved in the/var/log directory and named dmesg. Www.2cto.com parameter:-c display information, clear the content in the ring buffer. -S <缓冲区大小> The value is set to 8196, which is equal to the size of the ring buffer. -N: set the record information level. The use of printk () indicates that the information output by the kernel through printk () has a log level. the log level is controlled by adding an integer with angle brackets before the string output by printk, for example, printk ("<6> Hello, world! ");. The kernel provides eight different log levels, which have corresponding macros in linux/kernel. h. # 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 */so printk () can be used as follows: printk (KERN_INFO "Hello, world! ");. The default log-level printk () is DEFAULT_MESSAGE_LOGLEVEL. this macro is defined as an integer 4 in kernel/printk. c, which corresponds to KERN_WARNING. At www.2cto.com, four values are displayed in/proc/sys/kernel/printk (which can be modified by echo ), indicates the log level of the current console, the default message log level that is not explicitly specified for the log level, the minimum (highest) level that can be set for the console log level, and the default log level at the boot time. When the message log level in printk () is smaller than the current console log level, the information of printk (which must have a letter) is displayed on the console. However, no matter what the log level of the current console is, you can always view it through/proc/kmsg (or dmesg. In addition, if syslogd or klogd is configured and run, the printk information that is not displayed on the console will be appended to/var/log/messages. log. You can read and write the/proc/sys/kernel/printk file to read and modify the log level of the console. The method for viewing this file is as follows: # cat/proc/sys/kernel/printk6 4 1 7 the four data displayed above correspond to the console log level, default message log level, lowest console log level, and default console respectively log level. Use the following command to set the current log level: # echo 8>/proc/sys/kernel/printk analysis and summary: www.2cto.com Printf is displayed on the terminal, printk () the function is the information printing function in the kernel space, just like the printf () function used in c programming. it is used exclusively for displaying information in the kernel. it does not call printf () the reason is that there is no c library function available for calling when compiling the kernel. In linux, you can use printk () like printf () or add a priority, for example: printk (KERN_ALERT "LCD light exit. the \ n "); printk () function has eight priority definitions, as follows: # 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> "/* norma L but significant condition */# define KERN_INFO "<6>"/* informational */# define KERN_DEBUG "<7>"/* debug-level messages */when printk () the priority is lower than int lele_loglevel, and the information will be printed directly on your terminal (it does not seem to work in the x environment ). If both syslogd and klogd are running, the information is also added to the file/var/log/messages, regardless of whether the information is displayed on the console or not. We use a high priority like KERN_ALERT to ensure that printk () outputs information to the console rather than simply adding it to the log file. When writing a practical module, you should use the appropriate priority for possible situations. You can use cat/var/log/messages to view information in www.2cto.com/var/log/messages. You can use cat/peoc/sys/kernel/printk to view the log level on the console. You can use echo 3>/peoc/sys/kernel/printk to change the log level on the console (3 is the value to be written, which can be changed ).
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service