c++ 写日志

/***********************************************/
/* 函数:用于写日志
/* 参数:strLog:日志内容
/*      strType:日志类型 如 Info(信息) Error(错误) Warning(警告)	
/***********************************************/
void WriteLog(CString strLog, CString strType)
{
	CString strPath = g_strPath + L"\\Log.txt";		//log路径
	CString strTime = CTime::GetCurrentTime().Format(L"[%Y-%m-%d %H:%M:%S]");
	USES_CONVERSION;
	char szStr[MAX_PATH] = { 0 };
	strcpy_s(szStr, sizeof(szStr), W2A(strPath));
	char *sPath = szStr;

	CString strText;
	strText = L"[" + strType + L"]" + strTime + L" " + strLog + L"\r\n";

	FILE *fp = NULL;
	fopen_s(&fp, sPath, "at+");		//以文本形式追加
	if (NULL == fp)
	{
		return;
	}

	char Text[MAX_PATH] = { 0 };
	strcpy_s(Text, sizeof(Text), W2A(strText));
	fprintf(fp, "%s", Text);
	fflush(fp);						//除读写缓冲区,需要立即把输出缓冲区的数据进行物理写入时
	fclose(fp);
}

猜你喜欢

转载自blog.csdn.net/weixin_37999268/article/details/85280200