Cpp_Log日志内容保存乱码问题笔记

内容保存乱码,主函数:

int main()
{
	//主程序目录
	TCHAR g_szHomePath[MAX_PATH] = { 0 };
	char g_szHomePathAscii[MAX_PATH] = { 0 };

	// 字符复制拷贝应用路径
	_tcscpy_s(g_szHomePath, MAX_PATH, Utilyun::CPath::GetCurDir().c_str());
	strcpy_s(g_szHomePathAscii, MAX_PATH, Utilyun::CPath::GetAppPathAscii().c_str());
	//wprintf_s(L"%s\n", g_szHomePath);

	SYSTEMTIME st = { 0 };
	::GetLocalTime(&st);
	TCHAR szLogFileName[MAX_PATH] = { 0 };
	_stprintf_s(szLogFileName, MAX_PATH, _T("%sLog\\%04d%02d%02d%02d%02d%02d.log"), g_szHomePath, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
	wprintf_s(L"%s\n", szLogFileName);

	// 日志初始化
	CIULog::Init(TRUE, szLogFileName);

	//CIULog::Log(LOG_NORMAL, __FUNCSIG__, _T("test log info."));
	CIULog::Log(LOG_NORMAL, __FUNCSIG__, _T("GroupMemberInfo: AccountID=%u, AccountName=%s, Gray=%d."),"00001", "xiaoming", 100);
}

运行结果:
在这里插入图片描述
修改:

CString temp = "小明";
unsigned int a = 00001;
CIULog::Log(LOG_NORMAL, __FUNCSIG__, _T("GroupMemberInfo: AccountID=%u, AccountName=%s, Gray=%d."), a, temp, 100);

修改结果:
在这里插入图片描述

CIULog

BOOL CIULog::Log(long nLevel, PCSTR pszFunctionSig, PCTSTR pszFmt, ...)
{
#ifdef LOG_OUTPUT	
	//时间
	CString strDebugInfo(GetCurrentTime());
	//错误级别
	CString strLevel(_T("[Normal] "));
	if(nLevel == LOG_WARNING)
		strLevel = _T("[Warning] ");
	else if(nLevel == LOG_ERROR)
		strLevel = _T("[Error] ");
	strDebugInfo += strLevel;
	
	//当前线程信息
	TCHAR szThreadID[32] = {0};
	DWORD dwThreadID = ::GetCurrentThreadId();
	_stprintf_s(szThreadID, ARRAYSIZE(szThreadID), _T("[ThreadID:%u]"), dwThreadID);
	strDebugInfo += szThreadID;
	
	//函数签名
	TCHAR szFuncSig[512] = {0};
	AnsiToUnicode(pszFunctionSig, szFuncSig, ARRAYSIZE(szFuncSig));
	strDebugInfo += _T("[");
	strDebugInfo += szFuncSig;
	strDebugInfo += _T("]");
	
	//log正文
	CString str;
	va_list argList;

	va_start(argList, pszFmt);
	str.FormatV(pszFmt, argList);
	va_end(argList);

	strDebugInfo += str;
	strDebugInfo += _T("\r\n");

	if(m_bToFile)
	{
		if(m_hLogFile == INVALID_HANDLE_VALUE)
			return FALSE;
		
		::SetFilePointer(m_hLogFile, 0, NULL, FILE_END);
		DWORD dwBytesWritten = 0;
		CStringA strAsciiDebugIfo;
		UnicodeToAnsi(strDebugInfo, strAsciiDebugIfo.GetBuffer(strDebugInfo.GetLength()*2), strDebugInfo.GetLength()*2);
		strAsciiDebugIfo.ReleaseBuffer();
		::WriteFile(m_hLogFile, strAsciiDebugIfo.GetString(), strAsciiDebugIfo.GetLength(), &dwBytesWritten, NULL);
		return TRUE;
	}

	::OutputDebugString(strDebugInfo);

#endif // end LOG_OUTPUT

	return TRUE;
}

va_start(argList, pszFmt);
str.FormatV(pszFmt, argList);
va_end(argList);

知识点参考:
https://blog.csdn.net/f110300641/article/details/83822290

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值