123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <windows.h>
- #include "FrmSqlErrorF.h"
- #pragma hdrstop
- //---------------------------------------------------------------------------
- // Important note about DLL memory management when your DLL uses the
- // static version of the RunTime Library:
- //
- // If your DLL exports any functions that pass String objects (or structs/
- // classes containing nested Strings) as parameter or function results,
- // you will need to add the library MEMMGR.LIB to both the DLL project and
- // any other projects that use the DLL. You will also need to use MEMMGR.LIB
- // if any other projects which use the DLL will be performing new or delete
- // operations on any non-TObject-derived classes which are exported from the
- // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
- // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
- // the file BORLNDMM.DLL should be deployed along with your DLL.
- //
- // To avoid using BORLNDMM.DLL, pass string information using "char *" or
- // ShortString parameters.
- //
- // If your DLL uses the dynamic version of the RTL, you do not need to
- // explicitly add MEMMGR.LIB as this will be done implicitly for you
- //---------------------------------------------------------------------------
- #define __ITSLog_DLL_EXPORT__
- #include "ITSLogF.h"
- #include <tchar.h>
- #include <stdio.h>
- #include <io.h>
- #include <stdarg.h>
- #include <mmsystem.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <sys/timeb.h>
- #include <winsock.h>
- #include <stdio.h>
- #include <io.h>
- #include <memory.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <time.h>
- #pragma argsused
- #define SAFE_DELETE(p) {if (p != NULL) { delete p; p = NULL; }}
- #define MAINHANDLE Application->MainForm->Handle
- //#pragma data_seg (".ITSLog_seg")
- TITSLog *ITSLog = NULL;
- LOG_INFO g_LogCfg;
- TCriticalSection *csLog = NULL;
- //#pragma data_seg()
- int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
- {
- return 1;
- }
- //---------------------------------------------------------------------------
- /*
- * ITSLog.dll Initialize
- * arguments
- * return
- * None
- */
- TITSLog* __stdcall ITSLog_Initialize(String ALogDir, String ALogName, String ALogDay)
- {
- ITSLog = new TITSLog(ALogDir, ALogName, ALogDay);
- return ITSLog;
- }
- //---------------------------------------------------------------------------
- ITSLog_LIB int __stdcall ITSLog_SetConfig(LOG_INFO &ALogCfg)
- {
- g_LogCfg = ALogCfg;
- if (ITSLog)
- {
- ITSLog->FLogCfg = ALogCfg;
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- /*
- * ITSLog.dll Term
- * arguments
- * return
- * None
- */
- int __stdcall ITSLog_Term()
- {
- SAFE_DELETE(ITSLog);
- return 0;
- }
- //---------------------------------------------------------------------------
- TITSLog::TITSLog(String ALogDir, String ALogName, String ALogDay, bool ABackup/*=false*/)
- {
- if (!csLog) csLog = new TCriticalSection();
- InitializeCriticalSection(&CS);
- FLogDay = ALogDay;
- FErrLogDay = ALogDay;
- FLogFile = ALogDir + ALogName;
- ForceDirectories(ALogDir.c_str());
- String sSrcLogFile;;
- String sBakLogFile;
- String sLogDay = Now().FormatString("dd");
- if (ABackup)
- {
- try
- {
- sSrcLogFile = FLogFile + "_" + sLogDay + ".log";
- sBakLogFile = sSrcLogFile + ".bak";
- CopyFile(sSrcLogFile.c_str(), sBakLogFile.c_str(), true);
- sSrcLogFile = FLogFile + "_" + sLogDay + ".err";
- sBakLogFile = sSrcLogFile + ".bak";
- CopyFile(sSrcLogFile.c_str(), sBakLogFile.c_str(), true);
- }
- catch(Exception &e)
- {
- }
- }
- }
- //---------------------------------------------------------------------------
- TITSLog::~TITSLog()
- {
- if (csLog)
- {
- delete csLog;
- csLog = NULL;
- }
- }
- //---------------------------------------------------------------------------
- void TITSLog::SendLogMessage(int AKind, char *AFmt, ...)
- {
- va_list ap;
- int res;
- LRESULT result;
- LOG_MESSAGE *pMsg = NULL;
- bool bLog = false;
- switch(AKind)
- {
- case eLOG_INFO: bLog = FLogCfg.Info; break;
- case eLOG_DATA: bLog = FLogCfg.Data; break;
- case eLOG_ERROR: bLog = FLogCfg.Error; break;
- case eLOG_WARNING: bLog = FLogCfg.Warning; break;
- case eLOG_DEBUG: bLog = FLogCfg.Debug; break;
- case eLOG_DETAIL: bLog = FLogCfg.Detail; break;
- }
- if (!bLog) return;
- try
- {
- csLog->Enter();
- try
- {
- pMsg = new LOG_MESSAGE;
- memset((char*)pMsg, 0x00, sizeof(LOG_MESSAGE));
- va_start(ap, AFmt);
- res = vsprintf(pMsg->Data, AFmt, ap);
- va_end(ap);
- //LogWrite(NULL, AKind, pMsg->Data);
- pMsg->Kind = AKind;
- pMsg->Len = strlen(pMsg->Data);
- #if 0
- if (TITSLog::FLog.Debug)
- {
- ITSUtil_Trace(pMsg->Data);
- }
- #endif
- //result = ::PostMessage(MAINHANDLE, WM_LOG, (WPARAM)pMsg, 0);
- }
- catch(Exception &e)
- {
- SAFE_DELETE(pMsg);
- }
- }
- __finally
- {
- csLog->Leave();
- }
- }
- //---------------------------------------------------------------------------
- FILE *TITSLog::OpenLogFile(AnsiString AFileExt/*=".log"*/)
- {
- FILE *fp = NULL;
- AnsiString sOpenMode = "a+";
- try
- {
- AnsiString sLogDay = Now().FormatString("dd");
- if (AFileExt == ".err")
- {
- if (FErrLogDay != sLogDay)
- {
- sOpenMode = "w+";
- FErrLogDay = sLogDay;
- }
- AnsiString sLogFile = FLogFile + "_" + FErrLogDay + AFileExt;
- fp = fopen(sLogFile.c_str(), sOpenMode.c_str());
- }
- else
- {
- if (FLogDay != sLogDay)
- {
- sOpenMode = "w+";
- FLogDay = sLogDay;
- }
- AnsiString sLogFile = FLogFile + "_" + FLogDay + AFileExt;
- fp = fopen(sLogFile.c_str(), sOpenMode.c_str());
- }
- }
- catch(Exception &e)
- {
- }
- return fp;
- }
- //---------------------------------------------------------------------------
- int TITSLog::LogData(char *ASndRcv, const unsigned char *AData, int ALen, FILE *Afp/*=NULL*/)
- {
- AnsiString sTime;
- int ii, jj, pos;
- FILE *fp = Afp;
- bool bFileClose = false;
- if (!FLogCfg.Data || !AData)
- {
- return -1;
- }
- try
- {
- EnterCriticalSection(&CS);
- if (!fp)
- {
- fp = OpenLogFile();
- bFileClose = true;
- }
- if (!fp) return -1;
- try
- {
- //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
- sTime = AnsiString(Now().FormatString("hh:nn:ss"));
- fprintf(fp, "[%s] [DATA ] %d Bytes [%s]\n", sTime.c_str(), ALen, ASndRcv);
- AnsiString sLog;
- AnsiString sChar;
- AnsiString sMsg;
- for (ii = 0; ii < ALen; ii += 16)
- {
- sLog = " ";
- sMsg.printf("%08Xh ", ii);
- sLog += sMsg;
- for (jj = 0; jj < 16; jj++)
- {
- pos = ii + jj;
- if (pos < ALen)
- sMsg.printf(" %02X", AData[pos]);
- else
- sMsg.printf(" ");
- sLog += sMsg;
- if (jj == 7)
- {
- sMsg.printf(" "); sLog += sMsg;
- }
- }
- sMsg.printf(" ;"); sLog += sMsg;
- for (jj = 0; jj < 16; jj++)
- {
- pos = ii + jj;
- if (pos >= ALen)
- break;
- uint8_t v = AData[pos];
- sMsg.printf("%c", isprint(v) ? v : '.');
- sLog += sMsg;
- if (jj == 7)
- {
- sMsg.printf(" "); sLog += sMsg;
- }
- }
- fprintf(fp, "%s\n", sLog.c_str());
- fflush(fp);
- }
- //fprintf(fp, "\n");
- }
- catch(Exception &e)
- {
- }
- }
- __finally
- {
- if (bFileClose)
- {
- // 아규먼트로 파일디스크립터가 넘어온 경우 파일을 닫지 않는다.
- if (fp) fclose(fp);
- }
- LeaveCriticalSection(&CS);
- }
- return ALen;
- }
- //---------------------------------------------------------------------------
- int TITSLog::LogWrite(FILE *Afp, int ALogKind, char *AFmt, ...)
- {
- va_list ap;
- int cnt = 0;
- char szFmtData[MAX_LOG_BUFFER+1];
- AnsiString sLogKind = "";
- AnsiString sTime;
- FILE *fp = Afp;
- FILE *fpErr = NULL;
- bool bFileClose = false;
- bool bLog = false;
- switch(ALogKind)
- {
- case eLOG_INFO : bLog = FLogCfg.Info; sLogKind = "[INFO ]"; break;
- case eLOG_DATA : bLog = FLogCfg.Data; sLogKind = "[DATA ]"; break;
- case eLOG_ERROR : bLog = FLogCfg.Error; sLogKind = "[ERROR]"; break;
- case eLOG_WARNING: bLog = FLogCfg.Warning; sLogKind = "[WARN ]"; break;
- case eLOG_DEBUG : bLog = FLogCfg.Debug; sLogKind = "[DEGUG]"; break;
- case eLOG_DETAIL : bLog = FLogCfg.Detail; sLogKind = "[DETAL]"; break;
- }
- if (!bLog) return -1;
- try
- {
- EnterCriticalSection(&CS);
- if (!fp)
- {
- if (ALogKind == eLOG_ERROR)
- fpErr = OpenLogFile(".err");
- fp = OpenLogFile();
- bFileClose = true;
- }
- if (!fp) return -1;
- try
- {
- va_start(ap, AFmt);
- cnt = vsprintf(szFmtData, AFmt, ap);
- va_end(ap);
- //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
- sTime = AnsiString(Now().FormatString("hh:nn:ss"));
- fprintf(fp, "[%s] %s %s\n", sTime.c_str(), sLogKind.c_str(), szFmtData);
- if (fpErr) fprintf(fpErr, "[%s] %s %s\n", sTime.c_str(), sLogKind.c_str(), szFmtData);
- }
- catch(Exception &e)
- {
- }
- }
- __finally
- {
- if (bFileClose)
- {
- // 아규먼트로 파일디스크립터가 넘어온 경우 파일을 닫지 않는다.
- if (fp) fclose(fp);
- }
- if (fpErr) fclose(fpErr);
- LeaveCriticalSection(&CS);
- }
- return cnt;
- }
- //---------------------------------------------------------------------------
- int TITSLog::LogDbError(String ATitle, String AClass, String AError, String ASql,
- String AFile, String AFunc, int ALine, bool AShowMsg/*=true*/)
- {
- AnsiString sTime;
- FILE *fp;
- LogWrite(NULL, eLOG_ERROR, "[%s] DB Error Occured. FILE: %s LINE: %d FUNC: %s", AnsiString(ExtractFileName(AFile)).c_str(), ALine, AnsiString(AFunc).c_str());
- fp = OpenLogFile(".err");
- if (!fp) return -1;
- try
- {
- EnterCriticalSection(&CS);
- try
- {
- //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
- sTime = AnsiString(Now().FormatString("hh:nn:ss"));
- fprintf(fp, "[%s] DB Error Occured. FILE: %s LINE: %d FUNC: %s\n",
- sTime.c_str(), AnsiString(ExtractFileName(AFile)).c_str(), ALine, AnsiString(AFunc).c_str());
- fprintf(fp, "Title: %s\n", AnsiString(ATitle).c_str());
- fprintf(fp, "Class: %s\n", AnsiString(AClass).c_str());
- fprintf(fp, "Error: %s\n", AnsiString(AError).c_str());
- fprintf(fp, "SQL :\n%s\n",AnsiString(ASql).c_str());
- fprintf(fp, "\n");
- }
- catch(Exception &e)
- {
- }
- }
- __finally
- {
- if (fp) fclose(fp);
- LeaveCriticalSection(&CS);
- }
- if (AShowMsg)
- {
- #if 0
- String sErrMsg = ATitle +"\r\n" + AClass + "\r\n" + AError;
- Application->NormalizeTopMosts();
- Application->MessageBox(sErrMsg.c_str(), ATitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
- Application->RestoreTopMosts();
- #else
- TFrmSqlError *FrmSqlError = new TFrmSqlError(Application);
- FrmSqlError->Caption = ATitle;
- FrmSqlError->Edit1->Text = ATitle;
- FrmSqlError->Edit2->Text = AClass;
- FrmSqlError->Edit3->Text = AError;
- FrmSqlError->reMsg->Lines->Add(ASql);
- FrmSqlError->ShowModal();
- delete FrmSqlError;
- FrmSqlError = NULL;
- #endif
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- int TITSLog::LogDbInfo(String ATitle, String ASql)
- {
- AnsiString sTime;
- FILE *fp;
- fp = OpenLogFile(".log");
- if (!fp) return -1;
- try
- {
- EnterCriticalSection(&CS);
- try
- {
- //sTime = AnsiString(Now().FormatString("yyyy-mm-dd hh:nn:ss"));
- sTime = AnsiString(Now().FormatString("hh:nn:ss"));
- fprintf(fp, "[%s] %s\n", sTime.c_str(), AnsiString(ATitle).c_str());
- fprintf(fp, "SQL :\n%s\n",AnsiString(ASql).c_str());
- fprintf(fp, "\n");
- }
- catch(Exception &e)
- {
- }
- }
- __finally
- {
- if (fp) fclose(fp);
- LeaveCriticalSection(&CS);
- }
- return 0;
- }
- //---------------------------------------------------------------------------
|