karthik
karthik

Reputation: 17842

How to limit the log size in c++?

In my application i am creating log file of size 5kb .If i exceeds 5 kb of file size , I have to overwrite the old contents with the help of new contents.

If you have any ideas just share with me.

I also need implementation of this technique in c++

I provide some example

Initially the file look like this

sample.txt

sample application text sample

Assume the above sample text file exceeds 5kb then i added new in the source.txt then the file should be like this.

sample.txt

new sample application text sam

Regards, Karthik

Upvotes: 1

Views: 7296

Answers (6)

Aidan Ryan
Aidan Ryan

Reputation: 11609

log4cxx is a good soution that has already solved this problem plus many more you probably haven't yet considered.

Upvotes: 0

Björn Pollex
Björn Pollex

Reputation: 76848

Here is some code I have recently written to implement a simple log-file rotation:

std::ostream & libLogging::FileRotationLogSink::GetCurrentStream( 
    std::string::size_type required )
{
    if ( static_cast<std::string::size_type>(m_CurrentStream.tellp( )) + 
        required > m_Limit ) {
        m_CurrentStream.close();
        // remove old backup
        if ( boost::filesystem::exists( m_BackupPath ) ) {
            boost::filesystem::remove( m_BackupPath );
        }
        // backup current logfile
        boost::filesystem::rename( m_LogFilePath, m_BackupPath );
        // open new logfile
        m_CurrentStream.open( m_LogFilePath );
    }
    return m_CurrentStream;
}

required gives the size of the next message that is to be written to the log. If the file gets too big, it is copied (old backup is overwritten), and a new one is started.

Upvotes: 5

Nim
Nim

Reputation: 33645

An approach like the following could work:

  1. Stream content to file using iostreams (start with a clean file each time you open)
  2. After streaming content, use tellp to get the last put position (and hence size of file so far).
  3. If the position is bigger than 5Kb, then seekp to begining of stream...

Upvotes: 0

Patrice Bernassola
Patrice Bernassola

Reputation: 14436

Since you are using MFC, you can use the CFile class for file management. This class has a GetLength method that return the size of the file.

To overwrite the old content you can manage a buffer of 5000 char representing your file content. And at each file writing you just have to replace file content.

Upvotes: 0

The GiG
The GiG

Reputation: 2611

You can count the amount of content you inserted into the log, and check if it is more the 5kb every time. Use a function like:

void writeToLog(char c) {
  if(writeIndex == 5000)
    writeIndex =0;
  log[writeIndex] = c;
  writeIndex += 1;
}

With this sure you can implement the string writing function.

Upvotes: 1

vlg789
vlg789

Reputation: 751

Using WinApi, you should

1) Check if file is bigger than a limit using GetFileSize 2) SetFilePointer to 0,0 + SetEndOfFile

Upvotes: 1

Related Questions