Reputation: 17842
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
Reputation: 11609
log4cxx is a good soution that has already solved this problem plus many more you probably haven't yet considered.
Upvotes: 0
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
Reputation: 33645
An approach like the following could work:
tellp
to get the last put position (and hence size of file so far).seekp
to begining of stream...Upvotes: 0
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
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
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