Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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)

Share

About the Author

PJ Arends
President
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionHow to change PinmemberSergey Severin3-Aug-04 5:15 
AnswerRe: How to change PinmemberPJ Arends3-Aug-04 8:29 
GeneralAbout the logfile format Pinmemberk_g29-Jan-04 4:02 
Generalnice code PinmemberLadwal13-Jul-03 23:36 
GeneralNice Code Pinmemberslim3-Apr-03 1:58 
QuestionCan it changed to rotate base on date (instead of line) Pinmembersanong13-Oct-02 20:19 
AnswerRe: Can it changed to rotate base on date (instead of line) PinmemberPJ Arends14-Oct-02 7:53 
GeneralBad running PinsussJean-Michel MARINO7-Sep-02 4:22 
GeneralRe: Bad running PinmemberPJ Arends7-Sep-02 9:56 
GeneralGood Job!! Pinsussredpink3-Sep-02 19:37 
QuestionHow to delete a specific word PinmemberThe Eclypse5-Oct-01 8:11 
AnswerRe: How to delete a specific word PinmemberPJ Arends5-Oct-01 10:17 
GeneralRe: How to delete a specific word PinmemberThe Eclypse6-Oct-01 4:53 
GeneralRe: How to delete a specific word PinmemberPJ Arends6-Oct-01 5:24 
GeneralCan't get it to work PinmemberThe Eclypse1-Oct-01 10:14 
GeneralRe: Can't get it to work PinmemberPJ Arends1-Oct-01 11:25 
GeneralRe: Can't get it to work PinmemberThe Eclypse3-Oct-01 4:36 
QuestionWhy a limit? PinmemberAlexMarbus8-Apr-01 3:30 
AnswerRe: Why a limit? PinmemberPJ Arends8-Apr-01 7:36 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 26 Feb 2004
Article Copyright 2001 by PJ Arends
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid