We use the log level in the PRINTK () function in order to enable programmers to customize the output of the information during programming, making it easier to master the current state of the system.
It plays an important role in the debugging of the program.
(The log level and the console log control level below are one meaning)
PRINTK (Log Level "message text"); The log level here refers to the specification of an output range of text information.
There are 8 levels in the log level, and the log level of PRINTK is defined as follows (in linux26/includelinux/kernel.h):
#defineKERN_EMERG "<0>"/* Emergency message, prompt before system crash, indicates system is not available */
#defineKERN_ALERT "<1>"/* report message that you must take immediate action */
#defineKERN_CRIT "<2>"/* critical condition, usually involving severe hardware or software operation failure */
#defineKERN_ERR "<3>"/* error condition, driver common Kern_err to report hardware errors */
#defineKERN_WARNING "<4>"/* warning condition to warn of possible problems */
#defineKERN_NOTICE "<5>"/* normal but important conditions for reminders. Frequently used for security-related messages */
#defineKERN_INFO "<6>"/* prompt information, such as when the driver starts, print hardware information */
#defineKERN_DEBUG <7>/* Debug-level messages */
The default level for PRINTK statements that do not specify a log level is Default_ message_loglevel (which is typically <4>, that is, with kern_warning at one level), which is defined in linux26/kernel/ Can be found in printk.c.
The following is a relatively simple use
PRINTK (kern_info "info\n"); Here you can use numbers instead of kern_info, which can be written as PRINTK (<6> "info\n");
In the definition of this format, the log level and the information text cannot be separated by commas, because the system converts the log level to a string of text information to be concatenated at the time of compilation.
In the system output control, mainly discusses the console and pseudo-terminal transmission, as well as the system log.
Here are some brief descriptions of the console log levels
The corresponding log level for the console is defined as follows:
#define MINIMUM_CONSOLE_LOGLEVEL 1/* The minimum log level that can be used */
#define DEFAULT_CONSOLE_LOGLEVEL 7/* More important messages than kern_debug are printed */
int console_printk[4] = {
default_console_loglevel,/* Console log level, messages with precedence above this value will be displayed in the console */
/* Default message log level, PRINTK when no priority is defined, print the message above the priority level */
Default_message_loglevel,
/* Minimum console log level, the minimum value that the console log level can be set to (highest priority) */
Minimum_console_loglevel,
default_console_loglevel,/* default Console Log level */
};
When viewing, you can use the command CAT/PROC/SYS/KERNEL/PRINTK to view these four values
You can change the current console log level by modifying the first value in the file/PROC/SYS/KERNEL/PRINTK.
(declaration: In the following module function, the console uses a log level of kern_warning level) when the logging level is higher than console_loglevel (the console log level), the message can be displayed in the console.
If we write a module function as follows:
1 #include <linux/init.h>
2 #include <linux/module.h>
3 Module_license ("Dual BSD/GPL");
4 static int book_init (void)
5 {
6 PRINTK (Kern_emerg "emerg\n");
7 PRINTK (Kern_alert "alert\n");
8 PRINTK (Kern_crit "crit\n");
9 PRINTK (kern_err "err\n");
Ten PRINTK (kern_warning "" warning\n ");
PRINTK (Kern_notice "notice\n");
PRINTK (kern_info "info\n");
PRINTK (kern_debug "debug\n");
return 0;
}
15static void Book_exit (void)
16{
PRINTK (kern_alert "book Module exit\n");
}
Module_init (Book_init);
Module_exit (Book_exit);
In the console (referred to as the virtual Terminal ctrl+alt+ (F1~F6)) after the module is loaded, the information given by the console is
6~9 the information requested in the line, we run the command on the pseudo-terminal (if it is not very clear to the pseudo-terminal) Tail-n 10/var/log/messages to view the log file just got the running record
You can discover that the value in messages is required to output to an information value after the kern_warning level. And if we look at the system log file in the file syslog and Kern-log, we can usually get all the output information
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637057] INFO
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637063] Crit
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637066] WARNING
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637068] ERR
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637069] ALERT
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637070] Emerg
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637071] NOTICE
Jul 11:44:19 xiyoulinux-desktop kernel: [16100.637072] DEBUG
(This is not the result of running on some machines)
In general, the contents recorded in Syslog and kern.log two files are basically consistent from the point of view of programming.
There are four files under directory/var/log/to view the log
Syslog, Kern.log,messages, DEBUG.
Syslog and Kern.log generally get all the system output values, and messages get a lower output value than the console log level, and debug gets just the debug level.
The output value.
In general, messages with a priority higher than the console log level will be printed to the console. Messages with a priority lower than the console log level are printed to the messages log file, and no information is printed under the pseudo-terminal.
When we are working on programming, if we use the PRINTK () function, the general viewing information is viewed under messages and virtual terminals, and for syslog and kern.log to verify the output of all information.
Linux kernel debug PRINTK () Summary