log.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1, "%s/log4crc",
  99. getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
  100. snprintf(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, "%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(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1, "%s/log4crc",
  153. getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
  154. snprintf(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, "%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(strMsgBuffer, sizeof(strMsgBuffer)-1, "%-8s(%d) %s\n", 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(strMsgBuffer, sizeof(strMsgBuffer)-1, "%-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. 日志记录为一个字符串指针指向的内容
  311. @return void
  312. 作者:jesse 日期:2008-2-3
  313. */
  314. void log_msg_no_file_num(
  315. const char *strCatName, ///category名
  316. const int iPriority, ///日志记录级别
  317. const char *strFormat, ///日志内容格式
  318. ... ///日志内容
  319. )
  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(strMsgBuffer, sizeof(strMsgBuffer)-1, "%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. /** 日志记录,不记录文件名和行号,没有任何layout转换,直接输出相应的字符文本到日志中
  344. 此条记录没有行号,也没有线程号,也没有回车等
  345. 日志记录为一个字符串指针指向的内容
  346. @return void
  347. 作者:jesse 日期:2008-9-14
  348. */
  349. void log_msg_no_file_num_no_layout(
  350. const char *strCatName, ///category名
  351. const int iPriority, ///日志记录级别
  352. const char *strFormat, ///日志内容格式
  353. ... ///日志内容
  354. )
  355. {
  356. const log4c_category_t* a_category = log4c_category_get(strCatName);
  357. if (log4c_category_is_priority_enabled(a_category, iPriority))
  358. {
  359. va_list va;
  360. va_start(va, strFormat);
  361. {
  362. ///static SEM_ID semphore = NULL;
  363. char strMsgBuffer[10240] = {0};
  364. ///为线程安全这里加锁,[2/15/2008 jesse]
  365. if (NULL == gs_semphore)
  366. {
  367. gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
  368. }
  369. VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
  370. snprintf(strMsgBuffer, sizeof(strMsgBuffer)-1, "%s", strFormat);
  371. log4c_category_vlog_no_file_num_no_layout(a_category, iPriority, strMsgBuffer, va);
  372. // 解锁 [2/15/2008 jesse]
  373. VOS_GiveSem(gs_semphore);
  374. }
  375. va_end(va);
  376. }
  377. }
  378. /** 设置appender
  379. @return int:
  380. 作者:jesse 日期:2008-2-3
  381. */
  382. int log_setappender(
  383. const char *strCatName, ///category名
  384. const char *strAppenderName ///appender名
  385. )
  386. {
  387. log4c_category_set_appender(log4c_category_get(strCatName), log4c_appender_get(strAppenderName));
  388. return(0);
  389. }
  390. /** 日志记录
  391. 支持类似printf函数的带格式输出
  392. @return void
  393. 作者:jesse 日期:2008-2-3
  394. */
  395. void log_log(
  396. const char *strCatName, ///category名
  397. const int iPriority, ///日志记录级别
  398. const char* strFormat, ///日志内容格式
  399. ... ///日志内容
  400. )
  401. {
  402. const log4c_category_t* a_category = log4c_category_get(strCatName);
  403. if (log4c_category_is_priority_enabled(a_category, iPriority))
  404. {
  405. va_list va;
  406. va_start(va, strFormat);
  407. log4c_category_vlog(a_category, iPriority, strFormat, va);
  408. va_end(va);
  409. }
  410. }
  411. #if 0
  412. /** 日志记录Wrapper函数
  413. 日志记录为一个字符串指针指向的内容
  414. @return int:return 0 for success
  415. 作者:jesse 日期:2008-2-4
  416. */
  417. void log_msg_wrapper (
  418. const int iPriority, ///日志记录级别
  419. const char *strFormat, ///日志内容格式
  420. ... ///日志内容
  421. )
  422. {
  423. const log4c_category_t* a_category = log4c_category_get(DEFAULT_LOG_CATEGORY_NAME);
  424. ///log_msg(__FILE__, __LINE__, DEFAULT_LOG_CATEGORY_NAME, iPriority, strFormat);
  425. if (log4c_category_is_priority_enabled(a_category, iPriority))
  426. {
  427. char strMsg[10240] = {0};
  428. va_list va;
  429. va_start(va, strFormat);
  430. {
  431. int iSprintSize = 0;
  432. iSprintSize = vsnprintf(strMsg, sizeof(strMsg), strFormat, va);
  433. if ( iSprintSize >= sizeof(strMsg))
  434. {
  435. sd_error("truncating message of %d bytes (bufsize = %d)", iSprintSize, sizeof(strMsg));
  436. }
  437. }
  438. va_end(va);
  439. log4c_category_vlog(a_category, iPriority, strMsg, va);
  440. }
  441. }
  442. #endif
  443. const LOG_PARAM log_vsprintf_wrapper(
  444. const int iPriority, ///日志记录级别
  445. const char* strFormat, ///日志内容格式
  446. ... ///日志内容
  447. )
  448. {
  449. LOG_PARAM log_param;
  450. va_list va;
  451. va_start(va, strFormat);
  452. {
  453. #if 1
  454. vsnprintf(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, strFormat, va);
  455. #else
  456. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  457. #endif
  458. }
  459. va_end(va);
  460. log_param.iPriority = iPriority;
  461. return log_param;
  462. }
  463. const LOG_PARAM log_condition_vsnprintf_wrapper(
  464. const int iCondition, ///条件
  465. const int iPriority, ///日志记录级别
  466. const char* strFormat, ///日志内容格式
  467. ... ///日志内容
  468. )
  469. {
  470. LOG_PARAM log_param;
  471. va_list va;
  472. va_start(va, strFormat);
  473. {
  474. #if 1
  475. vsnprintf(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, strFormat, va);
  476. #else
  477. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  478. #endif
  479. }
  480. va_end(va);
  481. log_param.iCondition = iCondition;
  482. log_param.iPriority = iPriority;
  483. return log_param;
  484. }
  485. void log_hex_dump_vsnprintf_wrapper(
  486. const int iPriority, ///日志记录级别
  487. const char* strHexBuf, ///缓冲区首地址
  488. const int iHexBufLen, ///缓冲区长度
  489. ... ///日志内容
  490. )
  491. {
  492. char *strBuffer = (char *)malloc(LOG4C_BUFFER_SIZE_MAX);
  493. memset(strBuffer, 0, LOG4C_BUFFER_SIZE_MAX);
  494. log4c_sprintf_data((char*)(strHexBuf), (iHexBufLen), strBuffer);
  495. log_msg_no_file_num(
  496. DEFAULT_LOG_CATEGORY_NAME,
  497. iPriority,
  498. strBuffer
  499. );
  500. }
  501. const LOG_PARAM log_vsnprintf_wrapper_msg(
  502. const char* strFormat, ///日志内容格式
  503. ... ///日志内容
  504. )
  505. {
  506. LOG_PARAM log_param;
  507. va_list va;
  508. va_start(va, strFormat);
  509. {
  510. #if 1
  511. vsnprintf(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, strFormat, va);
  512. #else
  513. memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
  514. #endif
  515. }
  516. va_end(va);
  517. return log_param;
  518. }
  519. void log_msg_wrapper(
  520. const char *strFileName,///文件名
  521. const int iLineNum, ///行号
  522. const char *strCatName, ///category名
  523. const int iPriority, ///日志记录级别
  524. const char *strFormat, ///日志内容格式
  525. ... ///日志内容
  526. )
  527. {
  528. const log4c_category_t* a_category = log4c_category_get(strCatName);
  529. if (log4c_category_is_priority_enabled(a_category, iPriority))
  530. {
  531. va_list va;
  532. va_start(va, strFormat);
  533. log4c_category_vlog(a_category, iPriority, strFormat, va);
  534. va_end(va);
  535. }
  536. }
  537. /**宏参数抽取format函数
  538. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回format的值
  539. @return const char* : strFormat
  540. 作者:jesse 日期:2008-2-4
  541. */
  542. const char* log_get_format_wrapper(
  543. const int iPriority, ///日志记录级别
  544. const char* strFormat, ///日志内容格式
  545. ... ///日志内容
  546. )
  547. {
  548. return strFormat;
  549. #if 0
  550. char* buffer = strdup(strFormat);
  551. return buffer;
  552. #endif
  553. }
  554. /**宏参数抽取priority函数
  555. 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回priority的值
  556. @return const int : iPriority.
  557. 作者:jesse 日期:2008-2-4
  558. */
  559. const int log_get_priority_wrapper(
  560. const int iPriority, ///日志记录级别
  561. const char* strFormat, ///日志内容格式
  562. ... ///日志内容
  563. )
  564. {
  565. return iPriority;
  566. }
  567. /**
  568. 将大小为count的缓冲区中内容按字节以16进制字符串打印出来,
  569. 返回值即为指向相应的字符串,
  570. 该返回指向的存储区域要调用本函数的用户显示的进行删除
  571. */
  572. void log4c_sprintf_data(char *buff, int count, char *dest_buffer)
  573. {
  574. int i,j,c;
  575. int printnext=1;
  576. char str[10] = {0};///由于这里要进行格式输出如"%.4x "等,这里给出10个字符作为格式输出冗余
  577. if(count)
  578. {
  579. if(count%16)
  580. {
  581. c=count+(16-count%16);
  582. }
  583. else
  584. {
  585. c=count;
  586. }
  587. }
  588. else
  589. {
  590. c=count;
  591. }
  592. for(i=0;i<c;i++)
  593. {
  594. if(printnext)
  595. {
  596. printnext--;
  597. snprintf(str, sizeof(str), "%.4x ",i&0xffff);
  598. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  599. ///ostr << str;
  600. }
  601. if(i<count)
  602. {
  603. snprintf(str, sizeof(str),"%3.2x",buff[i]&0xff);
  604. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  605. ///ostr << str;
  606. }
  607. else
  608. {
  609. snprintf(str, sizeof(str), " ");
  610. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  611. ///ostr << str;
  612. }
  613. if(!((i+1)%8))
  614. {
  615. if((i+1)%16)
  616. {
  617. snprintf(str, sizeof(str), " -");
  618. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  619. ///ostr << str;
  620. }
  621. else
  622. {
  623. snprintf(str, sizeof(str), " ");
  624. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  625. ///ostr << str;
  626. for(j=i-15;j<=i;j++)
  627. {
  628. if(j<count)
  629. {
  630. if( (buff[j]&0xff) >= 0x20 &&
  631. (buff[j]&0xff)<=0x7e)
  632. {
  633. snprintf(str, sizeof(str), "%c",buff[j]&0xff);
  634. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  635. }
  636. else
  637. {
  638. snprintf(str, sizeof(str), ".");
  639. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  640. }
  641. }
  642. else
  643. {
  644. snprintf(str, sizeof(str), " ");
  645. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  646. ///ostr << str;
  647. }
  648. }
  649. snprintf(str, sizeof(str), "\n");
  650. strncat(dest_buffer, str, LOG4C_BUFFER_SIZE_MAX);
  651. printnext=1;
  652. }
  653. }
  654. }
  655. }
  656. //////////////////////////////////////////////////////////////////////////
  657. //////////////////////////////////////////////////////////////////////////
  658. static char gs_strFileContent_InC[] =
  659. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  660. "<!DOCTYPE log4c SYSTEM \"\">\n"
  661. "\n"
  662. "<log4c version=\"1.2.1\">\n"
  663. " <config>\n"
  664. " <bufsize>0</bufsize>\n"
  665. " <debug level=\"2\"/>\n"
  666. " <nocleanup>0</nocleanup>\n"
  667. " <reread>1</reread>\n"
  668. " </config>\n"
  669. "\n"
  670. " <category name=\"root\" priority=\"notice\" appender=\"aname\"/>\n"
  671. "\n"
  672. " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"1048576\" maxnum=\"15\" />\n"
  673. " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"log\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
  674. "\n"
  675. " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
  676. " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
  677. " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
  678. "</log4c>\n";
  679. static char gs_strDefualtFileName[] = "log4crc";
  680. int log_init_with_string(
  681. const char *strFileContent,
  682. const char *strFileName
  683. )
  684. {
  685. int iResult = FAILURE;
  686. const char * strLocalFileContent = NULL;
  687. const char * strLocalFileName = NULL;
  688. PAssertNotNull_ReturnWithValue(strFileContent, FAILURE);
  689. PAssertNotNull_ReturnWithValue(strFileName, FAILURE);
  690. strLocalFileContent = (strlen(strFileContent) > 0 ? strFileContent:gs_strFileContent_InC);
  691. strLocalFileName = (strlen(strFileName) > 0 ? strFileName:gs_strDefualtFileName);
  692. /**
  693. 若文件已存在则不重新生成配置文件了
  694. 否则生成新的配置文件
  695. */
  696. if (OK != SD_ACCESS_READ(strLocalFileName))
  697. {
  698. ///文件不存在新建
  699. FILE *file = fopen(strLocalFileName, "wb");
  700. if (NULL != file)
  701. {
  702. fwrite(strLocalFileContent, 1, strlen(strLocalFileContent), file);
  703. fclose(file);
  704. }
  705. }
  706. else
  707. {
  708. ///文件存在,do nothing,使用原来的配置文件就OK了。
  709. }
  710. //////////////////////////////////////////////////////////////////////////
  711. ///调用Init函数进行实际的日志模块初始化工作
  712. iResult = log_init_with_cfg_file_wrapper(strLocalFileName);
  713. return iResult;
  714. }
  715. int log_init_with_cfg_file_wrapper(const char * strConfigFile)
  716. {
  717. int iResult = FAILURE;
  718. PAssertNotNull_ReturnWithValue(strConfigFile, FAILURE);
  719. ///调用log4c的函数来初始化日志
  720. if (strlen(strConfigFile) > 0)
  721. {
  722. iResult = log_init_with_cfg_file(strConfigFile);
  723. }
  724. if ( OK != iResult)
  725. {
  726. iResult = log_init();
  727. }
  728. return iResult;
  729. }
  730. /*****************************新增的一些日志模块配置属性设置类****************/
  731. #define MAX_FILE_NAME_LEN 512
  732. struct ST_LogProperty
  733. {
  734. char m_strLogCfgFileName[MAX_FILE_NAME_LEN];
  735. char m_strLogFileName[MAX_FILE_NAME_LEN];
  736. int m_iLogFileSize;
  737. char m_strLogLevel[128];
  738. int m_iLogFileNum;
  739. BOOL m_bReReadLogCfgFile;
  740. };
  741. typedef struct ST_LogProperty LogProperty;
  742. static LogProperty gs_st_LogProperty =
  743. {
  744. "log4crc",
  745. "log",
  746. 1048576,
  747. "notice",
  748. 15,
  749. TRUE
  750. };
  751. static char gs_strFileContentFormat[] =
  752. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  753. "<!DOCTYPE log4c SYSTEM \"\">\n"
  754. "\n"
  755. "<log4c version=\"1.2.1\">\n"
  756. " <config>\n"
  757. " <bufsize>0</bufsize>\n"
  758. " <debug level=\"2\"/>\n"
  759. " <nocleanup>0</nocleanup>\n"
  760. " <reread>%d</reread>\n"
  761. " </config>\n"
  762. "\n"
  763. " <category name=\"root\" priority=\"%s\" appender=\"aname\"/>\n"
  764. "\n"
  765. " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"%d\" maxnum=\"%d\" />\n"
  766. " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"%s\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
  767. "\n"
  768. " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
  769. " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
  770. " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
  771. "</log4c>\n";
  772. static char gs_strFileContentBuffer[4096] = {0};
  773. void log_set_log_cfg_file_name(const char *strFileName)
  774. {
  775. PAssertNotNull_Return(strFileName);
  776. strncpy(gs_st_LogProperty.m_strLogCfgFileName, strFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName));
  777. }
  778. void log_set_log_file_name(const char *strFileName)
  779. {
  780. PAssertNotNull_Return(strFileName);
  781. strncpy(gs_st_LogProperty.m_strLogFileName, strFileName, sizeof(gs_st_LogProperty.m_strLogFileName));
  782. }
  783. void log_set_log_level(const char *strLogLevel)
  784. {
  785. strncpy(gs_st_LogProperty.m_strLogLevel, strLogLevel,
  786. sizeof(gs_st_LogProperty.m_strLogLevel));
  787. }
  788. void log_set_log_file_size(const int iFileSize)
  789. {
  790. gs_st_LogProperty.m_iLogFileSize = iFileSize;
  791. }
  792. void log_set_log_file_num(const int iFileNum)
  793. {
  794. gs_st_LogProperty.m_iLogFileNum = iFileNum;
  795. }
  796. void log_set_reread_log_cfg_file(const BOOL bReReadLogCfgFile)
  797. {
  798. gs_st_LogProperty.m_bReReadLogCfgFile = bReReadLogCfgFile;
  799. }
  800. int log_init_with_param()
  801. {
  802. snprintf(gs_strFileContentBuffer, sizeof(gs_strFileContentBuffer),
  803. gs_strFileContentFormat,
  804. gs_st_LogProperty.m_bReReadLogCfgFile,
  805. gs_st_LogProperty.m_strLogLevel,
  806. gs_st_LogProperty.m_iLogFileSize,
  807. gs_st_LogProperty.m_iLogFileNum,
  808. gs_st_LogProperty.m_strLogFileName
  809. );
  810. return log_init_with_string(gs_strFileContentBuffer, gs_st_LogProperty.m_strLogCfgFileName);
  811. }
  812. /************************************************************************/
  813. /************************************************************************/
  814. // Instruct the compiler to put the g_lModuleUsage data
  815. // variable in its own data section, called Shared. We
  816. // then instruct the linker that we want the data in this
  817. // section to be shared by all instances of this application.
  818. #ifdef WIN32
  819. #pragma data_seg("LogShared")
  820. /** 数据库日志模块运行的实例个数
  821. 避免多个日志模块的运行实例使用同一个配置文件,避免使用同一个log文件来记录日志而产生冲突
  822. */
  823. long g_lLogModuleUsage = -1;
  824. #pragma data_seg()
  825. // Instruct the linker to make the Shared section
  826. // readable, writable, and shared.
  827. #pragma comment(linker, "/section:LogShared,rws")
  828. #else
  829. // long g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  830. // int g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  831. // extern long GetModuleUsage();
  832. // long g_lLogModuleUsage = GetModuleUsage();
  833. long g_lLogModuleUsage = -1;
  834. #endif
  835. int log_init_with_param_multi_process()
  836. {
  837. char strLogCfgFileName[MAX_FILE_NAME_LEN] = {0};
  838. char strLogFileName[MAX_FILE_NAME_LEN] = {0};
  839. char strLogModuleUsageNum[MAX_FILE_NAME_LEN] = {0};
  840. IncreaseLogModuleUsage();
  841. //snprintf(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName),
  842. // "%s%d", gs_st_LogProperty.m_strLogCfgFileName, g_lLogModuleUsage);
  843. //snprintf(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),
  844. // "%s%d", gs_st_LogProperty.m_strLogFileName, g_lLogModuleUsage);
  845. snprintf(strLogModuleUsageNum, sizeof(strLogModuleUsageNum), "%d", g_lLogModuleUsage);
  846. strcat(gs_st_LogProperty.m_strLogCfgFileName, strLogModuleUsageNum);
  847. strcat(gs_st_LogProperty.m_strLogFileName, strLogModuleUsageNum);
  848. return log_init_with_param();
  849. }
  850. extern long GetModuleUsage();
  851. extern void SetModuleUsage(long lModuleUsage);
  852. void IncreaseLogModuleUsage()
  853. {
  854. #ifdef WIN32
  855. InterlockedIncrement((long*) &g_lLogModuleUsage);
  856. #else
  857. g_lLogModuleUsage = GetModuleUsage();
  858. g_lLogModuleUsage ++;
  859. SetModuleUsage(g_lLogModuleUsage);
  860. #endif
  861. }
  862. /************************************************************************/
  863. /************************************************************************/
  864. ///下面为LOG4C_BLOCK_XXX相关宏定义的实现代码
  865. ///进行LOG4C_BLOCK_BEGIN和LOG4C_BLOCK_END宏定义使用的Block结构
  866. struct ST_Block
  867. {
  868. #define BLOCK_FREE 0
  869. #define BLOCK_BUSY 1
  870. ///标志是否被使用
  871. int iUseStatus;
  872. ///线程ID
  873. TASK_ID threadId;
  874. ///Block的层次
  875. int iBlockLevel;
  876. void (*Init)(struct ST_Block *ptrThis);
  877. void (*Reset)(struct ST_Block *ptrThis);
  878. int (*GetUseStatus)(struct ST_Block *ptrThis);
  879. TASK_ID (*GetThreadId)(struct ST_Block *ptrThis);
  880. int (*GetBlockLevel)(struct ST_Block *ptrThis);
  881. void (*SetUseStatus)(struct ST_Block *ptrThis, int iUseStatus);
  882. void (*SetThreadId)(struct ST_Block *ptrThis, TASK_ID threadId);
  883. void (*SetBlockLevel)(struct ST_Block *ptrThis, int iBlockLevel);
  884. };
  885. typedef struct ST_Block Block;
  886. /**一个ST_BlockGroup包含5个ST_Block
  887. ST_BlockGroup中作为ST_BlockManager中管理的节点
  888. */
  889. struct ST_BlockGroup
  890. {
  891. #define BLOCK_GROUP_BLOCK_NUM (5)
  892. Block aBlock[BLOCK_GROUP_BLOCK_NUM];
  893. struct ST_BlockGroup *ptrBlockGroupPre;
  894. struct ST_BlockGroup *ptrBlockGroupNext;
  895. void (*Init)(struct ST_BlockGroup *ptrThis);
  896. Block *(*GetThreadBlock)(struct ST_BlockGroup *ptrThis, TASK_ID threadId);
  897. Block *(*GetFreeBlock)(struct ST_BlockGroup *ptrThis);
  898. };
  899. typedef struct ST_BlockGroup BlockGroup;
  900. struct ST_BlockManager
  901. {
  902. BlockGroup *ptrBlockGroupHeader;
  903. BlockGroup *ptrBlockGroupTail;
  904. Block *(*GetThreadBlock)(struct ST_BlockManager *ptrThis, TASK_ID threadId);
  905. Block *(*GetFreeBlock)(struct ST_BlockManager *ptrThis);
  906. };
  907. typedef struct ST_BlockManager BlockManager;
  908. extern Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId);
  909. extern Block *BlockManager_GetFreeBlock(BlockManager *ptrThis);
  910. BlockManager g_blockManager =
  911. {
  912. NULL,
  913. NULL,
  914. BlockManager_GetThreadBlock,
  915. BlockManager_GetFreeBlock
  916. };
  917. extern void Block_Reset(Block *ptrThis);
  918. extern int Block_GetUseStatus(Block *ptrThis);
  919. extern TASK_ID Block_GetThreadId(Block *ptrThis);
  920. extern int Block_GetBlockLevel(Block *ptrThis);
  921. extern void Block_SetUseStatus(Block *ptrThis, int iUseStatus);
  922. extern void Block_SetThreadId(Block *ptrThis, TASK_ID threadId);
  923. extern void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel);
  924. /**ST_Block的初始化代码
  925. @param :Block *ptrThis
  926. @rerurn void.
  927. */
  928. void Block_Init(Block *ptrThis)
  929. {
  930. PAssert_Return(ptrThis);
  931. memset(ptrThis, 0, sizeof(*ptrThis));
  932. ///设置本结构的成员函数
  933. ptrThis->Init = Block_Init;
  934. ptrThis->Reset = Block_Reset;
  935. ptrThis->SetBlockLevel = Block_SetBlockLevel;
  936. ptrThis->GetBlockLevel = Block_GetBlockLevel;
  937. ptrThis->SetThreadId = Block_SetThreadId;
  938. ptrThis->GetThreadId = Block_GetThreadId;
  939. ptrThis->SetUseStatus = Block_SetUseStatus;
  940. ptrThis->GetUseStatus = Block_GetUseStatus;
  941. return;
  942. }
  943. /**ST_Block的重置代码
  944. @param :Block *ptrThis
  945. @rerurn void.
  946. */
  947. void Block_Reset(Block *ptrThis)
  948. {
  949. PAssert_Return(ptrThis);
  950. Block_Init(ptrThis);
  951. return;
  952. }
  953. /**ST_Block的获取成员变量iUseStatus的函数
  954. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  955. @param :int
  956. @rerurn void.
  957. */
  958. int Block_GetUseStatus(Block *ptrThis)
  959. {
  960. PAssert_ReturnWithValue(ptrThis, 0);
  961. return ptrThis->iUseStatus;
  962. }
  963. /**ST_Block的获取成员变量threadId的函数
  964. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  965. @param :TASK_ID.
  966. @rerurn void.
  967. */
  968. TASK_ID Block_GetThreadId(Block *ptrThis)
  969. {
  970. PAssert_ReturnWithValue(ptrThis, 0);
  971. return ptrThis->threadId;
  972. }
  973. /**ST_Block的获取成员变量iBlockLevel的函数
  974. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  975. @param :int.
  976. @rerurn void.
  977. */
  978. int Block_GetBlockLevel(Block *ptrThis)
  979. {
  980. PAssert_ReturnWithValue(ptrThis, 0);
  981. return ptrThis->iBlockLevel;
  982. }
  983. /**ST_Block的设置成员变量iUseStatus的函数
  984. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  985. @param :Block *ptrThis
  986. @param :int
  987. @rerurn void.
  988. */
  989. void Block_SetUseStatus(Block *ptrThis, int iUseStatus)
  990. {
  991. PAssert_Return(ptrThis);
  992. ptrThis->iUseStatus = iUseStatus;
  993. }
  994. /**ST_Block的设置成员变量threadId的函数
  995. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  996. @param :Block *ptrThis
  997. @param :TASK_ID.
  998. @rerurn void.
  999. */
  1000. void Block_SetThreadId(Block *ptrThis, TASK_ID threadId)
  1001. {
  1002. PAssert_Return(ptrThis);
  1003. ptrThis->threadId = threadId;
  1004. }
  1005. /**ST_Block的设置成员变量iBlockLevel的函数
  1006. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  1007. @param :Block *ptrThis
  1008. @param :int.
  1009. @rerurn void.
  1010. */
  1011. void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel)
  1012. {
  1013. PAssert_Return(ptrThis);
  1014. ptrThis->iBlockLevel = iBlockLevel;
  1015. }
  1016. extern Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId);
  1017. extern Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis);
  1018. /**ST_BlockGroup的初始化代码
  1019. @param :Block *ptrThis
  1020. @rerurn void.
  1021. */
  1022. void BlockGroup_Init(BlockGroup *ptrThis)
  1023. {
  1024. int i = 0;
  1025. PAssert_Return(ptrThis);
  1026. memset(ptrThis, 0, sizeof(*ptrThis));
  1027. ptrThis->Init = BlockGroup_Init;
  1028. ptrThis->GetThreadBlock = BlockGroup_GetThreadBlock;
  1029. ptrThis->GetFreeBlock = BlockGroup_GetFreeBlock;
  1030. ///初始化每个Block
  1031. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1032. {
  1033. Block *ptrBlock = &ptrThis->aBlock[i];
  1034. ptrBlock->Init = Block_Init;
  1035. ptrBlock->Init(ptrBlock);
  1036. }
  1037. return;
  1038. }
  1039. /**从ST_BlockGroup中获取给定线程的Block的指针
  1040. @param :Block *ptrThis
  1041. @param :TASK_ID threadId
  1042. @rerurn Block *.
  1043. */
  1044. Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId)
  1045. {
  1046. Block *ptrBlockReturn = NULL;
  1047. int i = 0;
  1048. PAssert_ReturnWithValue(ptrThis, NULL);
  1049. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1050. {
  1051. Block *ptrBlock = &ptrThis->aBlock[i];
  1052. if (ptrBlock->GetThreadId(ptrBlock) == threadId)
  1053. {
  1054. ptrBlockReturn = ptrBlock;
  1055. break;
  1056. }
  1057. }
  1058. return ptrBlockReturn;
  1059. }
  1060. /**从ST_BlockGroup中获取空闲的Block的指针
  1061. @param :BlockGroup *ptrThis
  1062. @rerurn Block *.
  1063. */
  1064. Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis)
  1065. {
  1066. Block *ptrBlockReturn = NULL;
  1067. int i = 0;
  1068. PAssert_ReturnWithValue(ptrThis, NULL);
  1069. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1070. {
  1071. Block *ptrBlock = &ptrThis->aBlock[i];
  1072. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1073. {
  1074. ptrBlockReturn = ptrBlock;
  1075. break;
  1076. }
  1077. }
  1078. return ptrBlockReturn;
  1079. }
  1080. Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId)
  1081. {
  1082. Block *ptrBlockReturn = NULL;
  1083. BlockGroup *ptrBlockGroup = NULL;
  1084. PAssert_ReturnWithValue(ptrThis, NULL);
  1085. if (NULL == ptrThis->ptrBlockGroupHeader)
  1086. {
  1087. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1088. ptrGroupBlock->Init = BlockGroup_Init;
  1089. ptrGroupBlock->Init(ptrGroupBlock);
  1090. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1091. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1092. }
  1093. PAssert_ReturnWithValue(ptrThis->ptrBlockGroupHeader, NULL);
  1094. ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1095. do
  1096. {
  1097. ptrBlockReturn = ptrBlockGroup->GetThreadBlock(ptrBlockGroup, threadId);
  1098. if (NULL != ptrBlockReturn)
  1099. {
  1100. break;
  1101. }
  1102. else
  1103. {
  1104. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1105. }
  1106. } while (NULL != ptrBlockGroup);
  1107. ///此时找遍所有的链表都没有找到相应的Block,则重新找块没用的Block
  1108. if (NULL == ptrBlockReturn)
  1109. {
  1110. ptrBlockReturn = ptrThis->GetFreeBlock(ptrThis);
  1111. }
  1112. return ptrBlockReturn;
  1113. }
  1114. Block *BlockManager_GetFreeBlock(BlockManager *ptrThis)
  1115. {
  1116. Block *ptrBlockReturn = NULL;
  1117. PAssert_ReturnWithValue(ptrThis, NULL);
  1118. if(NULL == ptrThis->ptrBlockGroupHeader)
  1119. {
  1120. ///原来没有空间则动态分配
  1121. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1122. ptrGroupBlock->Init = BlockGroup_Init;
  1123. ptrGroupBlock->Init(ptrGroupBlock);
  1124. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1125. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1126. ptrBlockReturn = ptrGroupBlock->GetFreeBlock(ptrGroupBlock);
  1127. }
  1128. else
  1129. {
  1130. BlockGroup *ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1131. do
  1132. {
  1133. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1134. if (NULL != ptrBlockReturn)
  1135. {
  1136. break;
  1137. }
  1138. else
  1139. {
  1140. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1141. }
  1142. } while (NULL != ptrBlockGroup);
  1143. ///此时找遍所有的链表都没有找到空闲空间,则进行动态分配
  1144. if(NULL == ptrBlockReturn)
  1145. {
  1146. BlockGroup *ptrBlockGroup = (BlockGroup *)malloc(sizeof(BlockGroup));
  1147. ptrBlockGroup->Init = BlockGroup_Init;
  1148. ptrBlockGroup->Init(ptrBlockGroup);
  1149. ptrThis->ptrBlockGroupTail->ptrBlockGroupNext = ptrBlockGroup;
  1150. ptrBlockGroup->ptrBlockGroupPre = ptrThis->ptrBlockGroupTail;
  1151. ptrThis->ptrBlockGroupTail = ptrBlockGroup;
  1152. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1153. }
  1154. }
  1155. return ptrBlockReturn;
  1156. }
  1157. void log4c_block_begin(const char * fileName, int lineNum, const char * traceName)
  1158. {
  1159. TASK_ID threadId = VOS_GetSelfTaskID();
  1160. int i = 0;
  1161. Block *ptrBlock = NULL;
  1162. PAssert_Return(fileName);
  1163. PAssert_Return(traceName);
  1164. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1165. if (NULL != ptrBlock)
  1166. {
  1167. int iBlockLevel = 0;
  1168. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1169. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1170. {
  1171. ptrBlock->SetUseStatus(ptrBlock, BLOCK_BUSY);
  1172. ptrBlock->SetThreadId(ptrBlock, threadId);
  1173. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1174. }
  1175. if (ptrBlock->GetThreadId(ptrBlock) != threadId)
  1176. {
  1177. ptrBlock->SetThreadId(ptrBlock, threadId);
  1178. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1179. }
  1180. ///ptrBlock->SetThreadId(ptrBlock, threadId);
  1181. ///ptrBlock->SetBlockLevel(ptrBlock, 0);
  1182. iBlockLevel = ++(ptrBlock->iBlockLevel) ;
  1183. strncat(strMsgBuffer, "B-Entry\t", sizeof(strMsgBuffer));
  1184. for (i = 0; i < iBlockLevel; i++)
  1185. {
  1186. strncat(strMsgBuffer, "==", sizeof(strMsgBuffer));
  1187. }
  1188. strncat(strMsgBuffer, "> ", sizeof(strMsgBuffer));
  1189. strncat(strMsgBuffer, traceName, sizeof(strMsgBuffer));
  1190. ///调用log4c的函数来记录日志
  1191. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1192. }
  1193. }
  1194. void log4c_block_end(const char * fileName, int lineNum, const char * traceName)
  1195. {
  1196. TASK_ID threadId = VOS_GetSelfTaskID();
  1197. int i = 0;
  1198. Block *ptrBlock = NULL;
  1199. PAssert_Return(fileName);
  1200. PAssert_Return(traceName);
  1201. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1202. if (NULL != ptrBlock)
  1203. {
  1204. int iBlockLevel = ptrBlock->GetBlockLevel(ptrBlock);
  1205. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1206. strncat(strMsgBuffer, "B-Exit\t", sizeof(strMsgBuffer));
  1207. strncat(strMsgBuffer, "<", sizeof(strMsgBuffer));
  1208. for (i = 0; i < iBlockLevel; i++)
  1209. {
  1210. strncat(strMsgBuffer, "==", sizeof(strMsgBuffer));
  1211. }
  1212. strncat(strMsgBuffer, " ", sizeof(strMsgBuffer));
  1213. strncat(strMsgBuffer, traceName, sizeof(strMsgBuffer));
  1214. ///调用log4c的函数来记录日志
  1215. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1216. iBlockLevel = --ptrBlock->iBlockLevel ;
  1217. if (ptrBlock->iBlockLevel == 0)
  1218. {
  1219. ptrBlock->SetThreadId(ptrBlock, 0);
  1220. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1221. ptrBlock->SetUseStatus(ptrBlock, BLOCK_FREE);
  1222. }
  1223. }
  1224. }
  1225. /************************************************************************/
  1226. /************** End of log.c *******************************************/