在MFC里面写了一个日志,记录一些关键数据,用来出现bug时方便查找问题。代码如下:void Log::WriteLog(CString strLogInfo)
{
if(m_boolWriteLog)
{
try
{
// Get the path of utility
CString strPath;
::GetModuleFileName( NULL, strPath.GetBuffer( MAX_PATH ), MAX_PATH );
strPath.ReleaseBuffer();
strPath = strPath.Left(strPath.ReverseFind(_T('\\')));
strPath += "\\utility.log"; // If the log doesn't exist, create firstly.
CStdioFile outFile(strPath, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite | CFile::typeText);
CString strData;
CTime tt = CTime::GetCurrentTime();
strData = tt.Format("[%Y-%B-%d %A, %H:%M:%S] ") + strLogInfo;
strData += "\n"; outFile.SeekToEnd();
outFile.WriteString( strData );
outFile.Close();
}
catch(CFileException *fx)
{
fx->Delete();
}
}
}
每写一次日志都会调用该函数,每次都会重复打开关闭文件,如果写的日志很多,程序就会运行很慢。怎么简化写日志的过程,本来想收集所有的日志信息,然后一次性写入,但如果程序在运行的过程中出错,日志信息就获取不到,也就没法获得出错的信息,求大神帮助,怎样写日志才合理,既能记录bug出现时的错误信息,又能快速不影响程序性能。mfc日志bug性能
{
if(m_boolWriteLog)
{
try
{
// Get the path of utility
CString strPath;
::GetModuleFileName( NULL, strPath.GetBuffer( MAX_PATH ), MAX_PATH );
strPath.ReleaseBuffer();
strPath = strPath.Left(strPath.ReverseFind(_T('\\')));
strPath += "\\utility.log"; // If the log doesn't exist, create firstly.
CStdioFile outFile(strPath, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite | CFile::typeText);
CString strData;
CTime tt = CTime::GetCurrentTime();
strData = tt.Format("[%Y-%B-%d %A, %H:%M:%S] ") + strLogInfo;
strData += "\n"; outFile.SeekToEnd();
outFile.WriteString( strData );
outFile.Close();
}
catch(CFileException *fx)
{
fx->Delete();
}
}
}
每写一次日志都会调用该函数,每次都会重复打开关闭文件,如果写的日志很多,程序就会运行很慢。怎么简化写日志的过程,本来想收集所有的日志信息,然后一次性写入,但如果程序在运行的过程中出错,日志信息就获取不到,也就没法获得出错的信息,求大神帮助,怎样写日志才合理,既能记录bug出现时的错误信息,又能快速不影响程序性能。mfc日志bug性能
其中有FlushViewOfFile...
这样不实用的,如果某个 BUG 引发错误导致程序崩溃或者突然断电导致电脑关机的话,那么这个程序的退出是强制性的,并不由用户自己控制,所以你写 LOG 的代码并不会被触发的
打日志是调用PostLogMsg给LogThread线程
LogThread线程不断检索是否有日子消息打印...