ITSLogF.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <windows.h>
  4. #include "FrmSqlErrorF.h"
  5. #pragma hdrstop
  6. //---------------------------------------------------------------------------
  7. // Important note about DLL memory management when your DLL uses the
  8. // static version of the RunTime Library:
  9. //
  10. // If your DLL exports any functions that pass String objects (or structs/
  11. // classes containing nested Strings) as parameter or function results,
  12. // you will need to add the library MEMMGR.LIB to both the DLL project and
  13. // any other projects that use the DLL. You will also need to use MEMMGR.LIB
  14. // if any other projects which use the DLL will be performing new or delete
  15. // operations on any non-TObject-derived classes which are exported from the
  16. // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
  17. // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
  18. // the file BORLNDMM.DLL should be deployed along with your DLL.
  19. //
  20. // To avoid using BORLNDMM.DLL, pass string information using "char *" or
  21. // ShortString parameters.
  22. //
  23. // If your DLL uses the dynamic version of the RTL, you do not need to
  24. // explicitly add MEMMGR.LIB as this will be done implicitly for you
  25. //---------------------------------------------------------------------------
  26. #define __ITSLog_DLL_EXPORT__
  27. #include "ITSLogF.h"
  28. #include <tchar.h>
  29. #include <stdio.h>
  30. #include <io.h>
  31. #include <stdarg.h>
  32. #include <mmsystem.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <sys/stat.h>
  39. #include <time.h>
  40. #include <sys/timeb.h>
  41. #include <winsock.h>
  42. #include <stdio.h>
  43. #include <io.h>
  44. #include <memory.h>
  45. #include <fcntl.h>
  46. #include <errno.h>
  47. #include <stdarg.h>
  48. #include <time.h>
  49. #pragma argsused
  50. #define SAFE_DELETE(p) {if (p != NULL) { delete p; p = NULL; }}
  51. #define MAINHANDLE Application->MainForm->Handle
  52. //#pragma data_seg (".ITSLog_seg")
  53. TITSLog *ITSLog = NULL;
  54. LOG_INFO g_LogCfg;
  55. TCriticalSection *csLog = NULL;
  56. //#pragma data_seg()
  57. int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
  58. {
  59. return 1;
  60. }
  61. //---------------------------------------------------------------------------
  62. /*
  63. * ITSLog.dll Initialize
  64. * arguments
  65. * return
  66. * None
  67. */
  68. TITSLog* __stdcall ITSLog_Initialize(String ALogDir, String ALogName, String ALogDay)
  69. {
  70. ITSLog = new TITSLog(ALogDir, ALogName, ALogDay);
  71. return ITSLog;
  72. }
  73. //---------------------------------------------------------------------------
  74. ITSLog_LIB int __stdcall ITSLog_SetConfig(LOG_INFO &ALogCfg)
  75. {
  76. g_LogCfg = ALogCfg;
  77. if (ITSLog)
  78. {
  79. ITSLog->FLogCfg = ALogCfg;
  80. }
  81. return 0;
  82. }
  83. //---------------------------------------------------------------------------
  84. /*
  85. * ITSLog.dll Term
  86. * arguments
  87. * return
  88. * None
  89. */
  90. int __stdcall ITSLog_Term()
  91. {
  92. SAFE_DELETE(ITSLog);
  93. return 0;
  94. }
  95. //---------------------------------------------------------------------------
  96. TITSLog::TITSLog(String ALogDir, String ALogName, String ALogDay, bool ABackup/*=false*/)
  97. {
  98. if (!csLog) csLog = new TCriticalSection();
  99. InitializeCriticalSection(&CS);
  100. FLogDay = ALogDay;
  101. FErrLogDay = ALogDay;
  102. FLogFile = ALogDir + ALogName;
  103. ForceDirectories(ALogDir.c_str());
  104. String sSrcLogFile;;
  105. String sBakLogFile;
  106. String sLogDay = Now().FormatString("dd");
  107. if (ABackup)
  108. {
  109. try
  110. {
  111. sSrcLogFile = FLogFile + "_" + sLogDay + ".log";
  112. sBakLogFile = sSrcLogFile + ".bak";
  113. CopyFile(sSrcLogFile.c_str(), sBakLogFile.c_str(), true);
  114. sSrcLogFile = FLogFile + "_" + sLogDay + ".err";
  115. sBakLogFile = sSrcLogFile + ".bak";
  116. CopyFile(sSrcLogFile.c_str(), sBakLogFile.c_str(), true);
  117. }
  118. catch(Exception &e)
  119. {
  120. }
  121. }
  122. }
  123. //---------------------------------------------------------------------------
  124. TITSLog::~TITSLog()
  125. {
  126. if (csLog)
  127. {
  128. delete csLog;
  129. csLog = NULL;
  130. }
  131. }
  132. //---------------------------------------------------------------------------
  133. void TITSLog::SendLogMessage(int AKind, char *AFmt, ...)
  134. {
  135. va_list ap;
  136. int res;
  137. LRESULT result;
  138. LOG_MESSAGE *pMsg = NULL;
  139. bool bLog = false;
  140. switch(AKind)
  141. {
  142. case eLOG_INFO: bLog = FLogCfg.Info; break;
  143. case eLOG_DATA: bLog = FLogCfg.Data; break;
  144. case eLOG_ERROR: bLog = FLogCfg.Error; break;
  145. case eLOG_WARNING: bLog = FLogCfg.Warning; break;
  146. case eLOG_DEBUG: bLog = FLogCfg.Debug; break;
  147. case eLOG_DETAIL: bLog = FLogCfg.Detail; break;
  148. }
  149. if (!bLog) return;
  150. try
  151. {
  152. csLog->Enter();
  153. try
  154. {
  155. pMsg = new LOG_MESSAGE;
  156. memset((char*)pMsg, 0x00, sizeof(LOG_MESSAGE));
  157. va_start(ap, AFmt);
  158. res = vsprintf(pMsg->Data, AFmt, ap);
  159. va_end(ap);
  160. //LogWrite(NULL, AKind, pMsg->Data);
  161. pMsg->Kind = AKind;
  162. pMsg->Len = strlen(pMsg->Data);
  163. #if 0
  164. if (TITSLog::FLog.Debug)
  165. {
  166. ITSUtil_Trace(pMsg->Data);
  167. }
  168. #endif
  169. //result = ::PostMessage(MAINHANDLE, WM_LOG, (WPARAM)pMsg, 0);
  170. }
  171. catch(Exception &e)
  172. {
  173. SAFE_DELETE(pMsg);
  174. }
  175. }
  176. __finally
  177. {
  178. csLog->Leave();
  179. }
  180. }
  181. //---------------------------------------------------------------------------
  182. FILE *TITSLog::OpenLogFile(AnsiString AFileExt/*=".log"*/)
  183. {
  184. FILE *fp = NULL;
  185. AnsiString sOpenMode = "a+";
  186. try
  187. {
  188. AnsiString sLogDay = Now().FormatString("dd");
  189. if (AFileExt == ".err")
  190. {
  191. if (FErrLogDay != sLogDay)
  192. {
  193. sOpenMode = "w+";
  194. FErrLogDay = sLogDay;
  195. }
  196. AnsiString sLogFile = FLogFile + "_" + FErrLogDay + AFileExt;
  197. fp = fopen(sLogFile.c_str(), sOpenMode.c_str());
  198. }
  199. else
  200. {
  201. if (FLogDay != sLogDay)
  202. {
  203. sOpenMode = "w+";
  204. FLogDay = sLogDay;
  205. }
  206. AnsiString sLogFile = FLogFile + "_" + FLogDay + AFileExt;
  207. fp = fopen(sLogFile.c_str(), sOpenMode.c_str());
  208. }
  209. }
  210. catch(Exception &e)
  211. {
  212. }
  213. return fp;
  214. }
  215. //---------------------------------------------------------------------------
  216. int TITSLog::LogData(char *ASndRcv, const unsigned char *AData, int ALen, FILE *Afp/*=NULL*/)
  217. {
  218. AnsiString sTime;
  219. int ii, jj, pos;
  220. FILE *fp = Afp;
  221. bool bFileClose = false;
  222. if (!FLogCfg.Data || !AData)
  223. {
  224. return -1;
  225. }
  226. try
  227. {
  228. EnterCriticalSection(&CS);
  229. if (!fp)
  230. {
  231. fp = OpenLogFile();
  232. bFileClose = true;
  233. }
  234. if (!fp) return -1;
  235. try
  236. {
  237. //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
  238. sTime = AnsiString(Now().FormatString("hh:nn:ss"));
  239. fprintf(fp, "[%s] [DATA ] %d Bytes [%s]\n", sTime.c_str(), ALen, ASndRcv);
  240. AnsiString sLog;
  241. AnsiString sChar;
  242. AnsiString sMsg;
  243. for (ii = 0; ii < ALen; ii += 16)
  244. {
  245. sLog = " ";
  246. sMsg.printf("%08Xh ", ii);
  247. sLog += sMsg;
  248. for (jj = 0; jj < 16; jj++)
  249. {
  250. pos = ii + jj;
  251. if (pos < ALen)
  252. sMsg.printf(" %02X", AData[pos]);
  253. else
  254. sMsg.printf(" ");
  255. sLog += sMsg;
  256. if (jj == 7)
  257. {
  258. sMsg.printf(" "); sLog += sMsg;
  259. }
  260. }
  261. sMsg.printf(" ;"); sLog += sMsg;
  262. for (jj = 0; jj < 16; jj++)
  263. {
  264. pos = ii + jj;
  265. if (pos >= ALen)
  266. break;
  267. uint8_t v = AData[pos];
  268. sMsg.printf("%c", isprint(v) ? v : '.');
  269. sLog += sMsg;
  270. if (jj == 7)
  271. {
  272. sMsg.printf(" "); sLog += sMsg;
  273. }
  274. }
  275. fprintf(fp, "%s\n", sLog.c_str());
  276. fflush(fp);
  277. }
  278. //fprintf(fp, "\n");
  279. }
  280. catch(Exception &e)
  281. {
  282. }
  283. }
  284. __finally
  285. {
  286. if (bFileClose)
  287. {
  288. // 아규먼트로 파일디스크립터가 넘어온 경우 파일을 닫지 않는다.
  289. if (fp) fclose(fp);
  290. }
  291. LeaveCriticalSection(&CS);
  292. }
  293. return ALen;
  294. }
  295. //---------------------------------------------------------------------------
  296. int TITSLog::LogWrite(FILE *Afp, int ALogKind, char *AFmt, ...)
  297. {
  298. va_list ap;
  299. int cnt = 0;
  300. char szFmtData[MAX_LOG_BUFFER+1];
  301. AnsiString sLogKind = "";
  302. AnsiString sTime;
  303. FILE *fp = Afp;
  304. FILE *fpErr = NULL;
  305. bool bFileClose = false;
  306. bool bLog = false;
  307. switch(ALogKind)
  308. {
  309. case eLOG_INFO : bLog = FLogCfg.Info; sLogKind = "[INFO ]"; break;
  310. case eLOG_DATA : bLog = FLogCfg.Data; sLogKind = "[DATA ]"; break;
  311. case eLOG_ERROR : bLog = FLogCfg.Error; sLogKind = "[ERROR]"; break;
  312. case eLOG_WARNING: bLog = FLogCfg.Warning; sLogKind = "[WARN ]"; break;
  313. case eLOG_DEBUG : bLog = FLogCfg.Debug; sLogKind = "[DEGUG]"; break;
  314. case eLOG_DETAIL : bLog = FLogCfg.Detail; sLogKind = "[DETAL]"; break;
  315. }
  316. if (!bLog) return -1;
  317. try
  318. {
  319. EnterCriticalSection(&CS);
  320. if (!fp)
  321. {
  322. if (ALogKind == eLOG_ERROR)
  323. fpErr = OpenLogFile(".err");
  324. fp = OpenLogFile();
  325. bFileClose = true;
  326. }
  327. if (!fp) return -1;
  328. try
  329. {
  330. va_start(ap, AFmt);
  331. cnt = vsprintf(szFmtData, AFmt, ap);
  332. va_end(ap);
  333. //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
  334. sTime = AnsiString(Now().FormatString("hh:nn:ss"));
  335. fprintf(fp, "[%s] %s %s\n", sTime.c_str(), sLogKind.c_str(), szFmtData);
  336. if (fpErr) fprintf(fpErr, "[%s] %s %s\n", sTime.c_str(), sLogKind.c_str(), szFmtData);
  337. }
  338. catch(Exception &e)
  339. {
  340. }
  341. }
  342. __finally
  343. {
  344. if (bFileClose)
  345. {
  346. // 아규먼트로 파일디스크립터가 넘어온 경우 파일을 닫지 않는다.
  347. if (fp) fclose(fp);
  348. }
  349. if (fpErr) fclose(fpErr);
  350. LeaveCriticalSection(&CS);
  351. }
  352. return cnt;
  353. }
  354. //---------------------------------------------------------------------------
  355. int TITSLog::LogDbError(String ATitle, String AClass, String AError, String ASql,
  356. String AFile, String AFunc, int ALine, bool AShowMsg/*=true*/)
  357. {
  358. AnsiString sTime;
  359. FILE *fp;
  360. LogWrite(NULL, eLOG_ERROR, "[%s] DB Error Occured. FILE: %s LINE: %d FUNC: %s", AnsiString(ExtractFileName(AFile)).c_str(), ALine, AnsiString(AFunc).c_str());
  361. fp = OpenLogFile(".err");
  362. if (!fp) return -1;
  363. try
  364. {
  365. EnterCriticalSection(&CS);
  366. try
  367. {
  368. //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
  369. sTime = AnsiString(Now().FormatString("hh:nn:ss"));
  370. fprintf(fp, "[%s] DB Error Occured. FILE: %s LINE: %d FUNC: %s\n",
  371. sTime.c_str(), AnsiString(ExtractFileName(AFile)).c_str(), ALine, AnsiString(AFunc).c_str());
  372. fprintf(fp, "Title: %s\n", AnsiString(ATitle).c_str());
  373. fprintf(fp, "Class: %s\n", AnsiString(AClass).c_str());
  374. fprintf(fp, "Error: %s\n", AnsiString(AError).c_str());
  375. fprintf(fp, "SQL :\n%s\n",AnsiString(ASql).c_str());
  376. fprintf(fp, "\n");
  377. }
  378. catch(Exception &e)
  379. {
  380. }
  381. }
  382. __finally
  383. {
  384. if (fp) fclose(fp);
  385. LeaveCriticalSection(&CS);
  386. }
  387. if (AShowMsg)
  388. {
  389. #if 0
  390. String sErrMsg = ATitle +"\r\n" + AClass + "\r\n" + AError;
  391. Application->NormalizeTopMosts();
  392. Application->MessageBox(sErrMsg.c_str(), ATitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  393. Application->RestoreTopMosts();
  394. #else
  395. TFrmSqlError *FrmSqlError = new TFrmSqlError(Application);
  396. FrmSqlError->Caption = ATitle;
  397. FrmSqlError->Edit1->Text = ATitle;
  398. FrmSqlError->Edit2->Text = AClass;
  399. FrmSqlError->Edit3->Text = AError;
  400. FrmSqlError->reMsg->Lines->Add(ASql);
  401. FrmSqlError->ShowModal();
  402. delete FrmSqlError;
  403. FrmSqlError = NULL;
  404. #endif
  405. }
  406. return 0;
  407. }
  408. //---------------------------------------------------------------------------
  409. int TITSLog::LogDbInfo(String ATitle, String ASql)
  410. {
  411. AnsiString sTime;
  412. FILE *fp;
  413. fp = OpenLogFile(".log");
  414. if (!fp) return -1;
  415. try
  416. {
  417. EnterCriticalSection(&CS);
  418. try
  419. {
  420. //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
  421. sTime = AnsiString(Now().FormatString("hh:nn:ss"));
  422. fprintf(fp, "[%s] %s\n", sTime.c_str(), AnsiString(ATitle).c_str());
  423. fprintf(fp, "SQL :\n%s\n",AnsiString(ASql).c_str());
  424. fprintf(fp, "\n");
  425. }
  426. catch(Exception &e)
  427. {
  428. }
  429. }
  430. __finally
  431. {
  432. if (fp) fclose(fp);
  433. LeaveCriticalSection(&CS);
  434. }
  435. return 0;
  436. }
  437. //---------------------------------------------------------------------------