ash
ash

Reputation: 51

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

Upvotes: 2

Views: 1555

Answers (2)

user188402
user188402

Reputation: 31

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

See http://log4cplus.sourceforge.net/

Upvotes: 3

Adam Matan
Adam Matan

Reputation: 136371

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

Upvotes: 5

Related Questions