void OutputToFile(CString msg, CString time_file)
{
try
{
//设置文件的打开参数
CString logName = _T("");
GetModuleFileName(NULL, logName.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
logName.ReleaseBuffer();
int pos = logName.ReverseFind('\\');
logName = logName.Left(pos); //没有"\\"
CString strTimeFileName = _T("\\") + time_file;
logName += strTimeFileName;
CStdioFile outFile(logName, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite | CFile::typeText);
char* old_locale = _strdup(setlocale(LC_CTYPE, NULL));
setlocale(LC_CTYPE, "chs");
CString msLine;
CTime tt = CTime::GetCurrentTime();
//作为Log文件,经常要给每条Log打时间戳,时间格式可自由定义,
//这里的格式如:2010-June-10 Thursday, 15:58:12
// msLine = tt.Format(_T("[%H:%M:%S] ")) + msg;
msLine += msg;
msLine += _T("\r\n");
//在文件末尾插入新纪录
outFile.SeekToEnd();
outFile.WriteString(msLine);
outFile.Close();
}
catch (CFileException *fx)
{
fx->Delete();
}
}