printf/printk 增加tag

本文深入探讨了在C语言中如何自定义打印函数,包括使用printf()和printk()函数进行日志记录的方法。通过具体示例展示了如何在代码中实现自定义的日志输出格式,这对于理解和调试程序运行过程中的状态具有重要作用。

1.printf()自定义函数
#define DEBUG
#define LOG_TAG "MIC_DEBUG"
 
#ifdef DEBUG
#include <stdio.h> 
#define debug(fmt, x...) printf("%s: %s() line: %d "fmt, LOG_TAG, __FUNCTION__, __LINE__, ##x);
#else
#define debug(fmt, x...)
#endif
 
int main()
{
   int num = 4567;
   debug("num = %d\n", num);
}
 
 
2.printk()自定义函数
1、定义
#define LOG_TAG "[Camera]: %s() line: %d " 
#define Camera_ERR(fmt, args...)  printk(KERN_ERR LOG_TAG fmt, __FUNCTION__, __LINE__,  ##args) 
2、打印函数
void test(){
   Camera_ERR("%s\n", __func__);
 }
 
3、输出log为:
  [Camera] test
 

/home/wangjuncong/Documents/practice2/helloworld.c: In function ‘init_module’: /home/wangjuncong/Documents/practice2/helloworld.c:6:22: error: stray ‘\342’ in program 6 | printk(KERN_INFO <U+201C>Hello World!\n<U+201D>); | ^~~~~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:5: note: in expansion of macro ‘printk’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:23: error: expected ‘)’ before ‘Hello’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:5: note: in expansion of macro ‘printk’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~~ ./include/linux/printk.h:429:24: note: to match this ‘(’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^ ./include/linux/printk.h:457:26: note: in expansion of macro ‘printk_index_wrap’ 457 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:5: note: in expansion of macro ‘printk’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:35: error: stray ‘\’ in program 6 | printk(KERN_INFO “Hello World!\n”); | ^ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:5: note: in expansion of macro ‘printk’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:37: error: stray ‘\342’ in program 6 | printk(KERN_INFO <U+201C>Hello World!\n<U+201D>); | ^~~~~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:6:5: note: in expansion of macro ‘printk’ 6 | printk(KERN_INFO “Hello World!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c: In function ‘cleanup_module’: /home/wangjuncong/Documents/practice2/helloworld.c:12:22: error: stray ‘\342’ in program 12 | printk(KERN_INFO <U+201C>Goodbye!\n<U+201D>); | ^~~~~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:5: note: in expansion of macro ‘printk’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:23: error: expected ‘)’ before ‘Goodbye’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:5: note: in expansion of macro ‘printk’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~ ./include/linux/printk.h:429:24: note: to match this ‘(’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^ ./include/linux/printk.h:457:26: note: in expansion of macro ‘printk_index_wrap’ 457 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__) | ^~~~~~~~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:5: note: in expansion of macro ‘printk’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:31: error: stray ‘\’ in program 12 | printk(KERN_INFO “Goodbye!\n”); | ^ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:5: note: in expansion of macro ‘printk’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:33: error: stray ‘\342’ in program 12 | printk(KERN_INFO <U+201C>Goodbye!\n<U+201D>); | ^~~~~~~~ ./include/linux/printk.h:429:25: note: in definition of macro ‘printk_index_wrap’ 429 | _p_func(_fmt, ##__VA_ARGS__); \ | ^~~~ /home/wangjuncong/Documents/practice2/helloworld.c:12:5: note: in expansion of macro ‘printk’ 12 | printk(KERN_INFO “Goodbye!\n”); | ^~~~~~ In file included from ./include/linux/module.h:22: /home/wangjuncong/Documents/practice2/helloworld.c: At top level: /home/wangjuncong/Documents/practice2/helloworld.c:15:16: error: stray ‘\342’ in program 15 | MODULE_LICENSE(<U+201C>GPL<U+201D>); | ^~~~~~~~ ./include/linux/moduleparam.h:26:61: note: in definition of macro ‘__MODULE_INFO’ 26 | = __MODULE_INFO_PREFIX __stringify(tag) "=" info | ^~~~ ./include/linux/module.h:230:46: note: in expansion of macro ‘MODULE_INFO’ 230 | #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license) | ^~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:15:1: note: in expansion of macro ‘MODULE_LICENSE’ 15 | MODULE_LICENSE(“GPL”); | ^~~~~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:15:17: error: expected ‘,’ or ‘;’ before ‘GPL’ 15 | MODULE_LICENSE(“GPL”); | ^~~ ./include/linux/moduleparam.h:26:61: note: in definition of macro ‘__MODULE_INFO’ 26 | = __MODULE_INFO_PREFIX __stringify(tag) "=" info | ^~~~ ./include/linux/module.h:230:46: note: in expansion of macro ‘MODULE_INFO’ 230 | #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license) | ^~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:15:1: note: in expansion of macro ‘MODULE_LICENSE’ 15 | MODULE_LICENSE(“GPL”); | ^~~~~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:15:20: error: stray ‘\342’ in program 15 | MODULE_LICENSE(<U+201C>GPL<U+201D>); | ^~~~~~~~ ./include/linux/moduleparam.h:26:61: note: in definition of macro ‘__MODULE_INFO’ 26 | = __MODULE_INFO_PREFIX __stringify(tag) "=" info | ^~~~ ./include/linux/module.h:230:46: note: in expansion of macro ‘MODULE_INFO’ 230 | #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license) | ^~~~~~~~~~~ /home/wangjuncong/Documents/practice2/helloworld.c:15:1: note: in expansion of macro ‘MODULE_LICENSE’ 15 | MODULE_LICENSE(“GPL”); | ^~~~~~~~~~~~~~ make[3]: *** [scripts/Makefile.build:243:/home/wangjuncong/Documents/practice2/helloworld.o] 错误 1 make[2]: *** [/usr/src/linux-headers-6.8.0-64-generic/Makefile:1925:/home/wangjuncong/Documents/practice2] 错误 2 make[1]: *** [Makefile:240:__sub-make] 错误 2 make[1]: 离开目录“/usr/src/linux-headers-6.8.0-64-generic” make: *** [Makefile:19:default] 错误 2 怎么解决
08-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值