linux内核打印级别

时间:2023-03-09 18:37:06
linux内核打印级别

  1.printk()是一个内核的一个记录日志的机制,经常用来记录信息或者警告。printk可以指定输出日志的优先级,在include/linux/kern_levels.h中有相应的宏定义 

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

  如果不指定优先级,这printk就使用默认的优先级,DEFAULT_MESSAGE_LOGLEVEL 在linux-3.6.10/kernel/printk.c中有定义

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

如果未对CONFIG_DEFAULT_MESSAGE_LOGLEVEL进行配置,则默认值是4

dmesg:分析核心产生的讯息

dmesg |more                          //输出所有的核心开机时的信息。

截取命令:cut grep

  grep:分析一行讯息,若当中有我们所需要的信息,就将该行拿出来

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的打印级别

 #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 condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */

5、printk函数的使用

      printk(打印级别  “要打印的信息”)

打印级别  既上面定义的几个宏