views:

327

answers:

2

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", CFile::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.

+5  A: 

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, ....

Adam Matan
Sometimes a solution is this easy :)
DaClown
+3  A: 

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

See http://log4cplus.sourceforge.net/

Where can I find the documentation for logging into file as mentioned in question?
yesraaj
http://www.codeproject.com/KB/cpp/Log4cplus.aspx this may do the job, after that search for "log4cplus log rotation" - you'll find lots of useful stuff.