1. 程式人生 > >linux核心列印級別

linux核心列印級別

轉載地址:https://www.cnblogs.com/Caden-liu8888/p/5652168.html

1.printk()是一個核心的一個記錄日誌的機制,經常用來記錄資訊或者警告。printk可以指定輸出日誌的優先順序,在include/linux/kern_levels.h中有相應的巨集定義 

複製程式碼

 1 #define KERN_SOH    "\001"      /* ASCII Start Of Header */  
 2 #define KERN_SOH_ASCII  '\001'  
 3   
 4 #define KERN_EMERG  KERN_SOH "0"    /* system is unusable */  
 5 #define KERN_ALERT  KERN_SOH "1"    /* action must be taken immediately */  
 6 #define KERN_CRIT   KERN_SOH "2"    /* critical conditions */  
 7 #define KERN_ERR    KERN_SOH "3"    /* error conditions */  
 8 #define KERN_WARNING    KERN_SOH "4"    /* warning conditions */  
 9 #define KERN_NOTICE KERN_SOH "5"    /* normal but significant condition */  
10 #define KERN_INFO   KERN_SOH "6"    /* informational */  
11 #define KERN_DEBUG  KERN_SOH "7"    /* debug-level messages */  
12   
13 #define KERN_DEFAULT    KERN_SOH "d"    /* the default kernel loglevel */  
14   
15 /* 
16  * Annotation for a "continued" line of log printout (only done after a 
17  * line that had no enclosing \n). Only to be used by core/arch code 
18  * during early bootup (a continued line is not SMP-safe otherwise). 
19  */  
20 #define KERN_CONT   ""  

複製程式碼

  如果不指定優先順序,這printk就使用預設的優先順序,DEFAULT_MESSAGE_LOGLEVEL 在linux-3.6.10/kernel/printk.c中有定義

1 /* printk's without a loglevel use this.. */  
2 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL  
3   
4 /* We show everything that is MORE important than this.. */  
5 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */  
6 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */

如果未對CONFIG_DEFAULT_MESSAGE_LOGLEVEL進行配置,則預設值是4

 

dmesg:分析核心產生的訊息

dmesg |more                          //輸出所有的核心開機時的資訊。

擷取命令:cut grep

  grep:分析一行訊息,若當中有我們所需要的資訊,就將該行拿出來

1、檢視當前控制檯的列印級別
 1 cat /proc/sys/kernel/printk 
4    4    1    7
其中第一個“4”表示核心列印函式printk的列印級別,只有級別比他高的資訊才能在控制檯上打印出來,既 0-3級別的資訊

2、修改列印


echo "新的列印級別  4    1    7" >/proc/sys/kernel/printk

3、不夠列印級別的資訊會被寫到日誌中可通過dmesg 命令來檢視

4、printk的列印級別

複製程式碼

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

複製程式碼

 

5、printk函式的使用

      printk(列印級別  “要列印的資訊”)

       列印級別  既上面定義的幾個巨集