1. 程式人生 > >Linux核心列印函式printk的使用說明

Linux核心列印函式printk的使用說明

printk函式

        We used the printk function in earlier chapters with the simplifying assumption that it works like printf. Now it's time to introduce some of the differences. 

        我們在前面章節中簡單地把printk當作printf函式來使用。現在是時候來介紹它的一些不同之處了。 

        One of the differences is that printk lets you classify messages according to their severity by associating different loglevels, or priorities, with the messages. You usually indicate the loglevel with a macro. For example, KERN_INFO, which we saw prepended to some of the earlier print statements, is one of the possible loglevels of the message. The loglevel macro expands to a string, which is concatenated to the message text at compile time; that's why there is no comma between the priority and the format string in the following examples. Here are two examples of printk commands, a debug message and a critical message: 

        其中一個不同點是,printk允許你按照相關的記錄級或優先順序將訊息嚴格分類
。通常你需要一個巨集來指定記錄等級。例如,KERN_INFO,我們在早先的的例子中看到過這個巨集,它就是訊息記錄等級的一種。記錄等級巨集的作用是擴充套件為一個字串,這個字串會在編譯期間與相應的訊息文字相連線;這就解釋了下面例子中為什麼在優先順序和格式化字串之間沒有逗號了。下面是兩個printk函式的例子,一個是除錯訊息,一個是臨界訊息: 

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 loglevel strings, defined in the header   ; we list them in order of decreasing severity: 
        
        在標頭檔案
<linux/kernel.h>中共定義了八個可用的記錄級;我們下面按其嚴重性倒序列出: 

KERN_EMERG

Used for emergency messages, usually those that precede a crash. 

用於突發性事件的訊息,通常在系統崩潰之前報告此類訊息。 

KERN_ALERT

A situation requiring immediate action. 

在需要立即操作的情況下使用此訊息。 

KERN_CRIT

Critical conditions, often related to serious hardware or software failures. 

用於臨界條件下
,通常遇到嚴重的硬軟體錯誤時使用此訊息。 

KERN_ERR

Used to report error conditions; device drivers often use KERN_ERR to report hardware difficulties. 

用於報告錯誤條件;裝置驅動經常使用KERN_ERR報告硬體難題。 

KERN_WARNING

Warnings about problematic situations that do not, in themselves, create serious problems with the system. 

是關於問題狀況的警告,一般這些狀況不會引起系統的嚴重問題。 

KERN_NOTICE 

Situations that are normal, but still worthy of note. A number of security-related conditions are reported at this level. 

該級別較為普通,但仍然值得注意。許多與安全性相關的情況會在這個級別被報告。 

KERN_INFO

Informational messages. Many drivers print information about the hardware they find at startup time at this level. 

資訊訊息。許多驅動程式在啟動時刻用它來輸出獲得的硬體資訊。 

KERN_DEBUG 

Used for debugging messages. 

