log.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /************** Begin of Log.h *******************************************/
  2. /************************************************************************
  3. * Log.h
  4. *
  5. * edit by jesse in 2010-07-22
  6. * 日志相关代码的头文件
  7. ************************************************************************/
  8. #if !defined(LOG_H_INCLUDED_)
  9. #define LOG_H_INCLUDED_
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif // _MSC_VER > 1000
  13. ///用户不需要使用日志,不要定义LOG_USELOG4CXX和LOG4C_ENABLE宏命令就可以了
  14. #if !defined(LOG_USELOG4CXX) && !defined(LOG4C_ENABLE)
  15. #define LOG4C(X)
  16. #define LOG4C_INIT()
  17. #define LOG4C_PARAM_CFG_FILE_NAME(strCfgFileName)
  18. #define LOG4C_PARAM_LOG_LEVEL(strLogLevel)
  19. #define LOG4C_PARAM_LOG_FILE_NAME(strLogFileName)
  20. #define LOG4C_PARAM_LOG_FILE_SIZE(iFileSize)
  21. #define LOG4C_PARAM_LOG_FILE_NUM(iFileNum)
  22. #define LOG4C_PARAM_REREAD_LOG_CFG_FILE(bReReadLogCfgFile)
  23. #define LOG4C_INIT_WITH_PARAM()
  24. #define LOG4C_INIT_WITH_PARAM_MULTI_PROCESS()
  25. #define LOG4C_FINI()
  26. #define LOG4C_INIT_DEFAULT()
  27. #define LOG4C_NO_FILENUM(X)
  28. #define LOG4C_NO_FILENUM_NO_LAYOUT(X)
  29. #define LOG4C_ORIGIN LOG4C_NO_FILENUM_NO_LAYOUT
  30. #define LOG4C_BLOCK_BEGIN(X)
  31. #define LOG4C_BLOCK_END(X)
  32. #define LOG4C_FUN(X)
  33. #define LOG4C_IF(X)
  34. #define LOG4C_LINE()
  35. #define LOG4C_HEX_DUMP(X)
  36. #define LOG4C_RETURN(X)
  37. #define LOG4C_RETURN_WITH_VALUE(X, reurnValue)
  38. #else
  39. #include <stdlib.h>
  40. /**日志记录模块初始化宏定义
  41. 注意事项: 必须初始化日志模块后才能正确记录日志
  42. 本初始化不要求应用程序所在目录有日志配置文件log4crc,会自动生成一个缺省的log4crc文件
  43. 使用例子: LOG4C_INIT_DEFAULT();
  44. 作者:Jesse 日期:2010-07-22
  45. */
  46. #define LOG4C_INIT_DEFAULT() \
  47. log_init_with_string("", "")
  48. /**日志记录模块初始化宏定义
  49. 注意事项: 必须初始化日志模块后才能正确记录日志
  50. 本初始化要求应用程序所在目录有日志配置文件log4crc才行
  51. 使用例子: LOG4C_INIT();
  52. 作者:Jesse 日期:2010-07-22
  53. */
  54. #define LOG4C_INIT() \
  55. log_init()
  56. /************************************************************************
  57. 下面的为通过设置参数设置日志模块参数的宏定义(功能和LOG4C_INIT和LOG4C_INIT_DEFAULT类似):
  58. 1.必须先调用LOG_PARAM_XXXX系列宏定义设置参数,
  59. 1.1.如设置日志记录级别、生成日志文件名等。
  60. 1.2.若不LOG_PARAM_XXXX系列宏定义则将使用默认设置参数。
  61. 2.然后调用LOG_INIT_WITH_PARAM宏定义来初始化日志模块。
  62. 3.此时就可以记录日志了。
  63. */
  64. #define LOG4C_PARAM_CFG_FILE_NAME(strCfgFileName) \
  65. log_set_log_cfg_file_name(strCfgFileName)
  66. #define LOG4C_PARAM_LOG_LEVEL(strLogLevel) \
  67. log_set_log_level(strLogLevel)
  68. #define LOG4C_PARAM_LOG_FILE_NAME(strLogFileName) \
  69. log_set_log_file_name(strLogFileName)
  70. #define LOG4C_PARAM_LOG_FILE_SIZE(iFileSize) \
  71. log_set_log_file_size(iFileSize)
  72. #define LOG4C_PARAM_LOG_FILE_NUM(iFileNum) \
  73. log_set_log_file_num(iFileNum)
  74. #define LOG4C_PARAM_REREAD_LOG_CFG_FILE(bReReadLogCfgFile) \
  75. log_set_reread_log_cfg_file(bReReadLogCfgFile)
  76. #define LOG4C_INIT_WITH_PARAM() \
  77. log_init_with_param()
  78. #define LOG4C_INIT_WITH_PARAM_MULTI_PROCESS() \
  79. log_init_with_param_multi_process()
  80. /************************************************************************/
  81. /**日志记录模块结束宏定义
  82. 注意事项: 必须正确结束日志模块后才不会造成内存、资源泄漏
  83. 使用例子: LOG4C_FINI();
  84. 作者:Jesse 日期:2010-07-22
  85. */
  86. #define LOG4C_FINI() \
  87. log_fini()
  88. ///进行LOG宏定义参数获取的结果
  89. typedef struct __LOG_PARAM
  90. {
  91. char strMsg[LOG4C_BUFFER_SIZE_DEFAULT];
  92. int iPriority;
  93. ///LOG4C_IF时的条件参数
  94. int iCondition;
  95. ///LOG4C_HEX_DUMP时的参数
  96. char *strHexBuf;
  97. int iHexBufLen;
  98. }LOG_PARAM;
  99. #define LOG4C(X) \
  100. { \
  101. const LOG_PARAM log_param = log_vsprintf_wrapper X; \
  102. log_msg( __FILE__, __LINE__, DEFAULT_LOG_CATEGORY_NAME, log_param.iPriority, log_param.strMsg ); \
  103. }
  104. #define LOG4C_NO_FILENUM(X) \
  105. { \
  106. const LOG_PARAM log_param = log_vsprintf_wrapper X; \
  107. log_msg_no_file_num( DEFAULT_LOG_CATEGORY_NAME, log_param.iPriority, log_param.strMsg ); \
  108. }
  109. #define LOG4C_NO_FILENUM_NO_LAYOUT(X) \
  110. { \
  111. const LOG_PARAM log_param = log_vsprintf_wrapper X; \
  112. log_msg_no_file_num_no_layout( DEFAULT_LOG_CATEGORY_NAME, log_param.iPriority, log_param.strMsg ); \
  113. }
  114. #define LOG4C_ORIGIN LOG4C_NO_FILENUM_NO_LAYOUT
  115. #define LOG4C_BLOCK_BEGIN(X) \
  116. { \
  117. const LOG_PARAM log_param = log_vsnprintf_wrapper_msg X; \
  118. log4c_block_begin(__FILE__, __LINE__, log_param.strMsg); \
  119. }
  120. #define LOG4C_BLOCK_END(X) \
  121. { \
  122. const LOG_PARAM log_param = log_vsnprintf_wrapper_msg X; \
  123. log4c_block_end(__FILE__, __LINE__, log_param.strMsg); \
  124. }
  125. #define LOG4C_FUN(X) \
  126. { \
  127. const LOG_PARAM log_param = log_vsnprintf_wrapper_msg X; \
  128. log_msg_func(__FILE__, __FUNCTION__, __LINE__, DEFAULT_LOG_CATEGORY_NAME, LOG4C_PRIORITY_TRACE, log_param.strMsg ); \
  129. }
  130. /** Output trace on condition.
  131. This macro outputs a trace of any information needed, using standard stream
  132. output operators. The output is only made if the conditional is TRUE.
  133. example: LOG4C_IF((4>3, LOG4C_PRIORITY_TRACE, "HELLO"));
  134. */
  135. #define LOG4C_IF(X) \
  136. { \
  137. const LOG_PARAM log_param = log_condition_vsnprintf_wrapper X; \
  138. if( log_param.iCondition ) {\
  139. log_msg( \
  140. __FILE__, \
  141. __LINE__, \
  142. DEFAULT_LOG_CATEGORY_NAME, \
  143. log_param.iPriority, \
  144. log_param.strMsg\
  145. ); \
  146. }\
  147. }
  148. /** Trace the execution of a line.
  149. This macro outputs a trace of a source file line execution.
  150. */
  151. #define LOG4C_LINE() \
  152. { \
  153. char strBuffer[128] = {0}; \
  154. snprintf(strBuffer, sizeof(strBuffer), "line:%d", __LINE__); \
  155. log_msg( \
  156. __FILE__, \
  157. __LINE__, \
  158. DEFAULT_LOG_CATEGORY_NAME, \
  159. LOG4C_PRIORITY_TRACE, \
  160. strBuffer \
  161. ); \
  162. }
  163. /**将内存中数据以十六进制方式打印出来的一个宏定义
  164. example:LOG4C_HEX_DUMP((LOG4C_PRIORITY_TRACE, strHexBuf, iHexBufLen));
  165. 作者:Jesse 日期:2010-07-22
  166. */
  167. #define LOG4C_HEX_DUMP(X) \
  168. { \
  169. log_hex_dump_vsnprintf_wrapper X; \
  170. }
  171. /** 输出文件名和行号的日志输出宏,记录完日志直接return
  172. 然后再输入到LOG宏定义中
  173. 作者:Jesse 日期:2010-07-22
  174. */
  175. #define LOG4C_RETURN(X) \
  176. { \
  177. LOG4C(X); \
  178. return; \
  179. }
  180. /** 输出文件名和行号的日志输出宏,记录完日志直接return returnValue;
  181. 然后再输入到LOG宏定义中
  182. example:LOG4C_RETURN_WITH_VALUE(( LOG4C_PRIORITY_TRACE, "message%d", 1), returnValue);
  183. 作者:Jesse 日期:2010-07-22
  184. */
  185. #define LOG4C_RETURN_WITH_VALUE(X, returnValue) \
  186. { \
  187. LOG4C(X); \
  188. return returnValue; \
  189. }
  190. #endif ///!defined(LOG_USELOG4CXX) && !defined(LOG4C_ENABLE)
  191. #if ((defined(LOG_USELOG4CXX) && defined(__cplusplus)) || defined(LOG4C_ENABLE))
  192. #ifdef _WIN32
  193. ///log4c正确使用必须的一些宏定义
  194. #define snprintf _snprintf
  195. #ifndef HAVE_CONFIG_H
  196. #define HAVE_CONFIG_H
  197. #endif
  198. ///#define LOG4C_EXPORTS
  199. #endif
  200. ///定义linux下使用到而不存在的类型
  201. #ifdef linux
  202. ///typedef int BOOL;
  203. #ifndef BOOL
  204. #define BOOL int
  205. #define FALSE 0
  206. #define TRUE 1
  207. #endif
  208. #define sprintf_s snprintf
  209. ///#define strcpy_s strncpy
  210. #endif
  211. /// 日志输出缺省category
  212. #ifndef DEFAULT_LOG_CATEGORY_NAME
  213. #define DEFAULT_LOG_CATEGORY_NAME "root"
  214. #endif
  215. /** 日志记录级别
  216. */
  217. /** fatal */ #define LOG_FATAL LOG4C_PRIORITY_FATAL
  218. /** alert */ #define LOG_ALERT LOG4C_PRIORITY_ALERT
  219. /** crit */ #define LOG_CRIT LOG4C_PRIORITY_CRIT
  220. /** error */ #define LOG_ERROR LOG4C_PRIORITY_ERROR
  221. /** warn */ #define LOG_WARN LOG4C_PRIORITY_WARN
  222. /** notice */ #define LOG_NOTICE LOG4C_PRIORITY_NOTICE
  223. /** info */ #define LOG_INFO LOG4C_PRIORITY_INFO
  224. /** debug */ #define LOG_DEBUG LOG4C_PRIORITY_DEBUG
  225. /** trace */ #define LOG_TRACE LOG4C_PRIORITY_TRACE
  226. /** notset */ #define LOG_NOTSET LOG4C_PRIORITY_NOTSET
  227. /** unknown */ #define LOG_UNKNOWN LOG4C_PRIORITY_UNKNOWN
  228. #include <log4c/defs.h>
  229. __LOG4C_BEGIN_DECLS
  230. /** 日志模块初始化
  231. @return int:return 0 for success
  232. 作者:Jesse 日期:2010-07-22
  233. */
  234. LOG4C_API int log_init();
  235. /** 日志模块初始化,指定配置文件名称
  236. @return int:return 0 for success
  237. 作者:Jesse 日期:2010-07-22
  238. */
  239. LOG4C_API int log_init_with_cfg_file(const char *strCfgFileName);
  240. /** 日志模块清理
  241. @return int:return 0 for success
  242. 作者:Jesse 日期:2010-07-22
  243. */
  244. LOG4C_API int log_fini();
  245. /**宏参数抽取priority函数
  246. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回priority的值
  247. @return const int : iPriority.
  248. 作者:Jesse 日期:2010-07-22
  249. */
  250. LOG4C_API const int log_get_priority_wrapper(
  251. const int iPriority, ///日志记录级别
  252. const char* strFormat, ///日志内容格式
  253. ... ///日志内容
  254. );
  255. /**
  256. 将大小为count的缓冲区中内容按字节以16进制字符串打印出来,
  257. 返回值即为指向相应的字符串,
  258. 该返回指向的存储区域要调用本函数的用户显示的进行删除
  259. */
  260. LOG4C_API void log4c_sprintf_data(char *buff, int count, char *dest_buffer);
  261. /**日志记录宏定义
  262. 注意事项: 使用时参数必须使用两个括号括起来,LOG4C((X));见下面例子
  263. 使用例子: LOG4C((LOG_ERROR, "Hello World! My Name is %s, and my age is %d ", "Jess", "28" ));
  264. 作者:Jesse 日期:2010-07-22
  265. */
  266. LOG4C_API const LOG_PARAM log_vsprintf_wrapper(
  267. const int iPriority, ///日志记录级别
  268. const char* strFormat, ///日志内容格式
  269. ... ///日志内容
  270. );
  271. LOG4C_API const LOG_PARAM log_condition_vsnprintf_wrapper(
  272. const int iCondition, ///条件
  273. const int iPriority, ///日志记录级别
  274. const char* strFormat, ///日志内容格式
  275. ... ///日志内容
  276. );
  277. LOG4C_API void log_hex_dump_vsnprintf_wrapper(
  278. const int iPriority, ///日志记录级别
  279. const char* strFormat, ///日志内容格式
  280. const char* strHexBuf, ///缓冲区首地址
  281. const int iHexBufLen, ///缓冲区长度
  282. ... ///日志内容
  283. );
  284. LOG4C_API const LOG_PARAM log_vsnprintf_wrapper_msg(
  285. const char* strFormat, ///日志内容格式
  286. ... ///日志内容
  287. );
  288. /** 日志记录
  289. 日志记录为一个字符串指针指向的内容
  290. @return void
  291. 作者:Jesse 日期:2010-07-22
  292. */
  293. LOG4C_API void log_msg(
  294. const char *strFile, ///文件名
  295. const int iLineNum, ///行号
  296. const char *strCatName, ///category名
  297. const int iPriority, ///日志记录级别
  298. const char *strFormat, ///日志内容格式
  299. ... ///日志内容
  300. );
  301. LOG4C_API void log_msg_func(
  302. const char *strFile, ///文件名
  303. const char *strFuncName,///函数名
  304. const int iLineNum, ///行号
  305. const char *strCatName, ///category名
  306. const int iPriority, ///日志记录级别
  307. const char *strFormat, ///日志内容格式
  308. ... ///日志内容
  309. );
  310. /** 日志记录,不记录文件名和行号
  311. 日志记录为一个字符串指针指向的内容
  312. @return void
  313. 作者:Jesse 日期:2010-07-22
  314. */
  315. LOG4C_API void log_msg_no_file_num(
  316. const char *strCatName, ///category名
  317. const int iPriority, ///日志记录级别
  318. const char *strFormat, ///日志内容格式
  319. ... ///日志内容
  320. );
  321. /** 日志记录,不记录文件名和行号,没有任何layout转换,直接输出相应的字符文本到日志中
  322. 此条记录没有行号,也没有线程号,也没有回车等
  323. 日志记录为一个字符串指针指向的内容
  324. @return void
  325. 作者:Jesse 日期:2010-07-22
  326. */
  327. LOG4C_API void log_msg_no_file_num_no_layout(
  328. const char *strCatName, ///category名
  329. const int iPriority, ///日志记录级别
  330. const char *strFormat, ///日志内容格式
  331. ... ///日志内容
  332. );
  333. /** 日志模块初始化
  334. 以日志配置文件字符串内容和日志配置文件命作为参数
  335. 若相应配置文件存在则使用,否则按照给定内容和文件名创建日志配置文件并使用之
  336. @return int,0 表成功
  337. 作者:Jesse 日期:2010-07-22
  338. */
  339. LOG4C_API int log_init_with_string(
  340. const char *strFileContent,
  341. const char *strFileName
  342. );
  343. /** 日志模块初始化
  344. 以日志配置文件命作为参数
  345. @return int,0 表成功
  346. 作者:Jesse 日期:2010-07-22
  347. */
  348. LOG4C_API int log_init_with_cfg_file_wrapper(
  349. const char * strConfigFile
  350. );
  351. /** 日志模块初始化-日志配置文件的文件名设置
  352. 以日志配置文件命作为参数
  353. @return void
  354. 作者:Jesse 日期:2010-07-22
  355. */
  356. LOG4C_API void log_set_log_cfg_file_name(const char *strFileName);
  357. /** 日志模块初始化-生成的日志文件的文件名设置
  358. 以日志文件命作为参数
  359. @return void
  360. 作者:Jesse 日期:2010-07-22
  361. */
  362. LOG4C_API void log_set_log_file_name(const char *strFileName);
  363. /** 日志模块初始化-日志记录级别设置
  364. @return void
  365. 作者:Jesse 日期:2010-07-22
  366. */
  367. LOG4C_API void log_set_log_level(const char *strLogLevel);
  368. /** 日志模块初始化-日志记录文件的大设置
  369. @return void
  370. 作者:Jesse 日期:2010-07-22
  371. */
  372. LOG4C_API void log_set_log_file_size(const int iFileSize);
  373. /** 日志模块初始化-日志记录文件的个数设置
  374. 记录到最大个数后将回滚重复覆盖第一个日志文件,依次类推
  375. @return void
  376. 作者:Jesse 日期:2010-07-22
  377. */
  378. LOG4C_API void log_set_log_file_num(const int iFileNum);
  379. /** 日志模块初始化-是否实时读取日志配置文件
  380. 设置是否每次记录时都读取日志配置文件
  381. @param:const BOOL m_bReReadLogCfgFile
  382. @return void
  383. 作者:Jesse 日期:2010-07-22
  384. */
  385. LOG4C_API void log_set_reread_log_cfg_file(const BOOL bReReadLogCfgFile);
  386. /** 日志模块初始化-带参数进行日志模块初始化
  387. 这里的参数则为上面的几个API设置的参数,
  388. 所以本函数一定要在日志配置参数设置好之后调用设置的参数才能生效的
  389. @return int,OK,成功;FAILURE,失败
  390. 作者:Jesse 日期:2010-07-22
  391. */
  392. LOG4C_API int log_init_with_param();
  393. /** 日志模块初始化-带参数进行日志模块初始化,程序的多个运行实例产生的日志不互相冲突
  394. 这里的参数则为上面的几个API设置的参数,
  395. 所以本函数一定要在日志配置参数设置好之后调用设置的参数才能生效的
  396. @return int,OK,成功;FAILURE,失败
  397. 作者:Jesse 日期:2010-07-22
  398. */
  399. LOG4C_API int log_init_with_param_multi_process();
  400. /** 日志记录Block方式能清楚的表明调用层次
  401. 类似下面这种B-Entry类型日志:
  402. main.cpp(87) B-Entry ==> main
  403. main.cpp(90) B-Entry ====> main_01
  404. main.cpp(92) B-Entry ======> main_02
  405. main.cpp(92) B-Exit <====== main_02
  406. main.cpp(90) B-Exit <==== main_01
  407. @return void
  408. 作者:Jesse 日期:2010-07-22
  409. */
  410. LOG4C_API void log4c_block_begin(const char * fileName, int lineNum, const char * traceName);
  411. /** 日志记录Block方式能清楚的表明调用层次
  412. 类似下面这种B-Exit类型日志:
  413. main.cpp(87) B-Entry ==> main
  414. main.cpp(90) B-Entry ====> main_01
  415. main.cpp(92) B-Entry ======> main_02
  416. main.cpp(92) B-Exit <====== main_02
  417. main.cpp(90) B-Exit <==== main_01
  418. @return void
  419. 作者:Jesse 日期:2010-07-22
  420. */
  421. LOG4C_API void log4c_block_end(const char * fileName, int lineNum, const char * traceName);
  422. /**检测配置文件是否存在
  423. @return const int : iPriority.
  424. 作者:Jesse 日期:2010-07-22
  425. */
  426. extern const int log_check(void);
  427. /**检测配置文件是否存在,只检测传入的配置文件名
  428. @return const int : iPriority.
  429. 作者:Jesse 日期:2010-07-22
  430. */
  431. extern const int log_check_with_cfg_file(const char *strCfgFileName);
  432. /**宏参数抽取format函数
  433. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回format的值
  434. @return const char* : strFormat
  435. 作者:Jesse 日期:2010-07-22
  436. */
  437. extern const char* log_get_format_wrapper(
  438. const int iPriority, ///日志记录级别
  439. const char* strFormat, ///日志内容格式
  440. ... ///日志内容
  441. );
  442. /** 设置appender
  443. @return int:
  444. 作者:Jesse 日期:2010-07-22
  445. */
  446. extern int log_setappender(
  447. const char *strCatName, ///category名
  448. const char *strAppenderName ///appender名
  449. );
  450. /** 日志记录
  451. 支持类似printf函数的带格式输出
  452. @return void
  453. 作者:Jesse 日期:2010-07-22
  454. */
  455. extern void log_log(
  456. const char *strCatName, ///category名
  457. const int iPriority, ///日志记录级别
  458. const char *strFormat, ///日志内容格式
  459. ... ///日志内容
  460. );
  461. /** 日志模块初始化-日志模块使用实例个数自增
  462. @return void
  463. 作者:Jesse 日期:2010-07-22
  464. */
  465. extern void IncreaseLogModuleUsage();
  466. __LOG4C_END_DECLS
  467. ///#include "log4c.h"
  468. #endif ///((defined(LOG_USELOG4CXX) && defined(__cplusplus)) || defined(LOG4C_ENABLE))
  469. #endif // !defined(LOG_H_INCLUDED_)
  470. /************** End of Log.h *******************************************/