//--------------------------------------------------------------------------- #pragma hdrstop #include "LogData.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- int LogIndex; TList *LogList; //--------------------------------------------------------------------------- // ·Î±× »ý¼º bool __fastcall CreateLog() { LogList = new TList; InitLog(); return LogList != NULL; } //--------------------------------------------------------------------------- // ·Î±× ÃʱâÈ­ void __fastcall InitLog() { TLogData *LogData = NULL; LogIndex = 0; if (LogList) { for (int i = 0; i < LogList->Count; i++) { LogData = (TLogData *)LogList->Items[i]; if (LogData) delete LogData; } LogList->Clear(); } } //--------------------------------------------------------------------------- // ·Î±× ±â·ÏÀÇ ½ÃÀÛ TLogData * __fastcall BeginLog(String strParam, String strFileName , String strFunctionName, int nLine) { TLogData *LogData = new TLogData; LogData->nIndex = LogIndex; LogData->dtCurrent = TDateTime::CurrentDateTime(); if (strParam.IsEmpty() == false) { LogData->strParameter = "Param:" + strParam + ", "; } else { LogData->strParameter = " "; } LogData->strFileName = strFileName + ", "; LogData->strFunctionName = strFunctionName + ", "; LogData->nLine = nLine; LogList->Add(LogData); LogIndex++; return LogData; } //--------------------------------------------------------------------------- // ·Î±× ±â·ÏÀÇ ³¡ (ÇÔ¼ö°¡ ¹®Á¦ ¾øÀÌ Á¾·áµÇ¸é ·Î±× ±â·ÏÀ» »èÁ¦ÇÑ´Ù) void __fastcall EndLog(TLogData *LogData) { TLogData *TempLogData = NULL; if (LogData && LogList) { // ÃÖ±Ù¿¡ È£ÃâµÈ ³»¿ëÀÌ °¡Àå ¸¶Áö¸·¿¡ ÀÖÀ¸¹Ç·Î µÚ¿¡¼­ºÎÅÍ °Ë»öÇÏ´Â °ÍÀÌ ºü¸£´Ù for (int i = LogList->Count-1; 0 <= i; i--) { TempLogData = (TLogData *)LogList->Items[i]; if (TempLogData && TempLogData->nIndex == LogData->nIndex) { delete TempLogData; TempLogData = NULL; LogList->Delete(i); break; } } } } //--------------------------------------------------------------------------- // ·Î±× ÀúÀå bool __fastcall ExceptionLog(HANDLE hFile) { String strFile, strDateTime, strLine, strData; TLogData *LogData = NULL; DWORD dwWrite; if (hFile) { strData = "LOG :\r\n"; if (!WriteFile(hFile, strData.c_str(), sizeof(Char)*strData.Length(), &dwWrite, NULL)) return false; if (LogList) { for (int i = 0; i < LogList->Count; i++) { LogData = (TLogData *)LogList->Items[i]; strDateTime = LogData->dtCurrent.FormatString("yyyy-mm-dd hh:nn:ss") + ","; strLine = IntToStr(LogData->nLine) + " Line\r\n"; strData = " [" + IntToStr(LogData->nIndex) + "] " + strDateTime + LogData->strParameter; strData += LogData->strFileName + LogData->strFunctionName + strLine; if (!WriteFile(hFile, strData.c_str(), sizeof(Char)*strData.Length(), &dwWrite, NULL)) return false; } } } // ´ÙÀ½ Exception¿¡ ±â·ÏµÇÁö ¾Êµµ·Ï ÃʱâÈ­ÇÑ´Ù InitLog(); return true; } //--------------------------------------------------------------------------- // Á¾·á½Ã ·Î±× ÀúÀå (BEGIN_LOG, END_LOG½Ö °ËÃâ ¿ë) bool __fastcall SaveLog(String strDir) { HANDLE hFile = NULL; DWORD dwWrite; Char cUnicodeFile = 0xFEFF; // ·Î±×°¡ ÀÖÀ» °æ¿ì¸¸ ÀúÀå if (0 < LogList->Count) { // ÇöÀç ³¯Â¥ ½Ã°£ÀÌ ÆÄÀϸíÀÌ µÊ if ((hFile = CreateFile(String(strDir+"\\"+Now().FormatString("yyyy-mm-dd hh-nn-ss")+".log").c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) goto fail; if (SetFilePointer(hFile, 0, 0, FILE_END) == 0xFFFFFFFF) goto fail; // ¸Ç µÚÂÊ¿¡ Ãß°¡ // UnicodeÀÏ °æ¿ì ÀúÀå - by monkman (2010.07.21) #ifdef UNICODE if (!WriteFile(hFile, &cUnicodeFile, sizeof(Char), &dwWrite, NULL)) goto fail; #endif ExceptionLog(hFile); if (hFile) CloseHandle(hFile); } hFile = NULL; return true; fail: if (hFile) CloseHandle(hFile); hFile = NULL; return false; } //--------------------------------------------------------------------------- // ·Î±× »èÁ¦ void __fastcall DeleteLog() { if (LogList) { InitLog(); delete LogList; } LogList = NULL; } //---------------------------------------------------------------------------