在c语⾔中log函数的作⽤,c语⾔中log的⽤法指导
c语⾔中log的⽤法指导
C语⾔是⼀门实践性和动⼿能⼒要求很⾼的⼤学主⼲课程,但是C语⾔实验课的教学⼀直不受重视,教学效果也不太理想。下⾯⼩编就跟你们详细介绍下c语⾔中log的⽤法的⽤法,希望对你们有⽤。
Log4c中有三个重要的概念, Category, Appender, Layout。
Category⽤于区分不同的Logger, 其实它就是个logger。在⼀个程序中我们可以通过Category来指定很多的Logger,⽤于不同的⽬的。
Appdender⽤于描述输出流,通过为Category来指定⼀个Appdender,可以决定将log信息来输出到什么地⽅去,⽐如stdout, stderr, ⽂件, 或者是socket等等
Layout⽤于指定⽇志信息的格式,通过为Appender来指定⼀个Layout,可以决定log信息以何种格式来输出,⽐如是否有带有时间戳, 是否包含⽂件位置信息等,以及他们在⼀条log信息中的.输出格式的等。
例⼦:
系统:ubuntu12.10 .
准备:
安装log4c库, sudo apt-get install liblog4c-dev liblog4c-doc
⽂件:
log.h log.c ⾃⼰将log4c重新封装的函数
test-log.c 测试⽤的主函数
log4crc 配置⽂件(xml,照着写就⾏)
//log.h
[cpp] view plain copy
01.#ifndef _LOG_H_
02.#define _LOG_H_
03.
04.#include
05.#include
06.
07.#ifdef __cplusplus
< "C"
09.{
10.#endif
11.
12.#include "log4c.h"
13.
14.#ifdef __cplusplus
16.#endif
17.
18.#define LOG_PRI_ERROR LOG4C_PRIORITY_ERROR
19.#define LOG_PRI_WARN LOG4C_PRIORITY_WARN
20.#define LOG_PRI_NOTICE LOG4C_PRIORITY_NOTICE
21.#define LOG_PRI_DEBUG LOG4C_PRIORITY_DEBUG
22.#define LOG_PRI_TRACE LOG4C_PRIORITY_TRACE
23.
< int log_open(const char *category);
< void log_message(int priority ,const char* fmt, ...);
< void log_trace(const char *file , int line , const char *func, const char *fmt ,...);
< int log_close();
28.
29.#define LOG_ERROR(fmt , )
30. log_message(LOG_PRI_ERROR, fmt, ##args)
31.#define LOG_WARN(fmt, )
32. log_message(LOG_PRI_WARN, fmt , ##args)
33.#define LOG_NOTICE(fmt , )
34. log_message(LOG_PRI_NOTICE, fmt , ##args)
35.#define LOG_DEBUG(fmt , )
36. log_message(LOG_PRI_DEBUG, fmt , ##args)
37.#define LOG_TRACE()
38. log_trace(__FILE__ , __LINE__ , __FUNCTION__ , fmt ,## args)
39.
40.
41.#endif
//log.c
[cpp] view plain copy 在CODE上查看代码⽚派⽣到我的代码⽚
01.#include
02.#include
03.#include "log.h"
04.
05.
07.
08.int log_open(const char *category)
09.{
10. if (log4c_init() == 1)
11. {
12. return -1;
13. }
14. log_category = log4c_category_get(category);
15. return 0 ;
16.}
17.
18.void log_message(int priority , const char *fmt , ...)
19.{
20. va_list ap;
21.
22. assert(log_category != NULL);
23.
24. va_start(ap, fmt);
25. log4c_category_vlog(log_category , priority , fmt , ap);
26. va_end(ap);
27.}
28.
29.void log_trace(const char *file, int line, const char *fun,
30. const char *fmt , ...)
31.{
32. char new_fmt[2048];
33. const char *head_fmt = "[file:%s, line:%d, function:%s]";
34. va_list ap;
35. int n;
36.
37. assert(log_category != NULL);
38. n = sprintf(new_fmt, head_fmt , file , line , fun);
39. strcat(new_fmt + n , fmt);
41. va_start(ap , fmt);const的作用
42. log4c_category_vlog(log_category , LOG4C_PRIORITY_TRACE, new_fmt , ap);
43. va_end(ap);
44.}
45.
46.
47.int log_close()
48.{
49. return (log4c_fini());
50.}
/
/test-log.c
[cpp] view plain copy 在CODE上查看代码⽚派⽣到我的代码⽚
01.#include
02.#include "log.h"
03.
04.int main(void)
05.{
06. log_open("mycat");
07. LOG_TRACE("trace");
08. LOG_ERROR("error");
09. LOG_WARN("warn");
10. LOG_NOTICE("notice");
11. LOG_DEBUG("hello log4c!");
12. log_close();
13. return 0;
14.}
//配置⽂件,默认名为log4crc
[html] view plain copy 在CODE上查看代码⽚派⽣到我的代码⽚
01.
02.
03.
04.
05.
07.0
08.
09.0
10.1
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
编译命令:
[python] view plain copy 在CODE上查看代码⽚派⽣到我的代码⽚
< test-log.c log.c -o test-log -llog4c
运⾏效果
./test-log
[stdout] TRACE mycat - [file:test-log.c, line:7, function:main]trace
[stdout] ERROR mycat - error
[stdout] WARN mycat - warn
[stdout] NOTICE mycat - notice
[stdout] DEBUG mycat - hello log4c!
讲解:
关于log.h ,log.c封装的内容⼤家可以看看,⽤到了可变参数宏,可变参数这些。百度⼀下,就有很多⼈讲解了。这⾥就不说了。log.h与log.c⾥⾯⽤法也很简单