写日志文件


	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()
{
	//1-创建D:\\LOG, 最基本文件夹
	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;
		}
	}

	//2-根据时间获取文件夹
	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;
	//3-根据时间创建文件
	if( PathFileExists(t_filename))
	{
	}
	else
	{
		FILE*fp = fopen(t_dirbuffer , "a+");
		fclose(fp);
	}
//	m_str_logfilename = (CT2A)t_filename;
	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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值