资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
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信息中的输出格式的等。 ::/blog.csdn.net/fdl19881/article/details/8192363 例子: 系统:ubuntu12.10. 准备: 安装log4c库,sudoapt-getinstallliblog4c-devliblog4c-doc 别的系统请百度/GOOGLE找相关编译安装当。log4c官网::/log4c.sourceforge.net/ 文件: log.hlog.c自己将log4c重新封装的函数 test-log.c测试用的主函数 log4crc配置文件(xml,照着写就行) /log.h cppviewplaincopy 01.#ifndef_LOG_H_ 02.#define_LOG_H_ 03. 04.#include 05.#include 06. 07.#ifdef_cplusplus 08.externC 09. 10.#endif 11. 12.#includelog4c.h 13. 14.#ifdef_cplusplus 15. 16.#endif 17. 18.#defineLOG_PRI_ERRORLOG4C_PRIORITY_ERROR 19.#defineLOG_PRI_WARNLOG4C_PRIORITY_WARN 20.#defineLOG_PRI_NOTICELOG4C_PRIORITY_NOTICE 21.#defineLOG_PRI_DEBUGLOG4C_PRIORITY_DEBUG 22.#defineLOG_PRI_TRACELOG4C_PRIORITY_TRACE 23. 24.externintlog_open(constchar*category); 25.externvoidlog_message(intpriority,constchar*fmt,.); 26.externvoidlog_trace(constchar*file,intline,constchar*func,constchar*fmt,.); 27.externintlog_close(); 28. 29.#defineLOG_ERROR(fmt,args.) 30.log_message(LOG_PRI_ERROR,fmt,#args) 31.#defineLOG_WARN(fmt,args.) 32.log_message(LOG_PRI_WARN,fmt,#args) 33.#defineLOG_NOTICE(fmt,args.) 34.log_message(LOG_PRI_NOTICE,fmt,#args) 35.#defineLOG_DEBUG(fmt,args.) 36.log_message(LOG_PRI_DEBUG,fmt,#args) 37.#defineLOG_TRACE(fmt,args.) 38.log_trace(_FILE_,_LINE_,_FUNCTION_,fmt,#args) 39. 40. 41.#endif /log.c cppviewplaincopy在CODE上查看代码片派生到我的代码片 01.#include 02.#include 03.#includelog.h 04. 05. 06.staticlog4c_category_t*log_category=NULL; 07. 08.intlog_open(constchar*category) 09. 10.if(log4c_init()=1) 11. 12.return-1; 13. 14.log_category=log4c_category_get(category); 15.return0; 16. 17. 18.voidlog_message(intpriority,constchar*fmt,.) 19. 20.va_listap; 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.voidlog_trace(constchar*file,intline,constchar*fun, 30.constchar*fmt,.) 31. 32.charnew_fmt2048; 33.constchar*head_fmt=file:%s,line:%d,function:%s; 34.va_listap; 35.intn; 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. 47.intlog_close() 48. 49.return(log4c_fini(); 50. /test-log.c cppviewplaincopy在CODE上查看代码片派生到我的代码片 01.#include 02.#includelog.h 03. 04.intmain(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(hellolog4c!); 12.log_close(); 13.return0; 14. /配置文件,默认名为log4crc htmlviewplaincopy在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. 编译命令: pythonviewplaincopy在CODE上查看代码片派生到我的代码片 01.gcctest-log.clog.c-otest-log-llog4c 运行效果 ./test-log stdoutTRACEmycat-file:test-log.c,line:7,function:maintrace stdoutERRORmycat-error stdoutWARNmycat-warn stdoutNOTICEmycat-notice stdoutDEBUGmycat-hellolog4c! 讲解: 关于log.h,log.c封装的内容大家可以看看,用到了可变参数宏,可变参数这些。百度一下,就有很多人讲解了。这里就不说了。 log.h与log.c里面用法也很简单 log_open(category_name);/category_name一定得是log4crc里面已经定义的category. 关于配置文件log4crc 更复杂的配置参见::/xueqi.iteye./blog/1570013 配置文件的搜索是由LOG4C_RCPATH环境变量决定。搜索的配置文件名为log4crc(不知道能否改变,没研究过) 配置文件中category的priority不知道是什么意思,反正好像没什么用。不管设置成什么,好像都不影响。 环境变量: ?LOG4C_RCPATHholdsthepathtothemainlog4cr
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号