log.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /************** Begin of log.c *******************************************/
  2. /*
  3. * log.cpp
  4. *
  5. * added by jesse in 2008-2-3
  6. * 日志相关代码的定义文件
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #endif
  10. #include "log4c/config.h"
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifdef HAVE_ALLOCA_H
  14. #include <alloca.h>
  15. #endif
  16. #include <sd/sprintf.h>
  17. #include <sd/malloc.h>
  18. #include <sd/factory.h>
  19. #include <log4c/appender.h>
  20. #include <log4c/priority.h>
  21. #include <log4c/logging_event.h>
  22. #include <log4c/category.h>
  23. #include <log4c/rc.h>
  24. #include <sd/error.h>
  25. #include <sd/sd_xplatform.h>
  26. #include <sd/sd_xplatform.h>
  27. #include "log4c/vos.h"
  28. #include "log4c/log.h"
  29. #include "GlobalMacro.h"
  30. //////////////////////////////////////////////////////////////////////////
  31. ///下面括起来代码和log_check拷贝自init.c
  32. typedef struct log_c_rcfile
  33. {
  34. char name[256];
  35. time_t ctime;
  36. int exists;
  37. } log_c_rcfile_t;
  38. static log_c_rcfile_t log_c_rcfiles[] =
  39. {
  40. #if 1
  41. { "$LOG4C_RCPATH/log4crc" },
  42. { "$HOME/.log4crc" },
  43. #endif
  44. { "./log4crc" }
  45. };
  46. static const int log_c_nrcfiles = sizeof(log_c_rcfiles) / sizeof(log_c_rcfiles[0]);
  47. //////////////////////////////////////////////////////////////////////////
  48. static SEM_ID gs_semphore = NULL;
  49. static int gs_iModuleUsage_shmid = 0;
  50. //////////////////////////////////////////////////////////////////////////
  51. /** 日志模块初始化
  52. @return int:return 0 for success
  53. 作者:jesse 日期:2008-2-3
  54. */
  55. int log_init()
  56. {
  57. ///设置两个环境变量SD_DEBUG和SD_ERROR,使sd_debug和sd_error两个函数起作用
  58. ///putenv("SD_DEBUG=SD_DEBUG");
  59. ///putenv("SD_ERROR=SD_ERROR");
  60. log_check();
  61. return log4c_init();
  62. }
  63. /** 日志模块初始化,指定配置文件名称
  64. @return int:return 0 for success
  65. 作者:jesse 日期:2008-9-5
  66. */
  67. int log_init_with_cfg_file(const char *strCfgFileName)
  68. {
  69. log_check_with_cfg_file(strCfgFileName);
  70. return(log4c_init_with_cfg_file(strCfgFileName));
  71. }
  72. extern void ReleaseModuleUsage();
  73. /** 日志模块清理
  74. @return int:return 0 for success
  75. 作者:jesse 日期:2008-2-3
  76. */
  77. int log_fini()
  78. {
  79. int iResult = log4c_fini();
  80. // 解决资源泄漏 [4/18/2009 jesse]
  81. VOS_DELETE_SEM(gs_semphore);
  82. ReleaseModuleUsage();
  83. return iResult;
  84. }
  85. /**检测配置文件是否存在
  86. @return const int : iPriority.
  87. 作者:jesse 日期:2008-2-18
  88. */
  89. const int log_check(void)
  90. {
  91. int iReturn = 0;
  92. BOOL bCfgFileExist = FALSE;
  93. int i = 0;
  94. printf("check configuration files...\n");
  95. /* check configuration files */
  96. printf("looking for conf files...\n");
  97. #if 1
  98. _snprintf_s(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1,_TRUNCATE, "%s/log4crc",
  99. getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
  100. _snprintf_s(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, _TRUNCATE, "%s/.log4crc",
  101. getenv("HOME") ? getenv("HOME") : "");
  102. #endif
  103. for (i = 0; i < log_c_nrcfiles; i++)
  104. {
  105. printf("checking for conf file at '%s'\n", log_c_rcfiles[i].name);
  106. if (SD_ACCESS_READ(log_c_rcfiles[i].name))
  107. {
  108. continue;
  109. }
  110. if (SD_STAT_CTIME(log_c_rcfiles[i].name,&log_c_rcfiles[i].ctime) != 0)
  111. {
  112. sd_error("sd_stat_ctime %s failed", log_c_rcfiles[i].name);
  113. }
  114. log_c_rcfiles[i].exists=1;
  115. bCfgFileExist = TRUE;
  116. #if 0
  117. if (log4c_load(log_c_rcfiles[i].name) == -1)
  118. {
  119. sd_error("loading %s failed", log_c_rcfiles[i].name);
  120. iReturn = -1;
  121. }
  122. else
  123. {
  124. printf("loading %s succeeded\n", log_c_rcfiles[i].name);
  125. break;
  126. }
  127. #endif
  128. }
  129. if (TRUE == bCfgFileExist)
  130. {
  131. printf("find configuration file\n");
  132. }
  133. else
  134. {
  135. printf("can't find configuration file,\nplease put you conf file in the same dir of execute file.\n");
  136. }
  137. return iReturn;
  138. }
  139. /**检测配置文件是否存在,只检测传入的配置文件名
  140. @return const int : iPriority.
  141. 作者:jesse 日期:2008-9-4
  142. */
  143. const int log_check_with_cfg_file(const char *strCfgFileName)
  144. {
  145. int iReturn = 0;
  146. BOOL bCfgFileExist = FALSE;
  147. int i = 0;
  148. printf("check configuration files...\n");
  149. /* check configuration files */
  150. printf("looking for conf files...\n");
  151. #if 1
  152. _snprintf_s(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1, _TRUNCATE, "%s/log4crc",
  153. getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
  154. _snprintf_s(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, _TRUNCATE, "%s/.log4crc",
  155. getenv("HOME") ? getenv("HOME") : "");
  156. #endif
  157. if(NULL == strCfgFileName)
  158. {
  159. iReturn = -1;
  160. }
  161. else
  162. {
  163. printf("checking for conf file at '%s'\n", strCfgFileName);
  164. if (SD_ACCESS_READ(strCfgFileName))
  165. {
  166. iReturn = -1;
  167. }
  168. else
  169. {
  170. if (SD_STAT_CTIME(strCfgFileName,&log_c_rcfiles[i].ctime) != 0)
  171. {
  172. sd_error("sd_stat_ctime %s failed", strCfgFileName);
  173. }
  174. bCfgFileExist = TRUE;
  175. #if 0
  176. if (log4c_load(strCfgFileName) == -1)
  177. {
  178. sd_error("loading %s failed", strCfgFileName);
  179. iReturn = -1;
  180. }
  181. else
  182. {
  183. printf("loading %s succeeded\n", strCfgFileName);
  184. }
  185. #endif
  186. }
  187. }
  188. if (TRUE == bCfgFileExist)
  189. {
  190. printf("find configuration file\n");
  191. }
  192. else
  193. {
  194. printf("can't find configuration file,\nplease put you conf file in the same dir of execute file.\n");
  195. }
  196. return iReturn;
  197. }
  198. /** 日志记录
  199. 日志记录为一个字符串指针指向的内容
  200. @return void
  201. 作者:jesse 日期:2008-2-3
  202. */
  203. void log_msg(
  204. const char *strFile, ///文件名
  205. const int iLineNum, ///行号
  206. const char *strCatName, ///category名
  207. const int iPriority, ///日志记录级别
  208. const char *strFormat, ///日志内容格式
  209. ... ///日志内容
  210. )
  211. {
  212. const log4c_category_t* a_category = log4c_category_get(strCatName);
  213. if (log4c_category_is_priority_enabled(a_category, iPriority))
  214. {
  215. va_list va;
  216. va_start(va, strFormat);
  217. ///下面从带路径的文件名解析出不带路径的纯文件名
  218. {
  219. ///static SEM_ID semphore = NULL;
  220. const char * strFilePath = strFile;
  221. const char * strFileName = strrchr(strFilePath, '/');
  222. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  223. ///为线程安全这里加锁,[2/15/2008 jesse]
  224. if (NULL == gs_semphore)
  225. {
  226. gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
  227. }
  228. VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
  229. if (strFileName != NULL)
  230. {
  231. strFileName++;
  232. }
  233. else
  234. {
  235. strFileName = strrchr(strFilePath, '\\');
  236. if (strFileName != NULL)
  237. {
  238. strFileName++;
  239. }
  240. else
  241. {
  242. strFileName = strFilePath;
  243. }
  244. }
  245. _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%-8s(%d) %s", strFileName, iLineNum, strFormat);
  246. log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
  247. // 解锁 [2/15/2008 jesse]
  248. VOS_GiveSem(gs_semphore);
  249. }
  250. va_end(va);
  251. }
  252. }
  253. /** 日志记录
  254. 日志记录为一个字符串指针指向的内容
  255. @return void
  256. 作者:jesse 日期:2008-2-3
  257. */
  258. void log_msg_func(
  259. const char *strFile, ///文件名
  260. const char *strFuncName,///函数名
  261. const int iLineNum, ///行号
  262. const char *strCatName, ///category名
  263. const int iPriority, ///日志记录级别
  264. const char *strFormat, ///日志内容格式
  265. ... ///日志内容
  266. )
  267. {
  268. const log4c_category_t* a_category = log4c_category_get(strCatName);
  269. if (log4c_category_is_priority_enabled(a_category, iPriority))
  270. {
  271. va_list va;
  272. va_start(va, strFormat);
  273. ///下面从带路径的文件名解析出不带路径的纯文件名
  274. {
  275. ///static SEM_ID semphore = NULL;
  276. const char * strFilePath = strFile;
  277. const char * strFileName = strrchr(strFilePath, '/');
  278. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  279. ///为线程安全这里加锁,[2/15/2008 jesse]
  280. if (NULL == gs_semphore)
  281. {
  282. gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
  283. }
  284. VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
  285. if (strFileName != NULL)
  286. {
  287. strFileName++;
  288. }
  289. else
  290. {
  291. strFileName = strrchr(strFilePath, '\\');
  292. if (strFileName != NULL)
  293. {
  294. strFileName++;
  295. }
  296. else
  297. {
  298. strFileName = strFilePath;
  299. }
  300. }
  301. _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%-8s(%d) Function:%s %s\n", strFileName, iLineNum, strFuncName, strFormat);
  302. log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
  303. // 解锁 [2/15/2008 jesse]
  304. VOS_GiveSem(gs_semphore);
  305. }
  306. va_end(va);
  307. }
  308. }
  309. /************************************************************************/
  310. /* 函数:[3/30/2019 Wang];
  311. /* 描述:日志记录,不记录文件名和行号,日志记录为一个字符串指针指向的内容;
  312. /* 参数:;
  313. /* [IN] strCatName:category名;
  314. /* [IN] iPriority:日志记录级别;
  315. /* [IN] strFormat:日志内容格式;
  316. /* [IN] ...:日志内容;
  317. /* 返回:void;
  318. /************************************************************************/
  319. void log_msg_no_file_num(const char *strCatName, const int iPriority, const char *strFormat, ...)
  320. {
  321. const log4c_category_t* a_category = log4c_category_get(strCatName);
  322. if (log4c_category_is_priority_enabled(a_category, iPriority))
  323. {
  324. va_list va;
  325. va_start(va, strFormat);
  326. {
  327. ///static SEM_ID semphore = NULL;
  328. char strMsgBuffer[10240] = {0};
  329. ///为线程安全这里加锁,[2/15/2008 jesse]
  330. if (NULL == gs_semphore)
  331. {
  332. gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
  333. }
  334. VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
  335. _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%s\n", strFormat);
  336. log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
  337. // 解锁 [2/15/2008 jesse]
  338. VOS_GiveSem(gs_semphore);
  339. }
  340. va_end(va);
  341. }
  342. }
  343. /************************************************************************/
  344. /* 函数:[3/30/2019 Wang];
  345. /* 描述:日志记录,不记录文件名和行号,没有任何layout转换,直接输出相应的字符文本到日志中;
  346. /* 此条记录没有行号,也没有线程号,也没有回车等
  347. /* 日志记录为一个字符串指针指向的内容
  348. /* 参数:;
  349. /* [IN] strCatName:category名;
  350. /* [IN] iPriority:日志记录级别;
  351. /* [IN] strFormat:日志内容格式;
  352. /* [IN] ...:日志内容;
  353. /* 返回:void;
  354. /************************************************************************/
  355. void log_msg_no_file_num_no_layout(const char *strCatName, const int iPriority, const char *strFormat, ...)
  356. {
  357. const log4c_category_t* a_category = log4c_category_get(strCatName);
  358. if (log4c_category_is_priority_enabled(a_category, iPriority))
  359. {
  360. va_list va;
  361. va_start(va, strFormat);
  362. {
  363. ///static SEM_ID semphore = NULL;
  364. char strMsgBuffer[10240] = {0};
  365. ///为线程安全这里加锁,[2/15/2008 jesse]
  366. if (NULL == gs_semphore)
  367. {
  368. gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
  369. }
  370. VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
  371. _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%s", strFormat);
  372. log4c_category_vlog_no_file_num_no_layout(a_category, iPriority, strMsgBuffer, va);
  373. // 解锁 [2/15/2008 jesse]
  374. VOS_GiveSem(gs_semphore);
  375. }
  376. va_end(va);
  377. }
  378. }
  379. /** 设置appender
  380. @return int:
  381. 作者:jesse 日期:2008-2-3
  382. */
  383. int log_setappender(
  384. const char *strCatName, ///category名
  385. const char *strAppenderName ///appender名
  386. )
  387. {
  388. log4c_category_set_appender(log4c_category_get(strCatName), log4c_appender_get(strAppenderName));
  389. return(0);
  390. }
  391. /** 日志记录
  392. 支持类似printf函数的带格式输出
  393. @return void
  394. 作者:jesse 日期:2008-2-3
  395. */
  396. void log_log(
  397. const char *strCatName, ///category名
  398. const int iPriority, ///日志记录级别
  399. const char* strFormat, ///日志内容格式
  400. ... ///日志内容
  401. )
  402. {
  403. const log4c_category_t* a_category = log4c_category_get(strCatName);
  404. if (log4c_category_is_priority_enabled(a_category, iPriority))
  405. {
  406. va_list va;
  407. va_start(va, strFormat);
  408. log4c_category_vlog(a_category, iPriority, strFormat, va);
  409. va_end(va);
  410. }
  411. }
  412. #if 0
  413. /** 日志记录Wrapper函数
  414. 日志记录为一个字符串指针指向的内容
  415. @return int:return 0 for success
  416. 作者:jesse 日期:2008-2-4
  417. */
  418. void log_msg_wrapper (
  419. const int iPriority, ///日志记录级别
  420. const char *strFormat, ///日志内容格式
  421. ... ///日志内容
  422. )
  423. {
  424. const log4c_category_t* a_category = log4c_category_get(DEFAULT_LOG_CATEGORY_NAME);
  425. ///log_msg(__FILE__, __LINE__, DEFAULT_LOG_CATEGORY_NAME, iPriority, strFormat);
  426. if (log4c_category_is_priority_enabled(a_category, iPriority))
  427. {
  428. char strMsg[10240] = {0};
  429. va_list va;
  430. va_start(va, strFormat);
  431. {
  432. int iSprintSize = 0;
  433. iSprintSize = vsnprintf(strMsg, sizeof(strMsg), strFormat, va);
  434. if ( iSprintSize >= sizeof(strMsg))
  435. {
  436. sd_error("truncating message of %d bytes (bufsize = %d)", iSprintSize, sizeof(strMsg));
  437. }
  438. }
  439. va_end(va);
  440. log4c_category_vlog(a_category, iPriority, strMsg, va);
  441. }
  442. }
  443. #endif
  444. const LOG_PARAM log_vsprintf_wrapper(
  445. const int iPriority, ///日志记录级别
  446. const char* strFormat, ///日志内容格式
  447. ... ///日志内容
  448. )
  449. {
  450. LOG_PARAM log_param;
  451. va_list va;
  452. va_start(va, strFormat);
  453. {
  454. #if 1
  455. _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
  456. #else
  457. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  458. #endif
  459. }
  460. va_end(va);
  461. log_param.iPriority = iPriority;
  462. return log_param;
  463. }
  464. const LOG_PARAM log_condition_vsnprintf_wrapper(
  465. const int iCondition, ///条件
  466. const int iPriority, ///日志记录级别
  467. const char* strFormat, ///日志内容格式
  468. ... ///日志内容
  469. )
  470. {
  471. LOG_PARAM log_param;
  472. va_list va;
  473. va_start(va, strFormat);
  474. {
  475. #if 1
  476. _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
  477. #else
  478. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  479. #endif
  480. }
  481. va_end(va);
  482. log_param.iCondition = iCondition;
  483. log_param.iPriority = iPriority;
  484. return log_param;
  485. }
  486. /************************************************************************/
  487. /* 函数:[3/30/2019 Wang];
  488. /* 描述:;
  489. /* 参数:;
  490. /* [IN] iPriority:日志记录级别;
  491. /* [IN] strFormat:日志内容格式;
  492. /* [IN] strHexBuf:缓冲区首地址;
  493. /* [IN] iHexBufLen:缓冲区长度;
  494. /* [IN] ...:日志内容;
  495. /* 返回:void;
  496. /************************************************************************/
  497. void log_hex_dump_vsnprintf_wrapper(const int iPriority, const char* strFormat, const char* strHexBuf, const int iHexBufLen)
  498. {
  499. char *strBuffer = (char *)malloc(LOG4C_BUFFER_SIZE_MAX);
  500. memset(strBuffer, 0, LOG4C_BUFFER_SIZE_MAX);
  501. log4c_sprintf_data((char*)(strHexBuf), (iHexBufLen), strBuffer);
  502. log_msg_no_file_num(DEFAULT_LOG_CATEGORY_NAME, iPriority, strFormat, strBuffer );
  503. }
  504. /************************************************************************/
  505. /* 函数:[3/30/2019 Wang];
  506. /* 描述:;
  507. /* 参数:;
  508. /* [IN] strFormat:日志内容格式;
  509. /* [IN] ...:日志内容;
  510. /* 返回:void;
  511. /************************************************************************/
  512. const LOG_PARAM log_vsnprintf_wrapper_msg(const char* strFormat, ...)
  513. {
  514. LOG_PARAM log_param;
  515. va_list va;
  516. va_start(va, strFormat);
  517. {
  518. #if 1
  519. _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
  520. #else
  521. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  522. #endif
  523. }
  524. va_end(va);
  525. return log_param;
  526. }
  527. void log_msg_wrapper(
  528. const char *strFileName,///文件名
  529. const int iLineNum, ///行号
  530. const char *strCatName, ///category名
  531. const int iPriority, ///日志记录级别
  532. const char *strFormat, ///日志内容格式
  533. ... ///日志内容
  534. )
  535. {
  536. const log4c_category_t* a_category = log4c_category_get(strCatName);
  537. if (log4c_category_is_priority_enabled(a_category, iPriority))
  538. {
  539. va_list va;
  540. va_start(va, strFormat);
  541. log4c_category_vlog(a_category, iPriority, strFormat, va);
  542. va_end(va);
  543. }
  544. }
  545. /**宏参数抽取format函数
  546. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回format的值
  547. @return const char* : strFormat
  548. 作者:jesse 日期:2008-2-4
  549. */
  550. const char* log_get_format_wrapper(
  551. const int iPriority, ///日志记录级别
  552. const char* strFormat, ///日志内容格式
  553. ... ///日志内容
  554. )
  555. {
  556. return strFormat;
  557. #if 0
  558. char* buffer = strdup(strFormat);
  559. return buffer;
  560. #endif
  561. }
  562. /**宏参数抽取priority函数
  563. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回priority的值
  564. @return const int : iPriority.
  565. 作者:jesse 日期:2008-2-4
  566. */
  567. const int log_get_priority_wrapper(
  568. const int iPriority, ///日志记录级别
  569. const char* strFormat, ///日志内容格式
  570. ... ///日志内容
  571. )
  572. {
  573. return iPriority;
  574. }
  575. /**
  576. 将大小为count的缓冲区中内容按字节以16进制字符串打印出来,
  577. 返回值即为指向相应的字符串,
  578. 该返回指向的存储区域要调用本函数的用户显示的进行删除
  579. */
  580. void log4c_sprintf_data(char *buff, int count, char *dest_buffer)
  581. {
  582. int i,j,c;
  583. int printnext=1;
  584. char str[10] = {0};///由于这里要进行格式输出如"%.4x "等,这里给出10个字符作为格式输出冗余
  585. if(count)
  586. {
  587. if(count%16)
  588. {
  589. c=count+(16-count%16);
  590. }
  591. else
  592. {
  593. c=count;
  594. }
  595. }
  596. else
  597. {
  598. c=count;
  599. }
  600. for(i=0;i<c;i++)
  601. {
  602. if(printnext)
  603. {
  604. printnext--;
  605. _snprintf_s(str, sizeof(str), _TRUNCATE, "%.4x ",i&0xffff);
  606. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  607. ///ostr << str;
  608. }
  609. if(i<count)
  610. {
  611. _snprintf_s(str, sizeof(str), _TRUNCATE, "%3.2x",buff[i]&0xff);
  612. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  613. ///ostr << str;
  614. }
  615. else
  616. {
  617. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  618. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  619. ///ostr << str;
  620. }
  621. if(!((i+1)%8))
  622. {
  623. if((i+1)%16)
  624. {
  625. _snprintf_s(str, sizeof(str), _TRUNCATE, " -");
  626. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  627. ///ostr << str;
  628. }
  629. else
  630. {
  631. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  632. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  633. ///ostr << str;
  634. for(j=i-15;j<=i;j++)
  635. {
  636. if(j<count)
  637. {
  638. if( (buff[j]&0xff) >= 0x20 &&
  639. (buff[j]&0xff)<=0x7e)
  640. {
  641. _snprintf_s(str, sizeof(str), _TRUNCATE, "%c",buff[j]&0xff);
  642. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  643. }
  644. else
  645. {
  646. _snprintf_s(str, sizeof(str), _TRUNCATE, ".");
  647. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  648. }
  649. }
  650. else
  651. {
  652. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  653. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  654. ///ostr << str;
  655. }
  656. }
  657. _snprintf_s(str, sizeof(str), _TRUNCATE, "\n");
  658. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  659. printnext=1;
  660. }
  661. }
  662. }
  663. }
  664. //////////////////////////////////////////////////////////////////////////
  665. //////////////////////////////////////////////////////////////////////////
  666. static char gs_strFileContent_InC[] =
  667. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  668. "<!DOCTYPE log4c SYSTEM \"\">\n"
  669. "\n"
  670. "<log4c version=\"1.2.1\">\n"
  671. " <config>\n"
  672. " <bufsize>0</bufsize>\n"
  673. " <debug level=\"2\"/>\n"
  674. " <nocleanup>0</nocleanup>\n"
  675. " <reread>1</reread>\n"
  676. " </config>\n"
  677. "\n"
  678. " <category name=\"root\" priority=\"notice\" appender=\"aname\"/>\n"
  679. "\n"
  680. " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"1048576\" maxnum=\"15\" />\n"
  681. " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"log\" ext=\"txt\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
  682. "\n"
  683. " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
  684. " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
  685. " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
  686. "</log4c>\n";
  687. static char gs_strDefualtFileName[] = "log4crc";
  688. int log_init_with_string(
  689. const char *strFileContent,
  690. const char *strFileName
  691. )
  692. {
  693. int iResult = FAILURE;
  694. const char * strLocalFileContent = NULL;
  695. const char * strLocalFileName = NULL;
  696. PAssertNotNull_ReturnWithValue(strFileContent, FAILURE);
  697. PAssertNotNull_ReturnWithValue(strFileName, FAILURE);
  698. strLocalFileContent = (strlen(strFileContent) > 0 ? strFileContent:gs_strFileContent_InC);
  699. strLocalFileName = (strlen(strFileName) > 0 ? strFileName:gs_strDefualtFileName);
  700. /**
  701. 若文件已存在则不重新生成配置文件了
  702. 否则生成新的配置文件
  703. */
  704. if (OK != SD_ACCESS_READ(strLocalFileName))
  705. {
  706. ///文件不存在新建
  707. FILE *file = fopen(strLocalFileName, "wb");
  708. if (NULL != file)
  709. {
  710. fwrite(strLocalFileContent, 1, strlen(strLocalFileContent), file);
  711. fclose(file);
  712. }
  713. }
  714. else
  715. {
  716. ///文件存在,do nothing,使用原来的配置文件就OK了。
  717. }
  718. //////////////////////////////////////////////////////////////////////////
  719. ///调用Init函数进行实际的日志模块初始化工作
  720. iResult = log_init_with_cfg_file_wrapper(strLocalFileName);
  721. return iResult;
  722. }
  723. int log_init_with_cfg_file_wrapper(const char * strConfigFile)
  724. {
  725. int iResult = FAILURE;
  726. PAssertNotNull_ReturnWithValue(strConfigFile, FAILURE);
  727. ///调用log4c的函数来初始化日志
  728. if (strlen(strConfigFile) > 0)
  729. {
  730. iResult = log_init_with_cfg_file(strConfigFile);
  731. }
  732. if ( OK != iResult)
  733. {
  734. iResult = log_init();
  735. }
  736. return iResult;
  737. }
  738. /*****************************新增的一些日志模块配置属性设置类****************/
  739. #define MAX_FILE_NAME_LEN 512
  740. struct ST_LogProperty
  741. {
  742. char m_strLogCfgFileName[MAX_FILE_NAME_LEN];
  743. char m_strLogFileName[MAX_FILE_NAME_LEN];
  744. int m_iLogFileSize;
  745. char m_strLogLevel[128];
  746. int m_iLogFileNum;
  747. BOOL m_bReReadLogCfgFile;
  748. };
  749. typedef struct ST_LogProperty LogProperty;
  750. static LogProperty gs_st_LogProperty =
  751. {
  752. "log4crc",
  753. "log",
  754. 1048576,
  755. "notice",
  756. 15,
  757. TRUE
  758. };
  759. static char gs_strFileContentFormat[] =
  760. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  761. "<!DOCTYPE log4c SYSTEM \"\">\n"
  762. "\n"
  763. "<log4c version=\"1.2.1\">\n"
  764. " <config>\n"
  765. " <bufsize>0</bufsize>\n"
  766. " <debug level=\"2\"/>\n"
  767. " <nocleanup>0</nocleanup>\n"
  768. " <reread>%d</reread>\n"
  769. " </config>\n"
  770. "\n"
  771. " <category name=\"root\" priority=\"%s\" appender=\"aname\"/>\n"
  772. "\n"
  773. " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"%d\" maxnum=\"%d\" />\n"
  774. " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"%s\" ext=\"txt\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
  775. "\n"
  776. " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
  777. " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
  778. " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
  779. "</log4c>\n";
  780. static char gs_strFileContentBuffer[4096] = {0};
  781. void log_set_log_cfg_file_name(const char *strFileName)
  782. {
  783. PAssertNotNull_Return(strFileName);
  784. strncpy_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
  785. }
  786. void log_set_log_file_name(const char *strFileName)
  787. {
  788. PAssertNotNull_Return(strFileName);
  789. strncpy_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
  790. }
  791. void log_set_log_level(const char *strLogLevel)
  792. {
  793. strncpy_s(gs_st_LogProperty.m_strLogLevel, sizeof(gs_st_LogProperty.m_strLogLevel), strLogLevel, _TRUNCATE);
  794. }
  795. void log_set_log_file_size(const int iFileSize)
  796. {
  797. gs_st_LogProperty.m_iLogFileSize = iFileSize;
  798. }
  799. void log_set_log_file_num(const int iFileNum)
  800. {
  801. gs_st_LogProperty.m_iLogFileNum = iFileNum;
  802. }
  803. void log_set_reread_log_cfg_file(const BOOL bReReadLogCfgFile)
  804. {
  805. gs_st_LogProperty.m_bReReadLogCfgFile = bReReadLogCfgFile;
  806. }
  807. int log_init_with_param()
  808. {
  809. _snprintf_s(gs_strFileContentBuffer, sizeof(gs_strFileContentBuffer),_TRUNCATE,
  810. gs_strFileContentFormat,
  811. gs_st_LogProperty.m_bReReadLogCfgFile,
  812. gs_st_LogProperty.m_strLogLevel,
  813. gs_st_LogProperty.m_iLogFileSize,
  814. gs_st_LogProperty.m_iLogFileNum,
  815. gs_st_LogProperty.m_strLogFileName
  816. );
  817. return log_init_with_string(gs_strFileContentBuffer, gs_st_LogProperty.m_strLogCfgFileName);
  818. }
  819. /************************************************************************/
  820. /************************************************************************/
  821. // Instruct the compiler to put the g_lModuleUsage data
  822. // variable in its own data section, called Shared. We
  823. // then instruct the linker that we want the data in this
  824. // section to be shared by all instances of this application.
  825. #ifdef WIN32
  826. #pragma data_seg("LogShared")
  827. /** 数据库日志模块运行的实例个数
  828. 避免多个日志模块的运行实例使用同一个配置文件,避免使用同一个log文件来记录日志而产生冲突
  829. */
  830. long g_lLogModuleUsage = -1;
  831. #pragma data_seg()
  832. // Instruct the linker to make the Shared section
  833. // readable, writable, and shared.
  834. #pragma comment(linker, "/section:LogShared,rws")
  835. #else
  836. // long g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  837. // int g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  838. // extern long GetModuleUsage();
  839. // long g_lLogModuleUsage = GetModuleUsage();
  840. long g_lLogModuleUsage = -1;
  841. #endif
  842. int log_init_with_param_multi_process()
  843. {
  844. char strLogCfgFileName[MAX_FILE_NAME_LEN] = {0};
  845. char strLogFileName[MAX_FILE_NAME_LEN] = {0};
  846. char strLogModuleUsageNum[MAX_FILE_NAME_LEN] = {0};
  847. IncreaseLogModuleUsage();
  848. //snprintf(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName),
  849. // "%s%d", gs_st_LogProperty.m_strLogCfgFileName, g_lLogModuleUsage);
  850. //snprintf(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),
  851. // "%s%d", gs_st_LogProperty.m_strLogFileName, g_lLogModuleUsage);
  852. _snprintf_s(strLogModuleUsageNum, sizeof(strLogModuleUsageNum), _TRUNCATE, "%d", g_lLogModuleUsage);
  853. strcat_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strLogModuleUsageNum);
  854. strcat_s(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),strLogModuleUsageNum);
  855. return log_init_with_param();
  856. }
  857. extern long GetModuleUsage();
  858. extern void SetModuleUsage(long lModuleUsage);
  859. void IncreaseLogModuleUsage()
  860. {
  861. #ifdef WIN32
  862. InterlockedIncrement((long*) &g_lLogModuleUsage);
  863. #else
  864. g_lLogModuleUsage = GetModuleUsage();
  865. g_lLogModuleUsage ++;
  866. SetModuleUsage(g_lLogModuleUsage);
  867. #endif
  868. }
  869. /************************************************************************/
  870. /************************************************************************/
  871. ///下面为LOG4C_BLOCK_XXX相关宏定义的实现代码
  872. ///进行LOG4C_BLOCK_BEGIN和LOG4C_BLOCK_END宏定义使用的Block结构
  873. struct ST_Block
  874. {
  875. #define BLOCK_FREE 0
  876. #define BLOCK_BUSY 1
  877. ///标志是否被使用
  878. int iUseStatus;
  879. ///线程ID
  880. TASK_ID threadId;
  881. ///Block的层次
  882. int iBlockLevel;
  883. void (*Init)(struct ST_Block *ptrThis);
  884. void (*Reset)(struct ST_Block *ptrThis);
  885. int (*GetUseStatus)(struct ST_Block *ptrThis);
  886. TASK_ID (*GetThreadId)(struct ST_Block *ptrThis);
  887. int (*GetBlockLevel)(struct ST_Block *ptrThis);
  888. void (*SetUseStatus)(struct ST_Block *ptrThis, int iUseStatus);
  889. void (*SetThreadId)(struct ST_Block *ptrThis, TASK_ID threadId);
  890. void (*SetBlockLevel)(struct ST_Block *ptrThis, int iBlockLevel);
  891. };
  892. typedef struct ST_Block Block;
  893. /**一个ST_BlockGroup包含5个ST_Block
  894. ST_BlockGroup中作为ST_BlockManager中管理的节点
  895. */
  896. struct ST_BlockGroup
  897. {
  898. #define BLOCK_GROUP_BLOCK_NUM (5)
  899. Block aBlock[BLOCK_GROUP_BLOCK_NUM];
  900. struct ST_BlockGroup *ptrBlockGroupPre;
  901. struct ST_BlockGroup *ptrBlockGroupNext;
  902. void (*Init)(struct ST_BlockGroup *ptrThis);
  903. Block *(*GetThreadBlock)(struct ST_BlockGroup *ptrThis, TASK_ID threadId);
  904. Block *(*GetFreeBlock)(struct ST_BlockGroup *ptrThis);
  905. };
  906. typedef struct ST_BlockGroup BlockGroup;
  907. struct ST_BlockManager
  908. {
  909. BlockGroup *ptrBlockGroupHeader;
  910. BlockGroup *ptrBlockGroupTail;
  911. Block *(*GetThreadBlock)(struct ST_BlockManager *ptrThis, TASK_ID threadId);
  912. Block *(*GetFreeBlock)(struct ST_BlockManager *ptrThis);
  913. };
  914. typedef struct ST_BlockManager BlockManager;
  915. extern Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId);
  916. extern Block *BlockManager_GetFreeBlock(BlockManager *ptrThis);
  917. BlockManager g_blockManager =
  918. {
  919. NULL,
  920. NULL,
  921. BlockManager_GetThreadBlock,
  922. BlockManager_GetFreeBlock
  923. };
  924. extern void Block_Reset(Block *ptrThis);
  925. extern int Block_GetUseStatus(Block *ptrThis);
  926. extern TASK_ID Block_GetThreadId(Block *ptrThis);
  927. extern int Block_GetBlockLevel(Block *ptrThis);
  928. extern void Block_SetUseStatus(Block *ptrThis, int iUseStatus);
  929. extern void Block_SetThreadId(Block *ptrThis, TASK_ID threadId);
  930. extern void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel);
  931. /**ST_Block的初始化代码
  932. @param :Block *ptrThis
  933. @rerurn void.
  934. */
  935. void Block_Init(Block *ptrThis)
  936. {
  937. PAssert_Return(ptrThis);
  938. memset(ptrThis, 0, sizeof(*ptrThis));
  939. ///设置本结构的成员函数
  940. ptrThis->Init = Block_Init;
  941. ptrThis->Reset = Block_Reset;
  942. ptrThis->SetBlockLevel = Block_SetBlockLevel;
  943. ptrThis->GetBlockLevel = Block_GetBlockLevel;
  944. ptrThis->SetThreadId = Block_SetThreadId;
  945. ptrThis->GetThreadId = Block_GetThreadId;
  946. ptrThis->SetUseStatus = Block_SetUseStatus;
  947. ptrThis->GetUseStatus = Block_GetUseStatus;
  948. return;
  949. }
  950. /**ST_Block的重置代码
  951. @param :Block *ptrThis
  952. @rerurn void.
  953. */
  954. void Block_Reset(Block *ptrThis)
  955. {
  956. PAssert_Return(ptrThis);
  957. Block_Init(ptrThis);
  958. return;
  959. }
  960. /**ST_Block的获取成员变量iUseStatus的函数
  961. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  962. @param :int
  963. @rerurn void.
  964. */
  965. int Block_GetUseStatus(Block *ptrThis)
  966. {
  967. PAssert_ReturnWithValue(ptrThis, 0);
  968. return ptrThis->iUseStatus;
  969. }
  970. /**ST_Block的获取成员变量threadId的函数
  971. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  972. @param :TASK_ID.
  973. @rerurn void.
  974. */
  975. TASK_ID Block_GetThreadId(Block *ptrThis)
  976. {
  977. PAssert_ReturnWithValue(ptrThis, 0);
  978. return ptrThis->threadId;
  979. }
  980. /**ST_Block的获取成员变量iBlockLevel的函数
  981. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  982. @param :int.
  983. @rerurn void.
  984. */
  985. int Block_GetBlockLevel(Block *ptrThis)
  986. {
  987. PAssert_ReturnWithValue(ptrThis, 0);
  988. return ptrThis->iBlockLevel;
  989. }
  990. /**ST_Block的设置成员变量iUseStatus的函数
  991. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  992. @param :Block *ptrThis
  993. @param :int
  994. @rerurn void.
  995. */
  996. void Block_SetUseStatus(Block *ptrThis, int iUseStatus)
  997. {
  998. PAssert_Return(ptrThis);
  999. ptrThis->iUseStatus = iUseStatus;
  1000. }
  1001. /**ST_Block的设置成员变量threadId的函数
  1002. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  1003. @param :Block *ptrThis
  1004. @param :TASK_ID.
  1005. @rerurn void.
  1006. */
  1007. void Block_SetThreadId(Block *ptrThis, TASK_ID threadId)
  1008. {
  1009. PAssert_Return(ptrThis);
  1010. ptrThis->threadId = threadId;
  1011. }
  1012. /**ST_Block的设置成员变量iBlockLevel的函数
  1013. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  1014. @param :Block *ptrThis
  1015. @param :int.
  1016. @rerurn void.
  1017. */
  1018. void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel)
  1019. {
  1020. PAssert_Return(ptrThis);
  1021. ptrThis->iBlockLevel = iBlockLevel;
  1022. }
  1023. extern Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId);
  1024. extern Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis);
  1025. /**ST_BlockGroup的初始化代码
  1026. @param :Block *ptrThis
  1027. @rerurn void.
  1028. */
  1029. void BlockGroup_Init(BlockGroup *ptrThis)
  1030. {
  1031. int i = 0;
  1032. PAssert_Return(ptrThis);
  1033. memset(ptrThis, 0, sizeof(*ptrThis));
  1034. ptrThis->Init = BlockGroup_Init;
  1035. ptrThis->GetThreadBlock = BlockGroup_GetThreadBlock;
  1036. ptrThis->GetFreeBlock = BlockGroup_GetFreeBlock;
  1037. ///初始化每个Block
  1038. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1039. {
  1040. Block *ptrBlock = &ptrThis->aBlock[i];
  1041. ptrBlock->Init = Block_Init;
  1042. ptrBlock->Init(ptrBlock);
  1043. }
  1044. return;
  1045. }
  1046. /**从ST_BlockGroup中获取给定线程的Block的指针
  1047. @param :Block *ptrThis
  1048. @param :TASK_ID threadId
  1049. @rerurn Block *.
  1050. */
  1051. Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId)
  1052. {
  1053. Block *ptrBlockReturn = NULL;
  1054. int i = 0;
  1055. PAssert_ReturnWithValue(ptrThis, NULL);
  1056. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1057. {
  1058. Block *ptrBlock = &ptrThis->aBlock[i];
  1059. if (ptrBlock->GetThreadId(ptrBlock) == threadId)
  1060. {
  1061. ptrBlockReturn = ptrBlock;
  1062. break;
  1063. }
  1064. }
  1065. return ptrBlockReturn;
  1066. }
  1067. /**从ST_BlockGroup中获取空闲的Block的指针
  1068. @param :BlockGroup *ptrThis
  1069. @rerurn Block *.
  1070. */
  1071. Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis)
  1072. {
  1073. Block *ptrBlockReturn = NULL;
  1074. int i = 0;
  1075. PAssert_ReturnWithValue(ptrThis, NULL);
  1076. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1077. {
  1078. Block *ptrBlock = &ptrThis->aBlock[i];
  1079. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1080. {
  1081. ptrBlockReturn = ptrBlock;
  1082. break;
  1083. }
  1084. }
  1085. return ptrBlockReturn;
  1086. }
  1087. Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId)
  1088. {
  1089. Block *ptrBlockReturn = NULL;
  1090. BlockGroup *ptrBlockGroup = NULL;
  1091. PAssert_ReturnWithValue(ptrThis, NULL);
  1092. if (NULL == ptrThis->ptrBlockGroupHeader)
  1093. {
  1094. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1095. ptrGroupBlock->Init = BlockGroup_Init;
  1096. ptrGroupBlock->Init(ptrGroupBlock);
  1097. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1098. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1099. }
  1100. PAssert_ReturnWithValue(ptrThis->ptrBlockGroupHeader, NULL);
  1101. ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1102. do
  1103. {
  1104. ptrBlockReturn = ptrBlockGroup->GetThreadBlock(ptrBlockGroup, threadId);
  1105. if (NULL != ptrBlockReturn)
  1106. {
  1107. break;
  1108. }
  1109. else
  1110. {
  1111. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1112. }
  1113. } while (NULL != ptrBlockGroup);
  1114. ///此时找遍所有的链表都没有找到相应的Block,则重新找块没用的Block
  1115. if (NULL == ptrBlockReturn)
  1116. {
  1117. ptrBlockReturn = ptrThis->GetFreeBlock(ptrThis);
  1118. }
  1119. return ptrBlockReturn;
  1120. }
  1121. Block *BlockManager_GetFreeBlock(BlockManager *ptrThis)
  1122. {
  1123. Block *ptrBlockReturn = NULL;
  1124. PAssert_ReturnWithValue(ptrThis, NULL);
  1125. if(NULL == ptrThis->ptrBlockGroupHeader)
  1126. {
  1127. ///原来没有空间则动态分配
  1128. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1129. ptrGroupBlock->Init = BlockGroup_Init;
  1130. ptrGroupBlock->Init(ptrGroupBlock);
  1131. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1132. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1133. ptrBlockReturn = ptrGroupBlock->GetFreeBlock(ptrGroupBlock);
  1134. }
  1135. else
  1136. {
  1137. BlockGroup *ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1138. do
  1139. {
  1140. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1141. if (NULL != ptrBlockReturn)
  1142. {
  1143. break;
  1144. }
  1145. else
  1146. {
  1147. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1148. }
  1149. } while (NULL != ptrBlockGroup);
  1150. ///此时找遍所有的链表都没有找到空闲空间,则进行动态分配
  1151. if(NULL == ptrBlockReturn)
  1152. {
  1153. BlockGroup *ptrBlockGroup = (BlockGroup *)malloc(sizeof(BlockGroup));
  1154. ptrBlockGroup->Init = BlockGroup_Init;
  1155. ptrBlockGroup->Init(ptrBlockGroup);
  1156. ptrThis->ptrBlockGroupTail->ptrBlockGroupNext = ptrBlockGroup;
  1157. ptrBlockGroup->ptrBlockGroupPre = ptrThis->ptrBlockGroupTail;
  1158. ptrThis->ptrBlockGroupTail = ptrBlockGroup;
  1159. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1160. }
  1161. }
  1162. return ptrBlockReturn;
  1163. }
  1164. void log4c_block_begin(const char * fileName, int lineNum, const char * traceName)
  1165. {
  1166. TASK_ID threadId = VOS_GetSelfTaskID();
  1167. int i = 0;
  1168. Block *ptrBlock = NULL;
  1169. PAssert_Return(fileName);
  1170. PAssert_Return(traceName);
  1171. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1172. if (NULL != ptrBlock)
  1173. {
  1174. int iBlockLevel = 0;
  1175. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1176. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1177. {
  1178. ptrBlock->SetUseStatus(ptrBlock, BLOCK_BUSY);
  1179. ptrBlock->SetThreadId(ptrBlock, threadId);
  1180. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1181. }
  1182. if (ptrBlock->GetThreadId(ptrBlock) != threadId)
  1183. {
  1184. ptrBlock->SetThreadId(ptrBlock, threadId);
  1185. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1186. }
  1187. ///ptrBlock->SetThreadId(ptrBlock, threadId);
  1188. ///ptrBlock->SetBlockLevel(ptrBlock, 0);
  1189. iBlockLevel = ++(ptrBlock->iBlockLevel) ;
  1190. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Entry\t", sizeof(strMsgBuffer));
  1191. for (i = 0; i < iBlockLevel; i++)
  1192. {
  1193. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX,"==", sizeof(strMsgBuffer));
  1194. }
  1195. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "> ", sizeof(strMsgBuffer));
  1196. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
  1197. ///调用log4c的函数来记录日志
  1198. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1199. }
  1200. }
  1201. void log4c_block_end(const char * fileName, int lineNum, const char * traceName)
  1202. {
  1203. TASK_ID threadId = VOS_GetSelfTaskID();
  1204. int i = 0;
  1205. Block *ptrBlock = NULL;
  1206. PAssert_Return(fileName);
  1207. PAssert_Return(traceName);
  1208. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1209. if (NULL != ptrBlock)
  1210. {
  1211. int iBlockLevel = ptrBlock->GetBlockLevel(ptrBlock);
  1212. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1213. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Exit\t", sizeof(strMsgBuffer));
  1214. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "<", sizeof(strMsgBuffer));
  1215. for (i = 0; i < iBlockLevel; i++)
  1216. {
  1217. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "==", sizeof(strMsgBuffer));
  1218. }
  1219. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, " ", sizeof(strMsgBuffer));
  1220. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
  1221. ///调用log4c的函数来记录日志
  1222. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1223. iBlockLevel = --ptrBlock->iBlockLevel ;
  1224. if (ptrBlock->iBlockLevel == 0)
  1225. {
  1226. ptrBlock->SetThreadId(ptrBlock, 0);
  1227. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1228. ptrBlock->SetUseStatus(ptrBlock, BLOCK_FREE);
  1229. }
  1230. }
  1231. }
  1232. /************************************************************************/
  1233. /************** End of log.c *******************************************/