log.c 37 KB

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