SDC C-Project CF Review 프로그램
kojingeun
2023-06-07 b4a18bfef6cb9f5d48a12b455dfa78f9ef67e348
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "StdAfx.h"
#include "DisplayMessage.h"
 
CDisplayMessage::CDisplayMessage(const CString& strPath)
{
    m_pDM2P        = NULL;
    m_pFileLog    = NULL;
 
    SetPath(strPath);
 
    // #3417 LYWCF AOI Review 전설비 Log Process개선 ADD START
    DWORD  dwThreadId = 0;
    HANDLE hThread = CreateThread(NULL, 0, RunThreadDisplayLog, NULL, 0, &dwThreadId);
    m_ThreadId = dwThreadId;
    // #3417 LYWCF AOI Review 전설비 Log Process개선 ADD END
 
    InitializeCriticalSection(&m_csLog);
}
 
CDisplayMessage::~CDisplayMessage(void)
{
    if (m_pFileLog)
    {
        delete m_pFileLog;
        m_pFileLog = NULL;
    }
 
    PostThreadMessage(m_ThreadId, WM_QUIT, 0, 0);
 
    DeleteCriticalSection(&m_csLog);
}
 
void CDisplayMessage::SetPath(const CString& strPath)
{
    m_strLogPath = strPath;
    CString m_GlobalLogPath;
    m_strLogFile.Format(_T("D:\\DIT_LogData\\%04d%02d%02d"), m_strLogPath, m_TimeLogFile.GetYear(), m_TimeLogFile.GetMonth(), m_TimeLogFile.GetDay());
    CreateDirectory(m_strLogFile, NULL);
    CreateDirectory(m_strLogPath, NULL);
 
    MakeLogFile();
}
 
