内容保存乱码,主函数:
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