I
idesilva
Hi,
I have an application which sends/receives messages at a very high
rate. This app needs to 'log' the contents of these messages. Since the
msg rate is high, 'logging' each and every msg to a disk-file reduces
the app's performace significantly. I want to device a 'logging'
mechanism where the 'log' is actually buffered and it will be written
to the disk periodically. The time of writing to disk should be
controlled by the app rather than by the OS. At times, I also want to
reset the in-memory log without writing to disk.
Currently I use an 'ofstream' object for this purpose. But it does not
allow me to *control* buffering. I tried out another option as follows.
I extended the MFC's CMemFile class.
class CMemLogFile : public CMemFile
{
public:
CMemLogFile(CString sName);
virtual ~CMemLogFile();
// Base class overrides
void Close();
void Truncate();
void WriteToDisk();
private:
CFile m_file;
CString m_sName;
};
CMemLogFile::CMemLogFile(CString sName)
{
m_sName = sName;
m_file.Open(sName, CFile::modeCreate|CFile::modeWrite);
}
void CMemLogFile::Close()
{
m_file.Close();
CMemFile::Close();
}
void CMemLogFile::Truncate()
{
this->SetLength(0);
}
void CMemLogFile::WriteToDisk()
{
char buf[READ_LEN] = "";
this->SeekToBegin();
int iBytesRead = 0;
do
{
iBytesRead = this->Read(buf, READ_LEN);
m_file.Write(buf, iBytesRead);
} while ( iBytesRead >= READ_LEN);
m_file.Flush();
}
This works fine except for the fact that I don't have the advance
formatting options such as setw() that ofstream provides.
My questions are:
1. Can I control the buffering of an ofstream? (Not using 'endl' and
the default state 'nounitbuf' doesn't seem to work)
2. Failing (1), is there a way that we can increase the buffer size of
an ofstream buffer?
3. How can you reset an ofstream buffer? (Just like CFile::SetLength(0)
truncates)
I have an application which sends/receives messages at a very high
rate. This app needs to 'log' the contents of these messages. Since the
msg rate is high, 'logging' each and every msg to a disk-file reduces
the app's performace significantly. I want to device a 'logging'
mechanism where the 'log' is actually buffered and it will be written
to the disk periodically. The time of writing to disk should be
controlled by the app rather than by the OS. At times, I also want to
reset the in-memory log without writing to disk.
Currently I use an 'ofstream' object for this purpose. But it does not
allow me to *control* buffering. I tried out another option as follows.
I extended the MFC's CMemFile class.
class CMemLogFile : public CMemFile
{
public:
CMemLogFile(CString sName);
virtual ~CMemLogFile();
// Base class overrides
void Close();
void Truncate();
void WriteToDisk();
private:
CFile m_file;
CString m_sName;
};
CMemLogFile::CMemLogFile(CString sName)
{
m_sName = sName;
m_file.Open(sName, CFile::modeCreate|CFile::modeWrite);
}
void CMemLogFile::Close()
{
m_file.Close();
CMemFile::Close();
}
void CMemLogFile::Truncate()
{
this->SetLength(0);
}
void CMemLogFile::WriteToDisk()
{
char buf[READ_LEN] = "";
this->SeekToBegin();
int iBytesRead = 0;
do
{
iBytesRead = this->Read(buf, READ_LEN);
m_file.Write(buf, iBytesRead);
} while ( iBytesRead >= READ_LEN);
m_file.Flush();
}
This works fine except for the fact that I don't have the advance
formatting options such as setw() that ofstream provides.
My questions are:
1. Can I control the buffering of an ofstream? (Not using 'endl' and
the default state 'nounitbuf' doesn't seem to work)
2. Failing (1), is there a way that we can increase the buffer size of
an ofstream buffer?
3. How can you reset an ofstream buffer? (Just like CFile::SetLength(0)
truncates)