Refer to this:
Http://blogold.chinaunix.net/u3/111072/showart_2298170.html
Printk is the kernel state information printing function, which is similar to the printf function of quasi-C library.
Printk also has the information printing level.
Function prototype:
Int printk (const char * FMT ,... |
Required header file:
# Include <Linux/kernel. h |
Message printing level:
FMT ---- message level:
# Define Kern_emerg
"<0>" # Define Kern_alert
"<1>" # Define Kern_crit
"<2>" # Define Kern_err
"<3>" # Define kern_warning
"<4>" # Define Kern_notice
"<5>" # Define Kern_info
"<6>" # Define Kern_debug
"<7>" |
Different levels are represented by different strings. The smaller the number, the higher the level.
The printk output is related to the output log level. When the output log level is higher than the console level, it is displayed on the console. When the output log level is lower than the console level, it is recorded in/var/log
/Message. But when the system runs both klogd and syslogd, It is appended to/var/log/message. In/proc/sys
In the/kernel/printk file, the first two integers are the current log level and the default log level (the default log level is the printk output level ).
Note:
/Proc/sys/kernel/printk
This file has four numeric values, which define where messages are sent based on the importance of logging. For more information about different log levels, see the online help page of syslog (2. The four values of this file are:
Console Log Level: messages with a higher priority will be printed to the console
Default Message Log Level: this priority is used to print messages with no priority.
Lowest console Log Level: Minimum value that can be set at the console Log Level (highest priority)
Default console Log Level: Default console Log Level
/Proc/sys/kernel/printk
The four values in this file are console_loglevel, default_mes-
Sage_loglevel, minimum_lele_level and default_con-
Sole_loglevel. These values influence printk () behavior when
Printing or logging error messages. See syslog (2) for more info
On the different loglevels. messages with a higher priority
Than console_loglevel will be printed to the console. Messages
Without an explicit priority will be printed with priority
Default_message_level. minimum_console_loglevel is the minimum
(Highest) value to which console_loglevel can be set.
Default_console_loglevel is the default value for con-
Sole_loglevel.
Default settings: 6 4 1 7
Note:
You must enable the klogd and syslogd services for normal output. You can use klogd to change the system message output level.
Linux0.11 why does the printk () function be used in kernel state, but the printf () function be used in user State?
A: The printk () function directly uses the tty_write () function to write data to the terminal (). The printf () function calls the write () System Call function to write data to the standard output device.
Therefore, you cannot directly use the printk () function in the user State (such as process 0). Because the kernel state is already privileged, you do not need to call the system to change the privilege level, therefore, it can be used directly.
Printk () function.
Printk is the kernel output, which is invisible on the terminal.
You can check the system log. /Var/log/message
You can also run the dmesg command to check the configuration.
The terminal opened with CTRL + ALT + t cannot be displayed, it must be opened with CTRL + ALT + F2-F6 terminal can be displayed.
printk()
It is not designed to interact with users.
Hello-1
It is for this purpose! It actually provides the log function for the kernel,
Record the kernel information or give a warning. Therefore, each
printk()
The declaration will have a priority, as you can see
<1>
And
Kern_alert
That way. The kernel defines a total of eight priority macros,
So you don't have to use obscure digital code, and you can
Linux/kernel. h
View these macros and their meanings. If you
Priority is not specified. default priority
Default_message_loglevel
Will be used.
Read these priority macros. The header file also describes the meaning of each priority. In practice,
Use macros instead of numbers, just like
<4>
. Always use macros, just like
Kern_warning
.
When the priority is lower
Int
Lele_loglevel
, The information will be printed directly on your terminal. If
Syslogd
And
Klogd
All are running, and the information is also added to the file
/Var/log/messages
Whether displayed on the console or not. We use images
Kern_alert
This is a high priority to ensure
printk()
Output Information
The console is not just added to the log file.
When writing a practical module, you should use
Priority.