Click here to Skip to main content
13,046,726 members (42,644 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 17 Mar 2001

CRotatingLog - A simple rotary text file class

, 25 Feb 2004
Rate this:
Please Sign up or sign in to vote.
a simple class that implements a rotary (circular) text file


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.


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;
            CString str;
            if (m_plogfile->GetDumpLine(count, str))
        } while (count != start);


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.


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


About the Author

PJ Arends
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to change Pin
Sergey Severin3-Aug-04 5:15
memberSergey Severin3-Aug-04 5:15 
AnswerRe: How to change Pin
PJ Arends3-Aug-04 8:29
memberPJ Arends3-Aug-04 8:29 
GeneralAbout the logfile format Pin
k_g29-Jan-04 4:02
memberk_g29-Jan-04 4:02 
Generalnice code Pin
Ladwal13-Jul-03 23:36
memberLadwal13-Jul-03 23:36 
GeneralNice Code Pin
slim3-Apr-03 1:58
memberslim3-Apr-03 1:58 
QuestionCan it changed to rotate base on date (instead of line) Pin
sanong13-Oct-02 20:19
membersanong13-Oct-02 20:19 
AnswerRe: Can it changed to rotate base on date (instead of line) Pin
PJ Arends14-Oct-02 7:53
memberPJ 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
memberPJ 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
The Eclypse5-Oct-01 8:11
memberThe Eclypse5-Oct-01 8:11 
AnswerRe: How to delete a specific word Pin
PJ Arends5-Oct-01 10:17
memberPJ Arends5-Oct-01 10:17 
GeneralRe: How to delete a specific word Pin
The Eclypse6-Oct-01 4:53
memberThe Eclypse6-Oct-01 4:53 
GeneralRe: How to delete a specific word Pin
PJ Arends6-Oct-01 5:24
memberPJ Arends6-Oct-01 5:24 
GeneralCan't get it to work Pin
The Eclypse1-Oct-01 10:14
memberThe Eclypse1-Oct-01 10:14 
GeneralRe: Can't get it to work Pin
PJ Arends1-Oct-01 11:25
memberPJ Arends1-Oct-01 11:25 
GeneralRe: Can't get it to work Pin
The Eclypse3-Oct-01 4:36
memberThe Eclypse3-Oct-01 4:36 
QuestionWhy a limit? Pin
AlexMarbus8-Apr-01 3:30
memberAlexMarbus8-Apr-01 3:30 
AnswerRe: Why a limit? Pin
PJ Arends8-Apr-01 7:36
memberPJ 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Feb 2004
Article Copyright 2001 by PJ Arends
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid