- 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");
}
推薦閱讀
- Linux設備驅動開發(fā)詳解:基于最新的Linux4.0內核
- 每天5分鐘玩轉Kubernetes
- Linux系統(tǒng)文件安全實戰(zhàn)全攻略
- PLC控制系統(tǒng)應用與維護
- 構建可擴展分布式系統(tǒng):方法與實踐
- macOS效率手冊
- Java EE 8 Design Patterns and Best Practices
- Instant Optimizing Embedded Systems using Busybox
- Learning Magento 2 Administration
- Windows Server 2012網(wǎng)絡操作系統(tǒng)項目教程(第4版)
- 深入淺出Node.js
- Hands-On UX Design for Developers
- 嵌入式微系統(tǒng)
- OpenHarmony開發(fā)與實踐:基于紅莓RK2206開發(fā)板
- 操作系統(tǒng)之哲學原理第2版