[关闭]
@zifehng 2017-06-08T11:24:34.000000Z 字数 3080 阅读 3783

Linux内核log等级与printk打印消息控制

linux log


预定义的内核log等级

  1. // include/linux/kern_levels.h
  2. #define KERN_SOH "\001" /* ASCII Start Of Header */
  3. #define KERN_SOH_ASCII '\001'
  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. #define KERN_DEFAULT KERN_SOH "d" /* the default kernel loglevel */

printk的log等级控制

  1. // linux/include/printk.h
  2. /* printk's without a loglevel use this.. */
  3. #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
  4. /* We show everything that is MORE important than this.. */
  5. #define CONSOLE_LOGLEVEL_DEFAULT 7
  6. #define CONSOLE_LOGLEVEL_MIN 1
  7. #define CONSOLE_LOGLEVEL_DEFAULT 7
  8. -------------------------------------------------------------------------
  9. // kernel/printk.c
  10. int console_printk[4] = {
  11. CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
  12. MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
  13. CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
  14. CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
  15. };
  16. -------------------------------------------------------------------------
  17. // linux/include/printk.h
  18. extern int console_printk[];
  19. #define console_loglevel (console_printk[0])
  20. #define default_message_loglevel (console_printk[1])
  21. #define minimum_console_loglevel (console_printk[2])
  22. #define default_console_loglevel (console_printk[3])

通过procfs改变log等级

1. 查看当前printk的log等级

  1. # cat /proc/sys/kernel/printk
  2. # 7 4 1 7

“7 4 1 7” 分别对应console_loglevel、default_message_loglevel、minimum_c onsole_loglevel、default_console_loglevel,意味着只有优先级高于KERN_DEBUG(7)的打印消息才能输出到终端

2. 改变console_loglevel

  1. # echo 8 4 1 7 > /proc/sys/kernel/printk

输入“8 4 1 7”改变console_loglevel值,使得所有的打印消息都能输出到终端

3. procfs系列节点

  1. static struct ctl_table kern_table[] = {
  2. ......
  3. #if defined CONFIG_PRINTK
  4. {
  5. .procname = "printk",
  6. .data = &console_loglevel,
  7. .maxlen = 4*sizeof(int),
  8. .mode = 0644,
  9. .proc_handler = proc_dointvec,
  10. },
  11. {
  12. .procname = "printk_ratelimit",
  13. .data = &printk_ratelimit_state.interval,
  14. .maxlen = sizeof(int),
  15. .mode = 0644,
  16. .proc_handler = proc_dointvec_jiffies,
  17. },
  18. {
  19. .procname = "printk_ratelimit_burst",
  20. .data = &printk_ratelimit_state.burst,
  21. .maxlen = sizeof(int),
  22. .mode = 0644,
  23. .proc_handler = proc_dointvec,
  24. },
  25. {
  26. .procname = "printk_delay",
  27. .data = &printk_delay_msec,
  28. .maxlen = sizeof(int),
  29. .mode = 0644,
  30. .proc_handler = proc_dointvec_minmax,
  31. .extra1 = &zero,
  32. .extra2 = &ten_thousand,
  33. },
  34. {
  35. .procname = "printk_devkmsg",
  36. .data = devkmsg_log_str,
  37. .maxlen = DEVKMSG_STR_MAX_SIZE,
  38. .mode = 0644,
  39. .proc_handler = devkmsg_sysctl_set_loglvl,
  40. },
  41. {
  42. .procname = "dmesg_restrict",
  43. .data = &dmesg_restrict,
  44. .maxlen = sizeof(int),
  45. .mode = 0644,
  46. .proc_handler = proc_dointvec_minmax_sysadmin,
  47. .extra1 = &zero,
  48. .extra2 = &one,
  49. },
  50. {
  51. .procname = "kptr_restrict",
  52. .data = &kptr_restrict,
  53. .maxlen = sizeof(int),
  54. .mode = 0644,
  55. .proc_handler = proc_dointvec_minmax_sysadmin,
  56. .extra1 = &zero,
  57. .extra2 = &two,
  58. },
  59. #endif
  60. ......
  61. { }
  62. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注