log.c 36 KB

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