//--------------------------------------------------------------------------- #include #include #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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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; } //---------------------------------------------------------------------------