void WriteLog(LPCTSTR pFormat,...);
void WriteLog(LPCTSTR pFormat,...)
{
TCHAR chMsg[4096];
va_list pArg;
va_start(pArg,pFormat);
_vstprintf(chMsg,pFormat,pArg);
va_end(pArg);
SYSTEMTIME st;
::GetLocalTime(&st);
char logname[4096];
memset(logname,'/0',MAX_PATH);
sprintf(logname,"%s%04d-%02d-%02dlog.txt",m_logpath.GetBuffer(m_logpath.GetLength()),st.wYear,st.wMonth,st.wDay);
CStdioFile f;
if(!f.Open( logname, CFile::modeCreate | CFile::modeWrite|CFile::modeNoTruncate|CFile::typeText))
{
DWORD dwVal =::GetLastError();
#ifdef _DEBUG
afxDump << "Unable to open file" << "/n";
#endif
return;
}
f.SeekToEnd();
char tmp[2048];
sprintf(tmp,"%s/t......../t%02d:%02d:%02d.%03d",chMsg,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
f.WriteString(tmp);
f.WriteString("/r/n");
f.Close();
}
本文介绍了一个使用C++编写的日志记录函数WriteLog,该函数能够将带有时间戳的日志信息写入指定路径的文本文件中。通过格式化字符串的方式组织日志内容,并利用C标准库函数实现日期时间的获取与格式化。
1万+

被折叠的 条评论
为什么被折叠?



