开发者

How to write logs to files of size 64KB in C++/VC++?

How to write logs to files of size 64KB(to allow notepad to read).Once the file has reached 64KB it should go head and create another , another ...... File names can be automatically generated.

I tried the following code

static int iCounter=1;
CString temp;
      static CStdioFile f(L"c:\\Log1.txt", CFi开发者_StackOverflow社区le::modeWrite | CFile::modeRead |  CFile::modeCreate | CFile::modeNoTruncate);

 int nlength = (int)f.GetLength();
 if(nlength>(nMaxFileSize*1024))
 {
     //need to create a new file
     f.Close();
     iCounter++;
     temp.Format(_T("%s%d%s"), "c:\\Log",iCounter, ".txt");
     f.Open(temp,CFile::modeWrite | CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate);

 }
 f.SeekToEnd();
 f.WriteString(str);
 f.WriteString(L"\r\n");

i am looking for a better alternative.


Write a wrapper class that accepts log strings, writes them to the current log file and keeps a total-string-length counter.

When it reaches your threshold, close the current log file, create a new one, and reset your counter.

You can use a numbering name scheme, like log00001.txt, log 00002.txt, ....


Use log4cplus which certainly can handle it - being properly configured.

See http://log4cplus.sourceforge.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