Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

File Rotator

4.58/5 (15 votes)
17 Mar 2009CPOL2 min read 43.5K   1.9K  
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:

C++
#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:

C++
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)