用於輸出除錯資訊 

        Each string (in the macro expansion) represents an integer in angle brackets. Integers range from 0 to 7, with smaller values representing higher priorities. 

        每一個字串(由巨集擴充套件而成)表示了尖括號內的一個整數。數值範圍從0到7,數值越小,優先順序越高。 

        A printk statement with no specified priority defaults to DEFAULT_MESSAGE_LOGLEVEL, specified in kernel/printk.c as an integer. In the 2.6.10 kernel, DEFAULT_MESSAGE_LOGLEVEL is KERN_WARNING, but that has been known to change in the past. 

        一個printk的預設優先順序是DEFAULT_MESSAGE_LOGLEVEL,它是一個在kernel/printk.c檔案中指定的整數。在2.6.10核心中,DEFAULT_MESSAGE_LOGLEVEL相當於KERN_WARNING,但據說早期版本中這是兩個不同的優先順序。 

        Based on the loglevel, the kernel may print the message to the current console, be it a text-mode terminal, a serial port, or a parallel printer. If the priority is less than the integer variable console_loglevel, the message is delivered to the console one line at a time (nothing is sent unless a trailing newline is provided). If both klogd and syslogd are running on the system, kernel messages are appended to /var/log/messages (or otherwise treated depending on your syslogd configuration), independent of console_loglevel. If klogd is not running, the message won't reach user space unless you read /proc/kmsg (which is often most easily done with the dmesg command). When using klogd, you should remember that it doesn't save consecutive identical lines; it only saves the first such line and, at a later time, the number of repetitions it received. 

        基於這些記錄級,核心可以把訊息輸出到當前的控制檯,也可以是一個文字模式的終端,一個串列埠,或是一個並口印表機。如果優先順序小於整形變數 console_loglevel,那麼一次將會只發送一行訊息到控制檯中(除非遇到一個換行符,否則將什麼都不會發送)。如果系統中運行了klogd和syslogd程序,那麼核心訊息就會被完整地新增到/var/log/messages檔案中(或者根據你的syslogd程序的配置狀況進行傳送)而忽略console_loglevel,如果klogd沒有執行,那麼訊息將不會到達使用者空間,除非你對/proc/kmsg檔案讀取(實際上這項工作已被較早的dmesg命令完成)。當使用klogd時,你應該記住它不會保留重複的訊息行對於它接收到的重複訊息,它只會保留第一條。 

        The variable console_loglevel is initialized to DEFAULT_CONSOLE_LOGLEVEL and can be modified through the sys_syslog system call. One way to change it is by specifying the -c switch when invoking klogd, as specified in the klogd manpage. Note that to change the current value, you must first kill klogd and then restart it with the -c option. Alternatively, you can write a program to change the console loglevel. You'll find a version of such a program in misc-progs/setlevel.c in the source files provided on O'Reilly's FTP site. The new level is specified as an integer value between 1 and 8, inclusive. If it is set to 1, only messages of level 0 (KERN_EMERG) reach the console; if it is set to 8, all messages, including debugging ones, are displayed. 

        被初始化為DEFAULT_CONSOLE_LOGLEVELconsole_loglevel變數可以通過sys_syslog系統呼叫修改。改變它內容的一個方法就是在呼叫klogd時使用-c選項,具體參考klogd的man幫助。請注意,為了改變當前的數值,你必須首先結束klogd程序,並且用-c選項重新啟動它。另一種方法是,你可以寫一個應用程式來改變控制檯的記錄等級。你可以在O'Reilly的FTP站點提供的原始碼檔案中找到/misc-progs/setlevel.c檔案,其中就有一個這樣的程式。新記錄級為一個1到8的整數。如果設定為1,那麼只有優先順序為0(KERN_EMERG)的訊息才可以到達控制檯;如果等級設定為8,那麼包括除錯資訊在內的所有訊息都會顯示。 

        It is also possible to read and modify the console loglevel using the text file /proc/sys/kernel/printk. The file hosts four integer values: the current loglevel, the default level for messages that lack an explicit loglevel, the minimum allowed loglevel, and the boot-time default loglevel. Writing a single value to this file changes the current loglevel to that value; thus, for example, you can cause all kernel messages to appear at the console by simply entering: 

        也可以通過文字檔案/proc/sys/kernel/printk來獲取和更改控制檯的記錄等級。這個檔案中儲存著四個整型數值:當前記錄級,預設記錄級,最低記錄級和啟動時刻的預設記錄級。可以向該檔案寫入一個單一數值來改變當前記錄級;例如,如果你可以想所有的核心訊息都可以在控制檯中顯示,可以使用以下命令: 

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

        It should now be apparent why the hello.c sample had the KERN_ALERT; markers; they are there to make sure that the messages appear on the console. 

        現在你應該明白在hello.c示例程式碼中為什麼會有KERN_ALERT;標識了吧;這樣做可以保證訊息順利地輸出到控制檯中