1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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´Â ¸ÖƼ¹ÙÀÌÆ® Ä³¸¯ÅͼÂÀ¸·Î µ¿ÀÛÇÑ´Ù.
    //À¯´ÏÄڵ带 ¸ÖƼ¹ÙÀÌÆ® Ä³¸¯ÅÍ ¼ÂÀ¸·Î º¯È¯ÇÑ´Ù.
    //::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);
}