Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

CRotatingLog - A simple rotary text file class

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
25 Feb 2004CPOL 117.4K   890   35   19
a simple class that implements a rotary (circular) text file

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Canada Canada
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.

Comments and Discussions

 
QuestionHow to change Pin
Sergey Severin3-Aug-04 5:15
Sergey Severin3-Aug-04 5:15 
AnswerRe: How to change Pin
PJ Arends3-Aug-04 8:29
professionalPJ Arends3-Aug-04 8:29 
Currently not possible. But you have the source, just add a function that closes the current file and opens the new one. Not all that hard to do.








"You're obviously a superstar." - Christian Graus about me - 12 Feb '03

"Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04

Within you lies the power for good - Use it!
GeneralAbout the logfile format Pin
k_g29-Jan-04 4:02
k_g29-Jan-04 4:02 
Generalnice code Pin
ladwal13-Jul-03 23:36
ladwal13-Jul-03 23:36 
GeneralNice Code Pin
slim3-Apr-03 1:58
slim3-Apr-03 1:58 
QuestionCan it changed to rotate base on date (instead of line) Pin
sanong13-Oct-02 20:19
sanong13-Oct-02 20:19 
AnswerRe: Can it changed to rotate base on date (instead of line) Pin
PJ Arends14-Oct-02 7:53
professionalPJ Arends14-Oct-02 7:53 
GeneralBad running Pin
Jean-Michel MARINO7-Sep-02 4:22
sussJean-Michel MARINO7-Sep-02 4:22 
GeneralRe: Bad running Pin
PJ Arends7-Sep-02 9:56
professionalPJ Arends7-Sep-02 9:56 
GeneralGood Job!! Pin
redpink3-Sep-02 19:37
sussredpink3-Sep-02 19:37 
QuestionHow to delete a specific word Pin
Nnamdi Onyeyiri5-Oct-01 8:11
Nnamdi Onyeyiri5-Oct-01 8:11 
AnswerRe: How to delete a specific word Pin
PJ Arends5-Oct-01 10:17
professionalPJ Arends5-Oct-01 10:17 
GeneralRe: How to delete a specific word Pin
Nnamdi Onyeyiri6-Oct-01 4:53
Nnamdi Onyeyiri6-Oct-01 4:53 
GeneralRe: How to delete a specific word Pin
PJ Arends6-Oct-01 5:24
professionalPJ Arends6-Oct-01 5:24 
GeneralCan't get it to work Pin
Nnamdi Onyeyiri1-Oct-01 10:14
Nnamdi Onyeyiri1-Oct-01 10:14 
GeneralRe: Can't get it to work Pin
PJ Arends1-Oct-01 11:25
professionalPJ Arends1-Oct-01 11:25 
GeneralRe: Can't get it to work Pin
Nnamdi Onyeyiri3-Oct-01 4:36
Nnamdi Onyeyiri3-Oct-01 4:36 
QuestionWhy a limit? Pin
AlexMarbus8-Apr-01 3:30
AlexMarbus8-Apr-01 3:30 
AnswerRe: Why a limit? Pin
PJ Arends8-Apr-01 7:36
professionalPJ Arends8-Apr-01 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.