string m_strfilename;
CEdit m_editLog;
CString m_strLog;
CRITICAL_SECTION m_csLog;
BOOL InitLogFile();
int writelog(string p_str_filename , CString p_str_log ) ;
__int64 GetFolderSize(const std::string& folderPath);
void LogMessage(const CString& message);
#define DIR_PATH "D:\\LOG_报警"
#define MAX_LOG_LEN 1024
__int64 CDlgStepControl::GetFolderSize(const std::string& folderPath)
{
__int64 totalSize = 0;
std::string searchPath = folderPath + "\\*";
WIN32_FIND_DATAA findData;
HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) {
std::string fullPath = folderPath + "\\" + findData.cFileName;
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
totalSize += GetFolderSize(fullPath);
} else {
__int64 fileSize = (__int64)findData.nFileSizeHigh << 32 | findData.nFileSizeLow;
totalSize += fileSize;
}
}
} while (FindNextFileA(hFind, &findData));
FindClose(hFind);
}
return totalSize;
}
BOOL CDlgStepControl::InitLogFile()
{
if( PathFileExists(_T(DIR_PATH)))
{
int t_size = GetFolderSize(DIR_PATH);
if(t_size/(1024*1024*1024) > 5)
{
AfxMessageBox(_T("日志文件过大,请手动删除掉不必要的日志文件"));
}
}
else
{
if(!CreateDirectory(_T(DIR_PATH),NULL))
{
AfxMessageBox(_T("创建项目文件夹失败!"));
return FALSE;
}
}
SYSTEMTIME timeCur;
GetLocalTime(&timeCur);
CString t_filename = _T(DIR_PATH);
string m_str_dirname =DIR_PATH;
char t_dirbuffer[1024] = {0};
sprintf(t_dirbuffer , ("%s\\%04d%02d%02d_%02d.ini"), m_str_dirname.c_str(), timeCur.wYear, timeCur.wMonth, timeCur.wDay,timeCur.wHour);
t_filename=t_dirbuffer;
m_strfilename = t_dirbuffer;
if( PathFileExists(t_filename))
{
}
else
{
FILE*fp = fopen(t_dirbuffer , "a+");
fclose(fp);
}
return true;
}
void CDlgStepControl::LogMessage(const CString& message)
{
EnterCriticalSection(&m_csLog);
DWORD threadId = GetCurrentThreadId();
SYSTEMTIME timeCur;
GetLocalTime(&timeCur);
char t_logbuffer[1024] = {0};
sprintf(t_logbuffer , ("[%04d-%02d-%02d %02d:%02d:%02d:%03d][线程ID%d]")
, timeCur.wYear, timeCur.wMonth, timeCur.wDay
, timeCur.wHour, timeCur.wMinute, timeCur.wSecond , timeCur.wMilliseconds,threadId);
m_strLog += t_logbuffer ;
m_strLog+= _T(" ") + message + _T("\r\n");
m_editLog.SetWindowText(m_strLog);
int nline=m_editLog.GetLineCount();
m_editLog.LineScroll(nline-1);
if(nline==100)
{
m_strLog = "";
}
if(pMain->m_NodeInfo.m_bEnableLog == true)
{
writelog(m_strfilename,message);
}
LeaveCriticalSection(&m_csLog);
}
int CDlgStepControl::writelog(string p_str_filename , CString p_str_log )
{
DWORD threadId = GetCurrentThreadId();
SYSTEMTIME timeCur;
GetLocalTime(&timeCur);
char t_logbuffer[1024] = {0};
sprintf(t_logbuffer , ("[%04d%02d%02d_%02d:%02d:%02d:%03d][线程ID%d]")
, timeCur.wYear, timeCur.wMonth, timeCur.wDay
, timeCur.wHour, timeCur.wMinute, timeCur.wSecond , timeCur.wMilliseconds,threadId);
p_str_log+="\n";
strcat(t_logbuffer ,(CT2A) p_str_log);
t_logbuffer[1024-1]='\0';
FILE*FP = fopen(p_str_filename.c_str() , "a+");
if(FP==NULL)
{
return -1;
}
fwrite(t_logbuffer , 1 , strlen(t_logbuffer),FP);
fclose(FP);
return 0;
}