BOOL CDisplayMessage::MakeLogFile()
{
    // Log 처리
    CString m_GlobalLogPath;
    m_strLogFile.Format(_T("D:\\DIT_LogData\\%04d%02d%02d"), m_TimeLogFile.GetYear(), m_TimeLogFile.GetMonth(), m_TimeLogFile.GetDay());
    CreateDirectory(m_strLogFile, NULL);
 
    if (m_strLogPath.IsEmpty())
        return FALSE;
 
    m_TimeLogFile = CTime::GetCurrentTime();
    m_strLogFile.Format(_T("%s\\%04d-%02d-%02d.log"), m_strLogPath, m_TimeLogFile.GetYear(), m_TimeLogFile.GetMonth(), m_TimeLogFile.GetDay());
 
    if (m_pFileLog)
    {
        delete m_pFileLog;
        m_pFileLog = NULL;
    }
 
    m_pFileLog = new CFile();
 
    return TRUE;
}
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD START
void CDisplayMessage::DisplayMessage(const CString& strMessage)
{
//     WriteToFile(strMessage);
// 
//     if(m_pDM2P)
//     {
//         m_pDM2P->DM2P_DisplayMessage(strMessage);
//     }
    /*< SWK 20221226 - #4403 MOD Start >*/
//     CString* pstrLog = new CString(strMessage);
//     PostThreadMessage(m_ThreadId, WM_DIPLAY_LOG, reinterpret_cast<WPARAM>(pstrLog), 0);
    pLogData plogdata = new LogData;
    ::GetLocalTime(&plogdata->currentTime);
    plogdata->strLog = strMessage;
    PostThreadMessage(m_ThreadId, WM_DIPLAY_LOG, reinterpret_cast<WPARAM>(plogdata), 0);
    /*< SWK 20221226 - #4403 MOD End >*/
 
}
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD END
 
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD START
void CDisplayMessage::DisplayMessage(const TCHAR* lpstrFormat, ...)
{
    va_list list;
    TCHAR strText[2000] = {0};
    try
    {
        va_start(list, lpstrFormat);
        _vstprintf_s(strText, lpstrFormat, list);
        va_end(list);
 
        /*< SWK 20221226 - #4403 MOD Start >*/
//         CString* pstrLog = new CString(strText);
//         PostThreadMessage(m_ThreadId, WM_DIPLAY_LOG, reinterpret_cast<WPARAM>(pstrLog), 0);
        pLogData plogdata = new LogData;
        ::GetLocalTime(&plogdata->currentTime);
        plogdata->strLog = strText;
        PostThreadMessage(m_ThreadId, WM_DIPLAY_LOG, reinterpret_cast<WPARAM>(plogdata), 0);
        /*< SWK 20221226 - #4403 MOD End >*/
    }
    catch (...)
    {
        return;
    }
 
 
//     WriteToFile(strText);
// 
//     if(m_pDM2P)
//     {
//         m_pDM2P->DM2P_DisplayMessage(strText);
//     }
}
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD END
 
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD START
//< SWK 20221226 - #4403 MOD >
//void CDisplayMessage::ThreadDisplayMessage(const CString& strMessage)
void CDisplayMessage::ThreadDisplayMessage(const LogData& logdata)
{
    WriteToFile(logdata.currentTime, logdata.strLog);
 
    if (m_pDM2P)
    {
        m_pDM2P->DM2P_DisplayMessage(logdata.strLog);
    }
}
//< SWK 20221226 - #4403 MOD >
//BOOL CDisplayMessage::WriteToFile(const CString& strMessage)
BOOL CDisplayMessage::WriteToFile(const SYSTEMTIME& t, const CString& strMessage)
{
    if(m_pFileLog == NULL)    return FALSE;
 
    //EnterCriticalSection(&m_csLog);
 
    //< SWK 20221226 - #4403 MOD >
//    CTime    time = CTime::GetCurrentTime();
    CTime    time(t);
    CString    strTimeStamp = _T("");
    // 날짜가 바뀌면 파일명을 갱신해서 쓴다.
    if ((time.GetMonth() != m_TimeLogFile.GetMonth()) || (time.GetDay() != m_TimeLogFile.GetDay()) || !m_pFileLog)
        MakeLogFile();
 
    if (m_pFileLog->Open(m_strLogFile, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareDenyNone ))
    {
        // 유니코드 파일의 시작은 BOM(0xFEFF) 이어야 한다.
        TCHAR strCommand = 0xFEFF;
        CString strLog = _T("");
        //strTimeStamp.Format(_T("[%02d:%02d:%02d]"), time.GetHour(), time.GetMinute(), time.GetSecond());
        /*< SWK 20221221 - #4403 MOD Start >*/
//        strTimeStamp.Format(_T("[%02d:%02d:%02d_%02d:%02d:%02d]"), time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond());
        strTimeStamp.Format(_T("[%02d:%02d:%02d_%02d:%02d:%02d.%03d]"), time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond(), t.wMilliseconds);
        /*< SWK 20221221 - #4403 MOD End >*/
#ifdef UNICODE
        strLog.Format(_T("%c%s %s\r\n"), strCommand, strTimeStamp, strMessage);
#else
        strLog.Format(_T("%s %s\r\n"), strTimeStamp, strMessage);
#endif
        m_pFileLog->SeekToEnd();
        m_pFileLog->Write(strLog.GetBuffer(0), strLog.GetLength() * sizeof(TCHAR));
        strLog.ReleaseBuffer();
        m_pFileLog->Close();
    }
 
    //LeaveCriticalSection(&m_csLog);
 
    return TRUE;
}
 
DWORD CDisplayMessage::RunThreadDisplayLog(LPVOID param)
{
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        //< SWK 20221226 - #4403 MOD >
//        CString* pstrLog = reinterpret_cast<CString*>(msg.wParam);
        CDisplayMessage::pLogData plogdata = reinterpret_cast<CDisplayMessage::pLogData>(msg.wParam);
        switch (msg.message)
        {
        case WM_DIPLAY_LOG:
            
            /*< SWK 20221226 - #4403 MOD Start >*/
//             g_pLog->ThreadDisplayMessage(*pstrLog);
//             delete pstrLog;
            g_pLog->ThreadDisplayMessage(*plogdata);
            delete plogdata;
            /*< SWK 20221226 - #4403 MOD End >*/
            break;
 
        default:
            break;
        }
    }
    return 0;
}
// #3417 LYWCF AOI Review 전설비 Log Process개선 ADD END