官术网_书友最值得收藏!

How to do it...

In the previous example, we saw that we can use the printk() function to generate kernel messages, but there are other functions that we can use in place of printk() in order to have more efficient messages and compact and readable code:

  1. Use the following macros (as defined in the include/linux/printk.h file), which are listed in the following:
#define pr_emerg(fmt, ...) \
printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert(fmt, ...) \
printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit(fmt, ...) \
printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_err(fmt, ...) \
printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warning(fmt, ...) \
printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warn pr_warning
#define pr_notice(fmt, ...) \
printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...) \
printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  1. Now, to generate a kernel message, we can do the following: looking at these definitions, we can rewrite our dummy_code_init() and dummy_code_exit() functions from the previous example into the dummy-code.c file, as follows:
static int __init dummy_code_init(void)
{
pr_info("dummy-code loaded\n");
return 0;
}

static void __exit dummy_code_exit(void)
{
pr_info("dummy-code unloaded\n");
}

主站蜘蛛池模板: 卢龙县| 大兴区| 长寿区| 青岛市| 永春县| 福鼎市| 公主岭市| 德清县| 昔阳县| 当雄县| 公安县| 香港 | 烟台市| 叶城县| 周口市| 伊宁县| 通山县| 长白| 乌拉特中旗| 洛川县| 游戏| 呼玛县| 理塘县| 阿合奇县| 宁海县| 丰都县| 巴南区| 宁明县| 余庆县| 若羌县| 巴楚县| 绿春县| 裕民县| 丽水市| 乌拉特中旗| 扎赉特旗| 望都县| 铁力市| 杭锦后旗| 财经| 大邑县|