荟萃馆

位置:首页 > 计算机 > C语言

c语言中log的用法指导

C语言1.2W

C语言是一门实践性和动手能力要求很高的大学主干课程,但是C语言实验课的教学一直不受重视,教学效果也不太理想。下面小编就跟你们详细介绍下c语言中log的用法的用法,希望对你们有用。

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

别的系统请百度/GOOGLE找相关编译安装当。log4c官网:

文件:

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

rn "C"

09.{

10.#endif

11.

12.#include "log4c.h"

13.

14.#ifdef __cplusplus

15.}

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.

rn int log_open(const char *category);

rn void log_message(int priority ,const char* fmt, ...);

rn void log_trace(const char *file , int line , const char *func, const char *fmt ,...);

rn int log_close();

28.

29.#define LOG_ERROR(fmt , args...)

30. log_message(LOG_PRI_ERROR, fmt, ##args)

31.#define LOG_WARN(fmt, args...)

32. log_message(LOG_PRI_WARN, fmt , ##args)

33.#define LOG_NOTICE(fmt , args...)

34. log_message(LOG_PRI_NOTICE, fmt , ##args)

35.#define LOG_DEBUG(fmt , args...)

36. log_message(LOG_PRI_DEBUG, fmt , ##args)

37.#define LOG_TRACE(fmt,args...)

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.

ic log4c_category_t *log_category = NULL;

07.

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.

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.

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);

40.

41. va_start(ap , fmt);

42. log4c_category_vlog(log_category , LOG4C_PRIORITY_TRACE, new_fmt , ap);

43. va_end(ap);

44.}

45.

46.

log_close()

48.{

49. return (log4c_fini());

50.}

//test-log.c

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

01.#include

02.#include "log.h"

03.

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.

06.

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里面用法也很简单

log_open("category_name"); //category_name一定得是log4crc里面已经定义的category.

关于配置文件log4crc

更复杂的配置参见:

配置文件的搜索是由LOG4C_RCPATH环境变量决定。搜索的配置文件名为log4crc(不知道能否改变,没研究过)

配置文件中category的priority不知道是什么意思,,反正好像没什么用。不管设置成什么,好像都不影响。

环境变量:

?LOG4C_RCPATH holds the path to the main log4crc configuration file #环境变量若未设置,则在工作目录(一般为运行目录)搜索log4crc配置文件. 如果设置了此变量,则所以用log4c库的程序都会使用此路径下的log4c配置文件(可根据category区分).

?LOG4C_PRIORITY holds the "root" category priority #改变root的priority,,

?LOG4C_APPENDER holds the "root" category appender #改变root的appender,,因为root默认没设置appender.

标签:log 指导 语言