log.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. 日志记录为一个字符串指针指向的内容
  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_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. /** 日志记录,不记录文件名和行号,没有任何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_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%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_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, 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_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, 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_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, 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_s(str, sizeof(str), _TRUNCATE, "%.4x ",i&0xffff);
  598. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  599. ///ostr << str;
  600. }
  601. if(i<count)
  602. {
  603. _snprintf_s(str, sizeof(str), _TRUNCATE, "%3.2x",buff[i]&0xff);
  604. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  605. ///ostr << str;
  606. }
  607. else
  608. {
  609. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  610. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  611. ///ostr << str;
  612. }
  613. if(!((i+1)%8))
  614. {
  615. if((i+1)%16)
  616. {
  617. _snprintf_s(str, sizeof(str), _TRUNCATE, " -");
  618. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  619. ///ostr << str;
  620. }
  621. else
  622. {
  623. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  624. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  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_s(str, sizeof(str), _TRUNCATE, "%c",buff[j]&0xff);
  634. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  635. }
  636. else
  637. {
  638. _snprintf_s(str, sizeof(str), _TRUNCATE, ".");
  639. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  640. }
  641. }
  642. else
  643. {
  644. _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
  645. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  646. ///ostr << str;
  647. }
  648. }
  649. _snprintf_s(str, sizeof(str), _TRUNCATE, "\n");
  650. strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
  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\" ext=\"txt\" 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\" ext=\"txt\" 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_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
  777. }
  778. void log_set_log_file_name(const char *strFileName)
  779. {
  780. PAssertNotNull_Return(strFileName);
  781. strncpy_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
  782. }
  783. void log_set_log_level(const char *strLogLevel)
  784. {
  785. strncpy_s(gs_st_LogProperty.m_strLogLevel, sizeof(gs_st_LogProperty.m_strLogLevel), strLogLevel, _TRUNCATE);
  786. }
  787. void log_set_log_file_size(const int iFileSize)
  788. {
  789. gs_st_LogProperty.m_iLogFileSize = iFileSize;
  790. }
  791. void log_set_log_file_num(const int iFileNum)
  792. {
  793. gs_st_LogProperty.m_iLogFileNum = iFileNum;
  794. }
  795. void log_set_reread_log_cfg_file(const BOOL bReReadLogCfgFile)
  796. {
  797. gs_st_LogProperty.m_bReReadLogCfgFile = bReReadLogCfgFile;
  798. }
  799. int log_init_with_param()
  800. {
  801. _snprintf_s(gs_strFileContentBuffer, sizeof(gs_strFileContentBuffer),_TRUNCATE,
  802. gs_strFileContentFormat,
  803. gs_st_LogProperty.m_bReReadLogCfgFile,
  804. gs_st_LogProperty.m_strLogLevel,
  805. gs_st_LogProperty.m_iLogFileSize,
  806. gs_st_LogProperty.m_iLogFileNum,
  807. gs_st_LogProperty.m_strLogFileName
  808. );
  809. return log_init_with_string(gs_strFileContentBuffer, gs_st_LogProperty.m_strLogCfgFileName);
  810. }
  811. /************************************************************************/
  812. /************************************************************************/
  813. // Instruct the compiler to put the g_lModuleUsage data
  814. // variable in its own data section, called Shared. We
  815. // then instruct the linker that we want the data in this
  816. // section to be shared by all instances of this application.
  817. #ifdef WIN32
  818. #pragma data_seg("LogShared")
  819. /** 数据库日志模块运行的实例个数
  820. 避免多个日志模块的运行实例使用同一个配置文件,避免使用同一个log文件来记录日志而产生冲突
  821. */
  822. long g_lLogModuleUsage = -1;
  823. #pragma data_seg()
  824. // Instruct the linker to make the Shared section
  825. // readable, writable, and shared.
  826. #pragma comment(linker, "/section:LogShared,rws")
  827. #else
  828. // long g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  829. // int g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
  830. // extern long GetModuleUsage();
  831. // long g_lLogModuleUsage = GetModuleUsage();
  832. long g_lLogModuleUsage = -1;
  833. #endif
  834. int log_init_with_param_multi_process()
  835. {
  836. char strLogCfgFileName[MAX_FILE_NAME_LEN] = {0};
  837. char strLogFileName[MAX_FILE_NAME_LEN] = {0};
  838. char strLogModuleUsageNum[MAX_FILE_NAME_LEN] = {0};
  839. IncreaseLogModuleUsage();
  840. //snprintf(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName),
  841. // "%s%d", gs_st_LogProperty.m_strLogCfgFileName, g_lLogModuleUsage);
  842. //snprintf(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),
  843. // "%s%d", gs_st_LogProperty.m_strLogFileName, g_lLogModuleUsage);
  844. _snprintf_s(strLogModuleUsageNum, sizeof(strLogModuleUsageNum), _TRUNCATE, "%d", g_lLogModuleUsage);
  845. strcat_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strLogModuleUsageNum);
  846. strcat_s(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),strLogModuleUsageNum);
  847. return log_init_with_param();
  848. }
  849. extern long GetModuleUsage();
  850. extern void SetModuleUsage(long lModuleUsage);
  851. void IncreaseLogModuleUsage()
  852. {
  853. #ifdef WIN32
  854. InterlockedIncrement((long*) &g_lLogModuleUsage);
  855. #else
  856. g_lLogModuleUsage = GetModuleUsage();
  857. g_lLogModuleUsage ++;
  858. SetModuleUsage(g_lLogModuleUsage);
  859. #endif
  860. }
  861. /************************************************************************/
  862. /************************************************************************/
  863. ///下面为LOG4C_BLOCK_XXX相关宏定义的实现代码
  864. ///进行LOG4C_BLOCK_BEGIN和LOG4C_BLOCK_END宏定义使用的Block结构
  865. struct ST_Block
  866. {
  867. #define BLOCK_FREE 0
  868. #define BLOCK_BUSY 1
  869. ///标志是否被使用
  870. int iUseStatus;
  871. ///线程ID
  872. TASK_ID threadId;
  873. ///Block的层次
  874. int iBlockLevel;
  875. void (*Init)(struct ST_Block *ptrThis);
  876. void (*Reset)(struct ST_Block *ptrThis);
  877. int (*GetUseStatus)(struct ST_Block *ptrThis);
  878. TASK_ID (*GetThreadId)(struct ST_Block *ptrThis);
  879. int (*GetBlockLevel)(struct ST_Block *ptrThis);
  880. void (*SetUseStatus)(struct ST_Block *ptrThis, int iUseStatus);
  881. void (*SetThreadId)(struct ST_Block *ptrThis, TASK_ID threadId);
  882. void (*SetBlockLevel)(struct ST_Block *ptrThis, int iBlockLevel);
  883. };
  884. typedef struct ST_Block Block;
  885. /**一个ST_BlockGroup包含5个ST_Block
  886. ST_BlockGroup中作为ST_BlockManager中管理的节点
  887. */
  888. struct ST_BlockGroup
  889. {
  890. #define BLOCK_GROUP_BLOCK_NUM (5)
  891. Block aBlock[BLOCK_GROUP_BLOCK_NUM];
  892. struct ST_BlockGroup *ptrBlockGroupPre;
  893. struct ST_BlockGroup *ptrBlockGroupNext;
  894. void (*Init)(struct ST_BlockGroup *ptrThis);
  895. Block *(*GetThreadBlock)(struct ST_BlockGroup *ptrThis, TASK_ID threadId);
  896. Block *(*GetFreeBlock)(struct ST_BlockGroup *ptrThis);
  897. };
  898. typedef struct ST_BlockGroup BlockGroup;
  899. struct ST_BlockManager
  900. {
  901. BlockGroup *ptrBlockGroupHeader;
  902. BlockGroup *ptrBlockGroupTail;
  903. Block *(*GetThreadBlock)(struct ST_BlockManager *ptrThis, TASK_ID threadId);
  904. Block *(*GetFreeBlock)(struct ST_BlockManager *ptrThis);
  905. };
  906. typedef struct ST_BlockManager BlockManager;
  907. extern Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId);
  908. extern Block *BlockManager_GetFreeBlock(BlockManager *ptrThis);
  909. BlockManager g_blockManager =
  910. {
  911. NULL,
  912. NULL,
  913. BlockManager_GetThreadBlock,
  914. BlockManager_GetFreeBlock
  915. };
  916. extern void Block_Reset(Block *ptrThis);
  917. extern int Block_GetUseStatus(Block *ptrThis);
  918. extern TASK_ID Block_GetThreadId(Block *ptrThis);
  919. extern int Block_GetBlockLevel(Block *ptrThis);
  920. extern void Block_SetUseStatus(Block *ptrThis, int iUseStatus);
  921. extern void Block_SetThreadId(Block *ptrThis, TASK_ID threadId);
  922. extern void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel);
  923. /**ST_Block的初始化代码
  924. @param :Block *ptrThis
  925. @rerurn void.
  926. */
  927. void Block_Init(Block *ptrThis)
  928. {
  929. PAssert_Return(ptrThis);
  930. memset(ptrThis, 0, sizeof(*ptrThis));
  931. ///设置本结构的成员函数
  932. ptrThis->Init = Block_Init;
  933. ptrThis->Reset = Block_Reset;
  934. ptrThis->SetBlockLevel = Block_SetBlockLevel;
  935. ptrThis->GetBlockLevel = Block_GetBlockLevel;
  936. ptrThis->SetThreadId = Block_SetThreadId;
  937. ptrThis->GetThreadId = Block_GetThreadId;
  938. ptrThis->SetUseStatus = Block_SetUseStatus;
  939. ptrThis->GetUseStatus = Block_GetUseStatus;
  940. return;
  941. }
  942. /**ST_Block的重置代码
  943. @param :Block *ptrThis
  944. @rerurn void.
  945. */
  946. void Block_Reset(Block *ptrThis)
  947. {
  948. PAssert_Return(ptrThis);
  949. Block_Init(ptrThis);
  950. return;
  951. }
  952. /**ST_Block的获取成员变量iUseStatus的函数
  953. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  954. @param :int
  955. @rerurn void.
  956. */
  957. int Block_GetUseStatus(Block *ptrThis)
  958. {
  959. PAssert_ReturnWithValue(ptrThis, 0);
  960. return ptrThis->iUseStatus;
  961. }
  962. /**ST_Block的获取成员变量threadId的函数
  963. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  964. @param :TASK_ID.
  965. @rerurn void.
  966. */
  967. TASK_ID Block_GetThreadId(Block *ptrThis)
  968. {
  969. PAssert_ReturnWithValue(ptrThis, 0);
  970. return ptrThis->threadId;
  971. }
  972. /**ST_Block的获取成员变量iBlockLevel的函数
  973. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  974. @param :int.
  975. @rerurn void.
  976. */
  977. int Block_GetBlockLevel(Block *ptrThis)
  978. {
  979. PAssert_ReturnWithValue(ptrThis, 0);
  980. return ptrThis->iBlockLevel;
  981. }
  982. /**ST_Block的设置成员变量iUseStatus的函数
  983. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  984. @param :Block *ptrThis
  985. @param :int
  986. @rerurn void.
  987. */
  988. void Block_SetUseStatus(Block *ptrThis, int iUseStatus)
  989. {
  990. PAssert_Return(ptrThis);
  991. ptrThis->iUseStatus = iUseStatus;
  992. }
  993. /**ST_Block的设置成员变量threadId的函数
  994. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  995. @param :Block *ptrThis
  996. @param :TASK_ID.
  997. @rerurn void.
  998. */
  999. void Block_SetThreadId(Block *ptrThis, TASK_ID threadId)
  1000. {
  1001. PAssert_Return(ptrThis);
  1002. ptrThis->threadId = threadId;
  1003. }
  1004. /**ST_Block的设置成员变量iBlockLevel的函数
  1005. 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
  1006. @param :Block *ptrThis
  1007. @param :int.
  1008. @rerurn void.
  1009. */
  1010. void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel)
  1011. {
  1012. PAssert_Return(ptrThis);
  1013. ptrThis->iBlockLevel = iBlockLevel;
  1014. }
  1015. extern Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId);
  1016. extern Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis);
  1017. /**ST_BlockGroup的初始化代码
  1018. @param :Block *ptrThis
  1019. @rerurn void.
  1020. */
  1021. void BlockGroup_Init(BlockGroup *ptrThis)
  1022. {
  1023. int i = 0;
  1024. PAssert_Return(ptrThis);
  1025. memset(ptrThis, 0, sizeof(*ptrThis));
  1026. ptrThis->Init = BlockGroup_Init;
  1027. ptrThis->GetThreadBlock = BlockGroup_GetThreadBlock;
  1028. ptrThis->GetFreeBlock = BlockGroup_GetFreeBlock;
  1029. ///初始化每个Block
  1030. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1031. {
  1032. Block *ptrBlock = &ptrThis->aBlock[i];
  1033. ptrBlock->Init = Block_Init;
  1034. ptrBlock->Init(ptrBlock);
  1035. }
  1036. return;
  1037. }
  1038. /**从ST_BlockGroup中获取给定线程的Block的指针
  1039. @param :Block *ptrThis
  1040. @param :TASK_ID threadId
  1041. @rerurn Block *.
  1042. */
  1043. Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId)
  1044. {
  1045. Block *ptrBlockReturn = NULL;
  1046. int i = 0;
  1047. PAssert_ReturnWithValue(ptrThis, NULL);
  1048. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1049. {
  1050. Block *ptrBlock = &ptrThis->aBlock[i];
  1051. if (ptrBlock->GetThreadId(ptrBlock) == threadId)
  1052. {
  1053. ptrBlockReturn = ptrBlock;
  1054. break;
  1055. }
  1056. }
  1057. return ptrBlockReturn;
  1058. }
  1059. /**从ST_BlockGroup中获取空闲的Block的指针
  1060. @param :BlockGroup *ptrThis
  1061. @rerurn Block *.
  1062. */
  1063. Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis)
  1064. {
  1065. Block *ptrBlockReturn = NULL;
  1066. int i = 0;
  1067. PAssert_ReturnWithValue(ptrThis, NULL);
  1068. for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
  1069. {
  1070. Block *ptrBlock = &ptrThis->aBlock[i];
  1071. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1072. {
  1073. ptrBlockReturn = ptrBlock;
  1074. break;
  1075. }
  1076. }
  1077. return ptrBlockReturn;
  1078. }
  1079. Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId)
  1080. {
  1081. Block *ptrBlockReturn = NULL;
  1082. BlockGroup *ptrBlockGroup = NULL;
  1083. PAssert_ReturnWithValue(ptrThis, NULL);
  1084. if (NULL == ptrThis->ptrBlockGroupHeader)
  1085. {
  1086. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1087. ptrGroupBlock->Init = BlockGroup_Init;
  1088. ptrGroupBlock->Init(ptrGroupBlock);
  1089. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1090. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1091. }
  1092. PAssert_ReturnWithValue(ptrThis->ptrBlockGroupHeader, NULL);
  1093. ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1094. do
  1095. {
  1096. ptrBlockReturn = ptrBlockGroup->GetThreadBlock(ptrBlockGroup, threadId);
  1097. if (NULL != ptrBlockReturn)
  1098. {
  1099. break;
  1100. }
  1101. else
  1102. {
  1103. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1104. }
  1105. } while (NULL != ptrBlockGroup);
  1106. ///此时找遍所有的链表都没有找到相应的Block,则重新找块没用的Block
  1107. if (NULL == ptrBlockReturn)
  1108. {
  1109. ptrBlockReturn = ptrThis->GetFreeBlock(ptrThis);
  1110. }
  1111. return ptrBlockReturn;
  1112. }
  1113. Block *BlockManager_GetFreeBlock(BlockManager *ptrThis)
  1114. {
  1115. Block *ptrBlockReturn = NULL;
  1116. PAssert_ReturnWithValue(ptrThis, NULL);
  1117. if(NULL == ptrThis->ptrBlockGroupHeader)
  1118. {
  1119. ///原来没有空间则动态分配
  1120. BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
  1121. ptrGroupBlock->Init = BlockGroup_Init;
  1122. ptrGroupBlock->Init(ptrGroupBlock);
  1123. ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
  1124. ptrThis->ptrBlockGroupTail = ptrGroupBlock;
  1125. ptrBlockReturn = ptrGroupBlock->GetFreeBlock(ptrGroupBlock);
  1126. }
  1127. else
  1128. {
  1129. BlockGroup *ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
  1130. do
  1131. {
  1132. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1133. if (NULL != ptrBlockReturn)
  1134. {
  1135. break;
  1136. }
  1137. else
  1138. {
  1139. ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
  1140. }
  1141. } while (NULL != ptrBlockGroup);
  1142. ///此时找遍所有的链表都没有找到空闲空间,则进行动态分配
  1143. if(NULL == ptrBlockReturn)
  1144. {
  1145. BlockGroup *ptrBlockGroup = (BlockGroup *)malloc(sizeof(BlockGroup));
  1146. ptrBlockGroup->Init = BlockGroup_Init;
  1147. ptrBlockGroup->Init(ptrBlockGroup);
  1148. ptrThis->ptrBlockGroupTail->ptrBlockGroupNext = ptrBlockGroup;
  1149. ptrBlockGroup->ptrBlockGroupPre = ptrThis->ptrBlockGroupTail;
  1150. ptrThis->ptrBlockGroupTail = ptrBlockGroup;
  1151. ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
  1152. }
  1153. }
  1154. return ptrBlockReturn;
  1155. }
  1156. void log4c_block_begin(const char * fileName, int lineNum, const char * traceName)
  1157. {
  1158. TASK_ID threadId = VOS_GetSelfTaskID();
  1159. int i = 0;
  1160. Block *ptrBlock = NULL;
  1161. PAssert_Return(fileName);
  1162. PAssert_Return(traceName);
  1163. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1164. if (NULL != ptrBlock)
  1165. {
  1166. int iBlockLevel = 0;
  1167. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1168. if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
  1169. {
  1170. ptrBlock->SetUseStatus(ptrBlock, BLOCK_BUSY);
  1171. ptrBlock->SetThreadId(ptrBlock, threadId);
  1172. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1173. }
  1174. if (ptrBlock->GetThreadId(ptrBlock) != threadId)
  1175. {
  1176. ptrBlock->SetThreadId(ptrBlock, threadId);
  1177. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1178. }
  1179. ///ptrBlock->SetThreadId(ptrBlock, threadId);
  1180. ///ptrBlock->SetBlockLevel(ptrBlock, 0);
  1181. iBlockLevel = ++(ptrBlock->iBlockLevel) ;
  1182. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Entry\t", sizeof(strMsgBuffer));
  1183. for (i = 0; i < iBlockLevel; i++)
  1184. {
  1185. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX,"==", sizeof(strMsgBuffer));
  1186. }
  1187. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "> ", sizeof(strMsgBuffer));
  1188. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
  1189. ///调用log4c的函数来记录日志
  1190. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1191. }
  1192. }
  1193. void log4c_block_end(const char * fileName, int lineNum, const char * traceName)
  1194. {
  1195. TASK_ID threadId = VOS_GetSelfTaskID();
  1196. int i = 0;
  1197. Block *ptrBlock = NULL;
  1198. PAssert_Return(fileName);
  1199. PAssert_Return(traceName);
  1200. ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
  1201. if (NULL != ptrBlock)
  1202. {
  1203. int iBlockLevel = ptrBlock->GetBlockLevel(ptrBlock);
  1204. char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
  1205. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Exit\t", sizeof(strMsgBuffer));
  1206. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "<", sizeof(strMsgBuffer));
  1207. for (i = 0; i < iBlockLevel; i++)
  1208. {
  1209. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "==", sizeof(strMsgBuffer));
  1210. }
  1211. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, " ", sizeof(strMsgBuffer));
  1212. strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
  1213. ///调用log4c的函数来记录日志
  1214. log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
  1215. iBlockLevel = --ptrBlock->iBlockLevel ;
  1216. if (ptrBlock->iBlockLevel == 0)
  1217. {
  1218. ptrBlock->SetThreadId(ptrBlock, 0);
  1219. ptrBlock->SetBlockLevel(ptrBlock, 0);
  1220. ptrBlock->SetUseStatus(ptrBlock, BLOCK_FREE);
  1221. }
  1222. }
  1223. }
  1224. /************************************************************************/
  1225. /************** End of log.c *******************************************/