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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值