- Linux Device Driver Development Cookbook
- Rodolfo Giometti
- 165字
- 2021-06-24 13:54:13
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:
- 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__)
- 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");
}
推薦閱讀
- 樂學Windows操作系統
- Puppet實戰
- Learning Windows Server Containers
- 從零開始寫Linux內核:一書學透核心原理與實現
- 白話區塊鏈
- 蘋果電腦玩全攻略 OS X 10.8 Mountain Lion
- 巧學活用Windows 7
- Android物聯網開發細致入門與最佳實踐
- 操作系統分析
- ElasticSearch Cookbook
- Fedora 12 Linux應用基礎
- Windows 7實戰從入門到精通
- Linux應用大全 基礎與管理
- Implementing Cloud Design Patterns for AWS(Second Edition)
- 計算機應用基礎(Windows 7+Office 2010)