Logger.cpp 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. // Logger.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "Logger.h"
  5. #include "BuffManager.h"
  6. #include "FileManager.h"
  7. #include "localConfig.h"
  8. #include "CircleBuff.h"
  9. #include "common_api.h"
  10. #include "LogFileSaveThread.h"
  11. #include "CDInterface.h"
  12. #include <iostream>
  13. #include<chrono>
  14. bool g_SystemInit = false;
  15. Logger *g_pProcLogger = NULL;
  16. Logger *g_pBusLogger = NULL;
  17. void UpdateLogger(Logger &log)
  18. {
  19. Logger_Pattern Pattern;
  20. log.GetLogPattern(Pattern);
  21. ResDataObject res;
  22. if (GetLogPatternResource(res))
  23. {
  24. try {
  25. Pattern.m_Level = (LOG_LEVEL)((int)res["LEVEL"]);
  26. Pattern.MaxFileSize = res["MAXFILESIZE"];
  27. Pattern.MaxBackupCount = res["MAXBACKUPCOUNT"];
  28. Pattern.MaxTimePeriod = res["MAXTIMEPERIOD"];
  29. //debug info
  30. Pattern.PrintFileName = res["FILENAME"];
  31. Pattern.PrintFunctionName = res["FUNCTIONNAME"];
  32. Pattern.PrintLine = res["LINE"];
  33. //date
  34. Pattern.PrintDate = res["DATE"];
  35. //Proc
  36. Pattern.PrintProcId = res["PROC"];
  37. //Thread
  38. Pattern.PrintThreadId = res["THREAD"];
  39. Pattern.Cachable = res["CACHE"];
  40. log.SetLogPattern(Pattern);
  41. }
  42. catch (...)
  43. {
  44. }
  45. }
  46. };
  47. class Logger_Monitor {
  48. public:
  49. Logger_Monitor()
  50. {
  51. string FullFileName = GetProcessDirectory() + string("\\logs\\General.log");
  52. Logger* p = (new Logger());
  53. UpdateLogger(*p);
  54. if (p)
  55. {
  56. p->SetLogFilepath(FullFileName.c_str());
  57. g_pProcLogger = p;
  58. }
  59. FullFileName = GetProcessDirectory() + string("\\logs\\eBus.log");
  60. p = (new Logger());
  61. UpdateLogger(*p);
  62. if (p)
  63. {
  64. p->SetLogFilepath(FullFileName.c_str());
  65. g_pBusLogger = p;
  66. }
  67. };
  68. ~Logger_Monitor()
  69. {
  70. if (g_pProcLogger)
  71. {
  72. delete (g_pProcLogger);
  73. g_pProcLogger = NULL;
  74. }
  75. if (g_pBusLogger)
  76. {
  77. delete (g_pBusLogger);
  78. g_pBusLogger = NULL;
  79. }
  80. };
  81. };
  82. Logger_Monitor g_Logger_Monitor;
  83. Logger_Pattern::Logger_Pattern(void)
  84. {
  85. m_Level = LOG_LEVEL_INIT;
  86. MaxFileSize = 1024*1024*10;//log file size limit.def:10M
  87. MaxBackupCount = 1;//log file count limit.def:0
  88. MaxTimePeriod = 0;//use less
  89. PrintFileName = true;
  90. PrintFunctionName = false;
  91. PrintLine = true;
  92. //date
  93. PrintDate = false;
  94. //Proc
  95. PrintProcId = false;
  96. //Thread
  97. PrintThreadId = false;
  98. Cachable = true;
  99. }
  100. Logger_Pattern::~Logger_Pattern(void)
  101. {
  102. }
  103. bool Logger_Pattern::SetLevel(LONG Level)
  104. {
  105. if (Level < LOG_LEVEL_INIT || Level >= LOG_LEVEL_MAX)
  106. {
  107. return false;
  108. }
  109. m_Level = (LOG_LEVEL)Level;
  110. return true;
  111. }
  112. Logger::Logger(Logger &obj)
  113. {
  114. (*m_pFullLogFileName) = (*obj.m_pFullLogFileName);
  115. (*m_pPattern) = (*obj.m_pPattern);
  116. }
  117. Logger::Logger()
  118. {
  119. m_pFullLogFileName = new string();
  120. m_pErrFullLogFileName = new string();
  121. m_pPattern = new Logger_Pattern();
  122. m_FileId = 0;
  123. }
  124. Logger::~Logger()
  125. {
  126. Thread_Lock(7000);
  127. delete m_pFullLogFileName;
  128. delete m_pErrFullLogFileName;
  129. delete m_pPattern;
  130. Thread_UnLock();
  131. }
  132. Logger& Logger::operator = (const Logger &tValue)
  133. {
  134. Thread_Lock();
  135. if (this != &tValue)
  136. {
  137. (*m_pFullLogFileName) = (*tValue.m_pFullLogFileName);
  138. (*m_pPattern) = (*tValue.m_pPattern);
  139. }
  140. Thread_UnLock();
  141. return (*this);
  142. }
  143. void Logger::SetLogPattern(Logger_Pattern &Pattern)
  144. {
  145. Thread_Lock();
  146. (*m_pPattern) = Pattern;
  147. Thread_UnLock();
  148. }
  149. void Logger::GetLogPattern(Logger_Pattern &Pattern)
  150. {
  151. Thread_Lock();
  152. Pattern = (*m_pPattern);
  153. Thread_UnLock();
  154. }
  155. void Logger::SetLogFilepath(const char *pPath)
  156. {
  157. Thread_Lock();
  158. //create id
  159. if (pPath && strlen(pPath) > 0)
  160. {
  161. m_FileId = g_BlockMap.CreateLogFile(pPath);
  162. (*m_pFullLogFileName) = pPath;
  163. }
  164. Thread_UnLock();
  165. }
  166. bool Logger::IsLogFilePathExist()
  167. {
  168. bool ret = false;
  169. Thread_Lock();
  170. if (m_pFullLogFileName->size() > 0)
  171. {
  172. ret = true;
  173. }
  174. Thread_UnLock();
  175. return ret;
  176. }
  177. const char* Logger::GetErrLogFilePath()
  178. {
  179. bool ret = false;
  180. Thread_Lock();
  181. if (m_pFullLogFileName->size() > 0)
  182. {
  183. (*m_pErrFullLogFileName) = ReplaceFileTitle((*m_pFullLogFileName), string("err"));
  184. ret = true;
  185. }
  186. Thread_UnLock();
  187. if (ret)
  188. {
  189. return m_pErrFullLogFileName->c_str();
  190. }
  191. return NULL;
  192. }
  193. bool Logger::ChangeLoggerFileTitle(Logger* p, const char *pTitle)
  194. {
  195. bool ret = false;
  196. if (p == NULL || pTitle == NULL)
  197. {
  198. return ret;
  199. }
  200. string FilePath = GetFileDirectory((*m_pFullLogFileName));
  201. FilePath += string("\\") + string(pTitle);
  202. Thread_Lock();
  203. ret = g_BlockMap.ChangeFilePath(m_FileId, FilePath.c_str());
  204. Thread_UnLock();
  205. return ret;
  206. }
  207. DWORD Logger::GetLogFileId()
  208. {
  209. return m_FileId;
  210. }
  211. int Logger::GetLogLevel()
  212. {
  213. return m_pPattern->m_Level;
  214. }
  215. LOGGER_C_API Logger* GetBusLogger()
  216. {
  217. return g_pBusLogger;
  218. }
  219. LOGGER_C_API Logger* GetGlobalLogger()
  220. {
  221. return g_pProcLogger;
  222. }
  223. LOGGER_C_API Logger* GetThreadLogger()
  224. {
  225. PVOID pLogger = NULL;
  226. //#ifndef TEMP_COMP
  227. //CDInterface *p = CDInterface::GetCDI();
  228. //if (p)
  229. //{
  230. // p->GetThreadsLogger(GetCurrentThreadId(), pLogger);
  231. //}
  232. //#endif
  233. return (Logger*)pLogger;
  234. }
  235. LOGGER_C_API Logger* CreateLogger()
  236. {
  237. Logger* p = (new Logger());
  238. UpdateLogger(*p);
  239. return p;
  240. }
  241. LOGGER_C_API void ReleseLogger(Logger* p)
  242. {
  243. if ((g_pProcLogger == p) || (g_pBusLogger == p))
  244. {
  245. return;
  246. }
  247. delete p;
  248. }
  249. LOGGER_C_API void ExitLoggerModule()
  250. {
  251. printf("Logger:StopThread Entry\n");
  252. if (g_LogThread.StopThread() == false)
  253. {
  254. printf("Logger:StopThread Used Terminate!!!!!!!!!!!!!!!\n");
  255. }
  256. printf("Logger:StopThread Exit\n");
  257. }
  258. size_t GetHeadFormat(LOG_LEVEL Level,
  259. const char* file,
  260. int line,
  261. const char* function,
  262. Logger_Pattern &Pattern, BuffNode* pNode)
  263. {
  264. if (Level < Pattern.m_Level)
  265. {
  266. return 0;
  267. }
  268. int copiedcount = 0;
  269. size_t Idx = strlen(&(pNode->m_Buff[0]));
  270. SYSTEMTIME st;
  271. GetLocalTime(&st);
  272. //return code
  273. char szRet[] = "\n";
  274. if (strncpy_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, szRet, _TRUNCATE) == 0)
  275. {
  276. Idx += strlen(szRet);
  277. }
  278. else
  279. {
  280. return PAGE - 1;
  281. }
  282. //DATE
  283. if (Pattern.PrintDate)
  284. {
  285. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%04d:%02d:%02d] ", st.wYear, st.wMonth, st.wDay);
  286. if (copiedcount > 0)
  287. {
  288. Idx += copiedcount;
  289. }
  290. else
  291. {
  292. return PAGE - 1;
  293. }
  294. }
  295. //TIME
  296. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%02d:%02d:%02d:%03d] ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  297. if (copiedcount > 0)
  298. {
  299. Idx += copiedcount;
  300. }
  301. else
  302. {
  303. return PAGE - 1;
  304. }
  305. //LEVEL
  306. switch (Level)
  307. {
  308. case LOG_LEVEL_TRACE:
  309. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "TRACE");
  310. break;
  311. case LOG_LEVEL_DEBUG:
  312. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "DEBUG");
  313. break;
  314. case LOG_LEVEL_INFO:
  315. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "INFO");
  316. break;
  317. case LOG_LEVEL_WARNING:
  318. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "WARNING");
  319. break;
  320. case LOG_LEVEL_ERROR:
  321. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "ERROR");
  322. break;
  323. case LOG_LEVEL_FATAL:
  324. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "FATAL");
  325. break;
  326. default:
  327. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%-7s] ", "UNKNOWN");
  328. break;
  329. }
  330. if (copiedcount > 0)
  331. {
  332. Idx += copiedcount;
  333. }
  334. else
  335. {
  336. return PAGE - 1;
  337. }
  338. //PID
  339. if (Pattern.PrintProcId)
  340. {
  341. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[P:%08X] ", GetCurrentProcessId());
  342. if (copiedcount > 0)
  343. {
  344. Idx += copiedcount;
  345. }
  346. else
  347. {
  348. return PAGE - 1;
  349. }
  350. }
  351. //TID
  352. if (Pattern.PrintThreadId)
  353. {
  354. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[T:%08X] ", GetCurrentThreadId());
  355. if (copiedcount > 0)
  356. {
  357. Idx += copiedcount;
  358. }
  359. else
  360. {
  361. return PAGE - 1;
  362. }
  363. }
  364. //file name
  365. if (Pattern.PrintFileName)
  366. {
  367. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "[%s", file);
  368. if (copiedcount > 0)
  369. {
  370. Idx += copiedcount;
  371. }
  372. else
  373. {
  374. return PAGE - 1;
  375. }
  376. }
  377. //line num
  378. if (Pattern.PrintLine)
  379. {
  380. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "(%d)", line);
  381. if (copiedcount > 0)
  382. {
  383. Idx += copiedcount;
  384. }
  385. else
  386. {
  387. return PAGE - 1;
  388. }
  389. }
  390. //func name
  391. if (Pattern.PrintFunctionName)
  392. {
  393. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "(%s)", function);
  394. if (copiedcount > 0)
  395. {
  396. Idx += copiedcount;
  397. }
  398. else
  399. {
  400. return PAGE - 1;
  401. }
  402. }
  403. char szLast[] = "] :";
  404. //last one
  405. if (strncpy_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, szLast, _TRUNCATE) == 0)
  406. {
  407. Idx += strlen(szLast);
  408. return Idx;
  409. }
  410. return PAGE - 1;
  411. }
  412. void SaveTheLog(Logger_Pattern &Pattern, Logger *pLog, const char *pContext, size_t ContextSize, int Level)
  413. {
  414. LOGFILESTATUS status;
  415. status.InitStatus = true;
  416. status.MaxBackupCount = Pattern.MaxBackupCount;
  417. status.MaxFileSize = Pattern.MaxFileSize;
  418. status.MaxTimePeriod = Pattern.MaxTimePeriod;
  419. if (g_SystemInit == false && Pattern.Cachable != 0)
  420. {
  421. g_SystemInit = g_LogThread.StartThread();
  422. }
  423. if (Level >= LOG_LEVEL_ERROR)
  424. {
  425. const char *pFileName = pLog->GetErrLogFilePath();
  426. if (pFileName)
  427. {
  428. bool ret = g_Filemanager.PrepLogFile(status, pFileName);
  429. //filename + Context + ContextLen
  430. if (ret)
  431. {
  432. ret &= g_Filemanager.SaveContextToFile(pFileName, pContext, ContextSize);
  433. }
  434. if (ret == false)
  435. {
  436. for (DWORD i = 0; i < 10; i++)
  437. {
  438. printf("ERROR:----can't save error information\n");
  439. printf("ERROR:%s\n", pContext);
  440. }
  441. }
  442. }
  443. }
  444. else
  445. {
  446. g_BlockMap.WriteLogFile(pLog->GetLogFileId(), status, pContext, (DWORD)ContextSize);
  447. if (Pattern.Cachable == 0)
  448. {
  449. g_LogThread.DoSaveProcedure();
  450. }
  451. }
  452. //g_Filemanager.SaveLogFile(Pattern, pFileName, pContext, ContextSize);
  453. }
  454. LOGGER_C_API void __G_DebugPrintA(
  455. int Level,
  456. const char* file,
  457. int line,
  458. const char* function,
  459. const char* fmt,
  460. ...)
  461. {
  462. Logger *plog = GetGlobalLogger();
  463. Logger_Pattern Pattern;
  464. if (plog == NULL)
  465. {
  466. return;
  467. }
  468. if (plog->GetLogFileId() == 0)
  469. {
  470. return;
  471. }
  472. plog->Thread_Lock();
  473. (*plog).GetLogPattern(Pattern);
  474. if (Pattern.m_Level == LOG_LEVEL_INIT)
  475. {
  476. UpdateLogger((*plog));
  477. (*plog).GetLogPattern(Pattern);
  478. }
  479. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  480. {
  481. //good to go
  482. if ((*plog).IsLogFilePathExist())
  483. {
  484. //the whole pattern like below line
  485. /*
  486. //full log line
  487. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  488. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  489. //minimum log line
  490. [14:03:35:0231] [DEBUG]: context......
  491. */
  492. //head format
  493. BuffNode *pNode = g_BuffManager.GetBuff();
  494. if (pNode)
  495. {
  496. size_t Idx = GetHeadFormat((LOG_LEVEL)Level, file, line, function, Pattern, pNode);
  497. if (NULL != fmt && Idx < PAGE - 1)
  498. {
  499. va_list marker = NULL;
  500. va_start(marker, fmt);
  501. //size_t nLength = _vscprintf(fmt, marker) + 1;
  502. int copiedcount = vsnprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, fmt, marker);
  503. if (copiedcount > 0)
  504. {
  505. Idx += copiedcount;
  506. }
  507. else
  508. {
  509. Idx = PAGE - 1;
  510. }
  511. //save to the file ,Len = Idx,buff = pNode,FileName = pFilepath
  512. SaveTheLog(Pattern, plog, &(pNode->m_Buff[0]), Idx, Level);
  513. va_end(marker);
  514. }
  515. g_BuffManager.ReleaseBuff(pNode);
  516. }
  517. }
  518. }
  519. plog->Thread_UnLock();
  520. }
  521. LOGGER_C_API void __T_DebugPrintA(
  522. int Level,
  523. const char* file,
  524. int line,
  525. const char* function,
  526. const char* fmt,
  527. ...)
  528. {
  529. Logger *plog = GetThreadLogger();
  530. Logger_Pattern Pattern;
  531. if (plog == NULL)
  532. {
  533. return;
  534. }
  535. if (plog->GetLogFileId() == 0)
  536. {
  537. return;
  538. }
  539. plog->Thread_Lock();
  540. (*plog).GetLogPattern(Pattern);
  541. if (Pattern.m_Level == LOG_LEVEL_INIT)
  542. {
  543. UpdateLogger((*plog));
  544. (*plog).GetLogPattern(Pattern);
  545. }
  546. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  547. {
  548. //good to go
  549. if ((*plog).IsLogFilePathExist())
  550. {
  551. //the whole pattern like below line
  552. /*
  553. //full log line
  554. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  555. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  556. //minimum log line
  557. [14:03:35:0231] [DEBUG]: context......
  558. */
  559. //head format
  560. BuffNode *pNode = g_BuffManager.GetBuff();
  561. if (pNode)
  562. {
  563. size_t Idx = GetHeadFormat((LOG_LEVEL)Level, file, line, function, Pattern, pNode);
  564. if (NULL != fmt && Idx < PAGE - 1)
  565. {
  566. va_list marker = NULL;
  567. va_start(marker, fmt);
  568. //size_t nLength = _vscprintf(fmt, marker) + 1;
  569. int copiedcount = vsnprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, fmt, marker);
  570. if (copiedcount > 0)
  571. {
  572. Idx += copiedcount;
  573. }
  574. else
  575. {
  576. Idx = PAGE - 1;
  577. }
  578. //save to the file ,Len = Idx,buff = pNode,FileName = pFilepath
  579. SaveTheLog(Pattern, plog, &(pNode->m_Buff[0]), Idx, Level);
  580. va_end(marker);
  581. }
  582. g_BuffManager.ReleaseBuff(pNode);
  583. }
  584. }
  585. }
  586. plog->Thread_UnLock();
  587. }
  588. LOGGER_C_API void PrintA_IOLOG(Logger*& plog, int Level, const char* pszContext, size_t ContextSize)
  589. {
  590. Logger_Pattern Pattern;
  591. if (plog == NULL)
  592. {
  593. return;
  594. }
  595. if (plog->GetLogFileId() == 0)
  596. {
  597. return;
  598. }
  599. plog->Thread_Lock();
  600. (*plog).GetLogPattern(Pattern);
  601. if (Pattern.m_Level == LOG_LEVEL_INIT)
  602. {
  603. UpdateLogger((*plog));
  604. (*plog).GetLogPattern(Pattern);
  605. }
  606. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  607. {
  608. //good to go
  609. if ((*plog).IsLogFilePathExist())
  610. {
  611. //Context is made by io level.just save
  612. SaveTheLog(Pattern, plog, pszContext, ContextSize, Level);
  613. }
  614. }
  615. plog->Thread_UnLock();
  616. }
  617. static string CurrentDateTime()
  618. {
  619. std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  620. char buf[100] = { 0 };
  621. std::strftime(buf, sizeof(buf), " %Y-%m-%d %H:%M:%S ", std::localtime(&now));
  622. return buf;
  623. }
  624. LOGGER_C_API void __DebugPrintA(Logger* &plog,
  625. int Level,
  626. const char* file,
  627. int line,
  628. const char* function,
  629. const char* fmt,
  630. ...)
  631. {
  632. Logger_Pattern Pattern;
  633. if (plog == NULL)
  634. {
  635. char szMsg[512];
  636. va_list marker = NULL;
  637. va_start(marker, fmt);
  638. //size_t nLength = _vscprintf(fmt, marker) + 1;
  639. int copiedcount = vsnprintf_s(szMsg, sizeof(szMsg), sizeof(szMsg)-1, fmt, marker);
  640. if (copiedcount > 0)
  641. {
  642. //printf("++++!!!! TID [%d] [%s] fuc : [%s] %s \n",GetCurrentThreadId(), CurrentDateTime().c_str(), function, szMsg);
  643. }
  644. va_end(marker);
  645. return;
  646. }
  647. if (plog->GetLogFileId() == 0)
  648. {
  649. return;
  650. }
  651. plog->Thread_Lock();
  652. (*plog).GetLogPattern(Pattern);
  653. if (Pattern.m_Level == LOG_LEVEL_INIT)
  654. {
  655. UpdateLogger((*plog));
  656. (*plog).GetLogPattern(Pattern);
  657. }
  658. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  659. {
  660. //good to go
  661. if ((*plog).IsLogFilePathExist())
  662. {
  663. //the whole pattern like below line
  664. /*
  665. //full log line
  666. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  667. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  668. //minimum log line
  669. [14:03:35:0231] [DEBUG]: context......
  670. */
  671. //head format
  672. BuffNode *pNode = g_BuffManager.GetBuff();
  673. if (pNode)
  674. {
  675. size_t Idx = GetHeadFormat((LOG_LEVEL)Level, file, line, function, Pattern, pNode);
  676. if (NULL != fmt && Idx < PAGE - 1)
  677. {
  678. va_list marker = NULL;
  679. va_start(marker, fmt);
  680. //size_t nLength = _vscprintf(fmt, marker) + 1;
  681. int copiedcount = vsnprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, fmt, marker);
  682. if (copiedcount > 0)
  683. {
  684. Idx += copiedcount;
  685. }
  686. else
  687. {
  688. Idx = PAGE - 1;
  689. }
  690. //save to the file ,Len = Idx,buff = pNode,FileName = pFilepath
  691. SaveTheLog(Pattern, plog, &(pNode->m_Buff[0]), Idx, Level);
  692. va_end(marker);
  693. }
  694. g_BuffManager.ReleaseBuff(pNode);
  695. }
  696. }
  697. }
  698. plog->Thread_UnLock();
  699. }
  700. LOGGER_C_API void __Res_DebugPrintA(
  701. Logger* &plog,
  702. ResDataObject &obj,
  703. int Level,
  704. const char* file,
  705. int line,
  706. const char* function,
  707. const char* fmt,
  708. ...)
  709. {
  710. Logger_Pattern Pattern;
  711. if (plog == NULL)
  712. {
  713. return;
  714. }
  715. if (plog->GetLogFileId() == 0)
  716. {
  717. return;
  718. }
  719. plog->Thread_Lock();
  720. (*plog).GetLogPattern(Pattern);
  721. if (Pattern.m_Level == LOG_LEVEL_INIT)
  722. {
  723. UpdateLogger((*plog));
  724. (*plog).GetLogPattern(Pattern);
  725. }
  726. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  727. {
  728. //good to go
  729. if ((*plog).IsLogFilePathExist())
  730. {
  731. //the whole pattern like below line
  732. /*
  733. //full log line
  734. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  735. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  736. //minimum log line
  737. [14:03:35:0231] [DEBUG]: context......
  738. */
  739. //head format
  740. BuffNode *pNode = g_BuffManager.GetBuff();
  741. if (pNode)
  742. {
  743. size_t Idx = GetHeadFormat((LOG_LEVEL)Level, file, line, function, Pattern, pNode);
  744. if (NULL != fmt && Idx < PAGE - 1)
  745. {
  746. va_list marker = NULL;
  747. va_start(marker, fmt);
  748. //size_t nLength = _vscprintf(fmt, marker) + 1;
  749. int copiedcount = vsnprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, fmt, marker);
  750. if (copiedcount > 0)
  751. {
  752. Idx += copiedcount;
  753. //add ResDataObject
  754. if (Idx < PAGE - 1)
  755. {
  756. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "%s", obj.encode());
  757. if (copiedcount > 0)
  758. {
  759. Idx += copiedcount;
  760. }
  761. else
  762. {
  763. Idx = PAGE - 1;
  764. }
  765. }
  766. }
  767. else
  768. {
  769. Idx = PAGE - 1;
  770. }
  771. //save to the file ,Len = Idx,buff = pNode,FileName = pFilepath
  772. SaveTheLog(Pattern, plog, &(pNode->m_Buff[0]), Idx, Level);
  773. va_end(marker);
  774. }
  775. g_BuffManager.ReleaseBuff(pNode);
  776. }
  777. }
  778. }
  779. plog->Thread_UnLock();
  780. }
  781. LOGGER_C_API void __Res_T_DebugPrintA(
  782. ResDataObject &obj,
  783. int Level,
  784. const char* file,
  785. int line,
  786. const char* function,
  787. const char* fmt,
  788. ...)
  789. {
  790. Logger *plog = GetThreadLogger();
  791. Logger_Pattern Pattern;
  792. if (plog == NULL)
  793. {
  794. return;
  795. }
  796. if (plog->GetLogFileId() == 0)
  797. {
  798. return;
  799. }
  800. plog->Thread_Lock();
  801. (*plog).GetLogPattern(Pattern);
  802. if (Pattern.m_Level == LOG_LEVEL_INIT)
  803. {
  804. UpdateLogger((*plog));
  805. (*plog).GetLogPattern(Pattern);
  806. }
  807. if ((Pattern.m_Level > LOG_LEVEL_INIT) && (Pattern.m_Level <= Level))
  808. {
  809. //good to go
  810. if ((*plog).IsLogFilePathExist())
  811. {
  812. //the whole pattern like below line
  813. /*
  814. //full log line
  815. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  816. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  817. //minimum log line
  818. [14:03:35:0231] [DEBUG]: context......
  819. */
  820. //head format
  821. BuffNode *pNode = g_BuffManager.GetBuff();
  822. if (pNode)
  823. {
  824. size_t Idx = GetHeadFormat((LOG_LEVEL)Level, file, line, function, Pattern, pNode);
  825. if (NULL != fmt && Idx < PAGE - 1)
  826. {
  827. va_list marker = NULL;
  828. va_start(marker, fmt);
  829. //size_t nLength = _vscprintf(fmt, marker) + 1;
  830. int copiedcount = vsnprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, fmt, marker);
  831. if (copiedcount > 0)
  832. {
  833. Idx += copiedcount;
  834. //add ResDataObject
  835. if (Idx < PAGE - 1)
  836. {
  837. copiedcount = _snprintf_s(&(pNode->m_Buff[Idx]), pNode->m_Buff.size() - Idx, _TRUNCATE, "%s", obj.encode());
  838. if (copiedcount > 0)
  839. {
  840. Idx += copiedcount;
  841. }
  842. else
  843. {
  844. Idx = PAGE - 1;
  845. }
  846. }
  847. }
  848. else
  849. {
  850. Idx = PAGE - 1;
  851. }
  852. //save to the file ,Len = Idx,buff = pNode,FileName = pFilepath
  853. SaveTheLog(Pattern, plog, &(pNode->m_Buff[0]), Idx, Level);
  854. va_end(marker);
  855. }
  856. g_BuffManager.ReleaseBuff(pNode);
  857. }
  858. }
  859. }
  860. plog->Thread_UnLock();
  861. }