Click here to Skip to main content
Click here to Skip to main content

File Rotator

, 17 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Rotating files by appending 00, 01, 02, 03 etc. in file name once the file size reaches its predefined maximum file size.

Introduction

Generally if any application keeps logging something in a file, after some time, the log file size becomes very large. To view or search for something in these large log files is difficult. Most of the time, to avoid large log file size I use a file rotation strategy, i.e. once file size reaches a predefined size limit, close the current log file and start logging in a new file. This keeps the size of log files maintainable.

FileRotator Class

FileRotator is a class that implements the file name rotation logic by appending 0, 1, 2, 3, etc. once the file size reaches its predefined size. In the constructor of this class, we can specify the base filename, maximum file size and maximum file count. This class can be used easily with logger or stream data writer. FileRotator creates a file with base filename provided in the constructor. Once this file reaches its maximum size, it renames the current file to myfile00.txt and creates a new file myfile.txt. Again when the new myfile.txt reaches its maximum size, it again renames the current file to myfile01.txt and creates a new file myfile.txt. This process continues till it reaches the maximum file count defined in the constructor. After this, it starts deleting old files myfile00.txt, myfile01.txt, myfile02.txt, etc. It always writes in myfile.txt. This class internally decides if it needs to append 1 or 01 or 001 etc. based on the maximum file count.

Currently old files deletion logic is file name sequence base, later the same can be improved to file creation time base. I also uses similar logic to save the data coming from a stream. I will try to make this class more generic in future.

Using the Code

Using FileRotator class in your existing application is very simple. You just need to add FileRotator.cpp in your project and include FileRotator.h where you want to use this class.

Add a member variable of FileRotator in your existing logger class. Open and close your file using Open() and Close() method of this class. To write something in your file, use Write() method of this class. In the constructor of this class, specify the filename, maximum file size in bytes on which you want to rotate the file and the maximum file count. Default maximum file count is 99. For saving stream base data, you need to make some changes in the code. May be, I can make it more generic in my next version. Here is a sample Logger class:

#include "FileRotator.h"

class MyLogger
{
public:
    MyLogger() : mFileRotator(1024 /*bytes*/, "myfile.txt")
    {
        mFileRotator.Open();
    }

    ~MyLogger()
    {
        mFileRotator.Close();
    }

    int Log(char *str) 
    {
        //
        // customize your log message like adding date, time, etc. here
        //
        string logStr = string("Info:") + str + string("\n");
        return mFileRotator.Write(logStr.c_str());
}

private:
    FileRotator mFileRotator;
};

Using the above Logger class:

int _tmain(int argc, _TCHAR* argv[])
{
    MyLogger myLogger;
    int i = 0;

    while (i < 10000) {
        myLogger.Log("Sample log text");
        i++;
    }
    return 0;
}

History

  • 17th March, 2009: Initial revision

License

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

Share

About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.
Follow on   LinkedIn

Comments and Discussions

 
QuestionGood idea. But how about making it an IOStreams stream buffer? Pinmemberwtwhite24-Mar-09 5:24 
AnswerRe: Good idea. But how about making it an IOStreams stream buffer? PinmemberManish K. Agarwal24-Mar-09 19:18 
GeneralGood Article. PinmemberTejaSingh18-Mar-09 20:42 
GeneralGood PinmemberOnskee117-Mar-09 8:30 

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 | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Mar 2009
Article Copyright 2009 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid