Click here to Skip to main content
Licence CPOL
First Posted 17 Mar 2001
Views 83,858
Downloads 729
Bookmarked 32 times

CRotatingLog - A simple rotary text file class

By | 25 Feb 2004 | Article
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)

About the Author

PJ Arends



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to change PinmemberSergey Severin5:15 3 Aug '04  
AnswerRe: How to change PinmemberPJ Arends8:29 3 Aug '04  
GeneralAbout the logfile format Pinmemberk_g4:02 29 Jan '04  
Generalnice code PinmemberLadwal23:36 13 Jul '03  
GeneralNice Code Pinmemberslim1:58 3 Apr '03  
QuestionCan it changed to rotate base on date (instead of line) Pinmembersanong20:19 13 Oct '02  
AnswerRe: Can it changed to rotate base on date (instead of line) PinmemberPJ Arends7:53 14 Oct '02  
GeneralBad running PinsussJean-Michel MARINO4:22 7 Sep '02  
GeneralRe: Bad running PinmemberPJ Arends9:56 7 Sep '02  
GeneralGood Job!! Pinsussredpink19:37 3 Sep '02  
QuestionHow to delete a specific word PinmemberThe Eclypse8:11 5 Oct '01  
AnswerRe: How to delete a specific word PinmemberPJ Arends10:17 5 Oct '01  
GeneralRe: How to delete a specific word PinmemberThe Eclypse4:53 6 Oct '01  
GeneralRe: How to delete a specific word PinmemberPJ Arends5:24 6 Oct '01  
GeneralCan't get it to work PinmemberThe Eclypse10:14 1 Oct '01  
GeneralRe: Can't get it to work PinmemberPJ Arends11:25 1 Oct '01  
GeneralRe: Can't get it to work PinmemberThe Eclypse4:36 3 Oct '01  
QuestionWhy a limit? PinmemberAlexMarbus3:30 8 Apr '01  
AnswerRe: Why a limit? PinmemberPJ Arends7:36 8 Apr '01  

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.

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