#include "StdAfx.h"
|
#include "MemoryManager.h"
|
|
CMemoryManager::CMemoryManager(int MaxCount, int LogSize, int LogID, CString strLogPath, CString strLogName)
|
{
|
int i = 0;
|
m_LogSize = TLOGSIZE;
|
m_nLogID = LogID;
|
|
//»ý¼ºÀÚ¿¡¼ ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏ°í ½ºÅÿ¡ °è¼Ó ³Ö´Â´Ù.
|
//¿¹ 320Byte, 3000°³¸¦ ½ºÅÿ¡ ÀÔ·Â
|
for( i = 0; i < MaxCount; i++)
|
{
|
m_MemoryPool.push(new char[m_LogSize]);
|
}
|
|
//Å©¸®Æ¼Äà ¼½¼Ç °´Ã¼ ÃʱâÈ
|
InitializeCriticalSection(&m_csLogFile);
|
InitializeCriticalSection(&m_csMemory);
|
|
//½ÇÇàÆÄÀÏ À§Ä¡¸¦ ¾Ë¾Æ¿Â´Ù.
|
m_strPathName = strLogPath;
|
m_strLogName = strLogName;
|
//GetModulePath();
|
}
|
|
//exeÆÄÀϸíÀ» Á¦¿ÜÇÑ Æú´õ¸íÀ» ¾Ë¾Æ¿Â´Ù.
|
void CMemoryManager::GetModulePath(void)
|
{
|
TCHAR szBuffer[MAX_PATH];
|
memset(szBuffer,0x00,MAX_PATH*sizeof(TCHAR));
|
|
::GetModuleFileName(NULL,szBuffer,MAX_PATH);
|
|
for(int i = lstrlen(szBuffer) -1; i>=0; --i)
|
{
|
if(szBuffer[i] == '\\')
|
{
|
int j = lstrlen(szBuffer)-1;
|
for(;j>=i;--j)
|
{
|
szBuffer[j] = NULL;
|
}
|
|
if(szBuffer[j] == ';') szBuffer[j+1] = '\\';
|
|
szBuffer[j+1] = '\\';
|
|
m_strPathName = szBuffer;
|
return;
|
}
|
}
|
}
|
|
|
CMemoryManager::~CMemoryManager(void)
|
{
|
char * pLog = NULL;
|
//¸Þ¸ð¸® Pool »èÁ¦
|
while(!m_MemoryPool.empty())
|
{
|
pLog = m_MemoryPool.top();
|
m_MemoryPool.pop();
|
if ( pLog != NULL)
|
delete pLog;
|
pLog = NULL;
|
|
}
|
|
//·Î±× Å¥ »èÁ¦
|
while(!m_LogQueue.empty())
|
{
|
pLog = m_LogQueue.front();
|
m_LogQueue.pop();
|
if ( pLog != NULL)
|
delete pLog;
|
pLog = NULL;
|
}
|
|
//Å©¸®Æ¼Äà ¼½¼Ç °´Ã¼ »èÁ¦
|
DeleteCriticalSection(&m_csLogFile);
|
DeleteCriticalSection(&m_csMemory);
|
}
|
|
//¸Þ¸ð¸® ¸Ê¿¡¼ ·Î±×¸¦ Àоî¿À¸é ·Î±× Å¥¿¡ ÀúÀåÇÑ´Ù.
|
void CMemoryManager::PushLog(TCHAR* str, unsigned int* curIndex,
|
unsigned int* curLevel,unsigned int* curProcess, PSYSTEMTIME curTime)
|
{
|
|
EnterCriticalSection(&m_csMemory);
|
//¸Þ¸ð¸® Pool¿¡¼ »ç¿ëÇÒ ¸Þ¸ð¸® Çϳª¸¦ ²¨³»¿Â´Ù.
|
char* pLog = m_MemoryPool.top();
|
m_MemoryPool.pop();
|
|
//²¨³»¿Â ¸Þ¸ð¸®¸¦ ÃʱâÈ ÇÑ´Ù.
|
memset(pLog, 0x00, TLOGSIZE);
|
|
CString strlog;
|
|
strlog.Format(_T("%08d\t[%02d-%02d/%02d:%02d:%02d.%03d]\t[%02d:L%02d]%s\t\r\n"),
|
*curIndex, curTime->wMonth,curTime->wDay,curTime->wHour,
|
curTime->wMinute,curTime->wSecond,curTime->wMilliseconds,*curProcess,*curLevel,str);
|
|
USES_CONVERSION;
|
|
sprintf_s(pLog,TLOGSIZE,W2A(strlog));
|
//²¨³»¿Â ¸Þ¸ð¸®¿¡ ±Ô°Ý¿¡ ¸ÂÃç ·Î±×¸¦ »ý¼ºÇÑ´Ù.
|
// sprintf_s(pLog, TLOGSIZE,_T("%08d\t[%02d-%02d/%02d:%02d:%02d.%03d]\t[%02d:L%02d]%s\t\r\n"),
|
// *curIndex, curTime->wMonth,curTime->wDay,curTime->wHour,
|
// curTime->wMinute,curTime->wSecond,curTime->wMilliseconds,*curProcess,*curLevel,str);
|
|
//µð¹ö±× View´Â ¸ÖƼ¹ÙÀÌÆ® ij¸¯ÅͼÂÀ¸·Î µ¿ÀÛÇÑ´Ù.
|
//À¯´ÏÄڵ带 ¸ÖƼ¹ÙÀÌÆ® ij¸¯ÅÍ ¼ÂÀ¸·Î º¯È¯ÇÑ´Ù.
|
//::WideCharToMultiByte(CP_ACP, 0, m_TLog, -1, pLog, TLOGSIZE, NULL, NULL);
|
|
//»ý¼ºÇÑ ·Î±×¸¦ ·Î±×Å¥¿¡ ÀÔ·ÂÇÑ´Ù.
|
m_LogQueue.push(pLog);
|
|
LeaveCriticalSection(&m_csMemory);
|
}
|
|
|
void CMemoryManager::SaveLog()
|
{
|
//·Î±×Å¥¿¡ ÀúÀåµÈ ³»¿ëÀÌ ¾øÀ¸¸é ¸®ÅÏ
|
if(m_LogQueue.empty()) return;
|
|
SYSTEMTIME st;
|
GetLocalTime(&st);
|
|
//ÇÁ·Î¼¼½º ¾ÆÀ̵ð¿Í ³¯Â¥ÀÇ Á¶ÇÕÀ¸·Î ·Î±×ÆÄÀÏ »ý¼º
|
static TCHAR strFileName[MAX_PATH];
|
memset(strFileName, 0x00, MAX_PATH*sizeof(TCHAR));
|
_stprintf_s(strFileName, _T("%s\\%s%02d_%d%02d%02d.log"), m_strPathName, m_strLogName, m_nLogID, st.wYear, st.wMonth, st.wDay);
|
|
DWORD dwWritten;
|
HANDLE hFile;
|
|
EnterCriticalSection(&m_csLogFile);
|
hFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
int nFilePointer = SetFilePointer(hFile, 0, NULL, FILE_END);
|
|
EnterCriticalSection(&m_csMemory);
|
char* ptrLog;
|
|
//·Î±×Å¥°¡ ºô¶§±îÁö ¸ðµÎ ·Î±×ÆÄÀÏ¿¡ ¾´´Ù.
|
while(!m_LogQueue.empty())
|
{
|
//·Î±×Å¥¿¡¼ Çϳª ²¨³»¿Â´Ù.
|
ptrLog = m_LogQueue.front();
|
|
//ÆÄÀÏÀ» ÀÛ¼ºÇÑ´Ù.
|
WriteFile(hFile, ptrLog, strlen(ptrLog), &dwWritten, NULL);
|
|
//ÀÛ¼ºÇÑ ·Î±×´Â Á¦°ÅÇÑ´Ù.
|
m_LogQueue.pop();
|
|
//»ç¿ëÇÑ ¸Þ¸ð¸®´Â ´Ù½Ã ¸Þ¸ð¸® Ç®¿¡ ³Ö´Â´Ù.
|
m_MemoryPool.push(ptrLog);
|
}
|
|
LeaveCriticalSection(&m_csMemory);
|
CloseHandle(hFile);
|
LeaveCriticalSection(&m_csLogFile);
|
}
|