Introduction
This class is used to create and maintain a rotating log file. The user
specifies the maximum size of the file at creation time, and when new entries
are added to the file, the oldest ones are dropped, so that the maximum file
size is not exceeded.
User Functions
CRotatingLog::CRotatingLog(const CString &File,
ULONG MaxLines, ULONG LineLength)
throw ( CFileException )
The constructor opens the file File
. The maximum number of lines
allowed in the file is specified by MaxLines, and the length of each
line is specified by LineLength. A CFileException
is thrown
if the file could not be opened.
CRotatingLog::~CRotatingLog()
The destructor closes the file.
BOOL CRotatingLog::AddLine(const CString& Line)
The AddLine
function adds a line of text to the file. If the length
of the line exceeds the maximum length specified in the constructor, it will be
truncated. If the addition of this line causes the file to exceed its maximum
size, the oldest line in the file is deleted. AddLine
returns TRUE
on success, and FALSE
on failure. Use GetLastError()
to
get error information on failure.
ULONG CRotatingLog::GetDumpStart(BOOL bForward)
The GetDumpStart
function is used to get the position of the first
line to be used in the GetDumpLine()
function. Set bForward
to TRUE
to retrieve the lines from oldest to newest, and to FALSE
to retrieve the lines from newest to oldest. GetDumpStart
will
return zero if the file is empty, and -1 if an error occurs. Use GetLastError()
to get error information on failure.
BOOL CRotatingLog::GetDumpLine(ULONG &count, CString &line)
The GetDumpLine
function is used to retrieve a line of text from
the file at the position count. line is used to receive the
string. count is updated to the next line in the file. Please note that
as this is a rotating file, count will go from the last line entered to the
first line entered, thus you will have gone through the entire file when count
is equal the value returned from GetDumpStart
. Set the direction
you want to receive files with the GetDumpStart
function. GetDumpLine
returns TRUE
on success, and FALSE
on failure. Use GetLastError
to get error information on failure.
Reading the File
This example shows how to read the file from the oldest line to the newest line.
The loop ends when the count equals the starting count.
void MyClass::ReadLogFile()
{
ULONG start = m_plogfile->GetDumpStart(TRUE);
if (start > 0)
{
ULONG count = start;
do
{
CString str;
if (m_plogfile->GetDumpLine(count, str))
DoSomething(str);
else
break;
} while (count != start);
}
}
Updates
Feb 15, 2004. |
- The code is now works with VC7.0 and with UNICODE builds.
- Removed the limit on the number of lines. Now the limit is a 4GB total file
size.
|
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.