Create New Log File Once it reaches the Size Specified in MFC (VC++ )?
I have created a Log file by name "NDSLog" now i wanted that if NDSLog file exceeds its limit a new file should create by name NDSLog1 and so on...Currently i am able to create NDSLog file and when it exceeds it limit, I close that exisiting file & open a new file .
I want to know i can increment NDSLog to NDSLog1 and so on when it reaches that Limit ?
Any help is highly appreciated.
I am using WritetoLog method for this.
long CNDSLog::WriteLogData(char *logData, long lDataSize)
{
if (m_File == NULL)
{
GetFileName();
OpenNewFile();
}
else
{
long lFileSize = GetFileSize(m_sFileName);
if (lFileSize > m_lFileSize)
{
CloseNewFile();
GetFileName();
OpenNewFile();
}
}
WriteData(logData);
return ERR_NONE;
}
long CNDSLog::GetFileName()
{
char ctemp[300];
int lLen = sprintf(ctemp,"%s",m_sFName.data());
if (lLen > 0)
{
if (m_sFileName != NULL)
{
delete [] m_sFileName;
m_sFileName = NULL;
}
m_sFileName = new char[lLen + 1];
memset(m_sFileName,0,lLen + 1);
memcpy(m_sFileName,ctemp,lLen);
}
return ERR_NONE;
};
long CNDSLog::OpenNewFile()
{
if (m_sFileName != NULL)
{
char strPathName[_MAX_PATH];
::GetM开发者_高级运维oduleFileName(NULL, strPathName, _MAX_PATH);
// The following code will allow you to get the path.
CString newPath(strPathName);
int iPos = newPath.ReverseFind('\\');
if (iPos != -1)
newPath = newPath.Left(iPos+1);
newPath += "NDSLog\\" ;
if (GetFileAttributes(newPath) == INVALID_FILE_ATTRIBUTES)
{
CreateDirectory(newPath,NULL);
}
newPath += m_sFileName;
m_File = fopen(newPath,"at");
}
return ERR_NONE;
}
An esay way is to have a counter in your WriteLogData () function int nCounter passing it to GetFileName(). When you are vreating the filename you could add the counter like that:
sprintf(ctemp,"%s_%d",m_sFName.data(), nCounter);
Now you have your 1, 2, ...
Now your WriteLogData () should be like that:
long CNDSLog::WriteLogData(char *logData, long lDataSize)
{
in nCounter = 1; // or whatever
if (m_File == NULL)
{
GetFileName(nCounter);
OpenNewFile();
}
else
{
long lFileSize = GetFileSize(m_sFileName);
if (lFileSize > m_lFileSize)
{
nCounter++;
CloseNewFile();
GetFileName(nCounter);
OpenNewFile();
}
}
WriteData(logData);
return ERR_NONE;
精彩评论